如何检查Ssh.NET连接是否成功建立
本文关键字:连接 NET 是否 成功 建立 Ssh 何检查 检查 | 更新日期: 2024-09-23 04:29:33
我想知道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正在运行")
您可以检查SshClient.IsConnected
属性:
if (!client.IsConnected)
{
// Display error
}
您可以使用客户端。已为此连接
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