4

I'm trying to connect to our cisco switch via SSH.NET and run multiple commands on it (up to 200) and read the results. After one command, the connection always disconnects.

Code:

public void ConnectSSH(string username, string password)
{
    sshclient = new SshClient(_name, username, password);
    sshclient.Connect();
}

public string ExecuteSSH(string command)
{
    //if (!sshclient.IsConnected)
    //    sshclient.Connect();
    SshCommand x = sshclient.CreateCommand(command);
    x.Execute();
    return x.Result;
}

public void DisconnectSSH()
{
    sshclient.Disconnect();
    sshclient.Dispose();
}

Sample Usage:

ConnectSSH("user", "pw")
foreach (string exCmd in listToExecute)
    {
        listReturn.Add(ExecuteSSH(exCmd));
    }
DisconnectSSH();

Disconnect happens everytime right after x.Execute(). No Error in x.Error, x.ExitStatus = 0

x.Result is always correct

Obviously, if I take out the comments, it works. But as you can imagine, it's slow as hell and not what I can use.

Would be really glad if someone has an answer.

Luca Salzani
  • 132
  • 1
  • 11

0 Answers0