Howto check if Ssh.NET connection is established successfully?

udgru picture udgru · Mar 2, 2015 · Viewed 8.9k times · Source

I would like to get to know how Ssh.NET can tell me if the connection is established successfully:

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();

And how do I proof the result of "client.RunCommand("service apache2 status");" logically?
E.g. if(x == "apache2 is running")

Answer

Yuval Itzchakov picture Yuval Itzchakov · Mar 2, 2015

You can check the SshClient.IsConnected property:

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