5

我想知道 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");”的结果 逻辑上?
例如 if(x == "apache2 正在运行")

4

3 回答 3

6

您可以检查该SshClient.IsConnected属性:

if (!client.IsConnected)
{
   // Display error
}
于 2015-03-02T15:15:49.540 回答
1

您可以为此使用 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();
}
于 2015-03-02T15:17:12.920 回答
0

尝试使用 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
于 2019-01-09T15:42:32.407 回答