如何检查Ssh.NET连接是否建立成功?

Howto check if Ssh.NET connection is established successfully?

我想知道Ssh.NET如何告诉我连接是否成功:

SshClient client = new SshClient("127.0.0.1", 22, "root", "");
client.Connect();

// Connection ok? Then continue, else display error

SshCommand x = client.RunCommand("service apache2 status");
client.Disconnect();
client.Dispose();

以及如何证明 "client.RunCommand("service apache2 status 的结果");"逻辑上?
例如。如果(x == "apache2 is running")

您可以查看 SshClient.IsConnected 属性:

if (!client.IsConnected)
{
   // Display error
}

你可以使用 client.IsConnected 来完成这个

using (var client = new SshClient("127.0.0.1", 22, "root", "")) {
    client.Connect();

    if (client.IsConnected) {
        SshCommand x = client.RunCommand("cd ~");
    } else {
        Console.WriteLine("Not connected");
    }

    client.Disconnect();
}

尝试使用 IsConnected 属性

If cSSH.IsConnected Then
    Dim x As SshCommand = cSSH.RunCommand("service apache2 status")
    con.Text = "Success"
Else
    con.Text = "Error! Connection Failed"
End If