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

时间:2015-03-02 15:10:56

标签: c# ssh.net

我想了解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正在运行”)

3 个答案:

答案 0 :(得分:4)

您可以查看SshClient.IsConnected属性:

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

答案 1 :(得分: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();
}

答案 2 :(得分:0)

尝试使用IsConnected属性

BETWEEN