udgru
udgru

Reputation: 1377

Howto check if Ssh.NET connection is established successfully?

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")

Upvotes: 5

Views: 13419

Answers (3)

user10858952
user10858952

Reputation:

Try using IsConnected Property

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

Upvotes: 0

Owain van Brakel
Owain van Brakel

Reputation: 3349

You can use client.IsConnected for this

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

Upvotes: 1

Yuval Itzchakov
Yuval Itzchakov

Reputation: 149628

You can check the SshClient.IsConnected property:

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

Upvotes: 6

Related Questions