1

I want to connect to a Dell X1008 Switch and run some commands. I have used C# Tamir.SharpSsh and Renci.SshNet libraries.

using Tamir.SharpSsh;

SshExec exec = new SshExec("ip address", "username", "password");
exec.Connect();
var output = exec.RunCommand("show vlan");
exec.Close();

But my code freezes on "exec.RunCommand("show vlan")" line.

using Renci.SshNet;
using (var client = new SshClient(Host, UserName, Password))
{
    try
    {
        client.Connect();
    }
    catch (Exception ex)
    {

        throw;
    }

    var command = client.CreateCommand("show vlan");
    return command.Execute();
}

Here my code freezes on "var command = client.CreateCommand(cmd)" line.

Can anyone has idea about it?

FYI : Above code works well for Cisco switches.I can connect to Dell and Cisco switches by Putty software and I am able to run the commands from putty. My requirement is to run the commands from C# application.

Regards

Ravi

live2
  • 3,771
  • 2
  • 37
  • 46
Ravi
  • 61
  • 1
  • 7
  • 1) Do not even try SharpSSh, that's a dead project. 2) Are you sure it hangs on `CreateCommand`? That does nearly nothing. I'd expect it to hang on `command.Execute`. 3) Can you execute the command using `plink hostname show vlan`? (PLink in part of PuTTY package) – Martin Prikryl Sep 25 '17 at 11:47
  • Possibly related: [SSH.NET is not executing command on device](https://stackoverflow.com/q/39992334/850848). – Martin Prikryl Oct 07 '20 at 08:42

1 Answers1

0

Renci.SshNet is the better choice.

Check the response of this method, the command have a timeout.

public string ExecuteCommandSsh(string host, int port, string username, string password, string[] commands)
{
    var returnMessage = string.Empty;

    try
    {
        using (var client = new SshClient(host, port, username, password))
        {
            //Create the command string
            var fullCommand = string.Empty;
            foreach (var command in commands)
            {
                fullCommand += command + "\n";
            }

            client.Connect();
            var sshCommand = client.CreateCommand(fullCommand);
            sshCommand.CommandTimeout = new TimeSpan(0, 0, 10);

            try
            {
                sshCommand.Execute();
                returnMessage = sshCommand.Result;
            }
            catch (SshOperationTimeoutException)
            {
                returnMessage = sshCommand.Result;
            }


            client.Disconnect();
        }
    }
    catch (Exception e)
    {
        //other exception
    }

    return returnMessage;
}
live2
  • 3,771
  • 2
  • 37
  • 46
  • How does this answer the question? You have posted the same code that the OP is using. + Also what do you mean by *"Renci.SshNet is the better library SSH.NET"*? – Martin Prikryl Sep 25 '17 at 13:18
  • The OP have two libaries Tamir.SharpSsh and Renci. My example have a timeout for the command. With the timeout is it possible to show the response. – live2 Sep 25 '17 at 13:25