Skip to content

Running RunCommand in the SshClient class on an cisco asa results in hanging forever #52

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ravelund opened this issue Jul 27, 2016 · 15 comments

Comments

@ravelund
Copy link

Running the bellow code on a Cisco ASA result in an forever hang.

using (var client = new SshClient(host, 22, username, password))
{
client.Connect();
var result = client.RunCommand(command);
returnString = result.Result;
client.Disconnect();
}

@drieseng
Copy link
Member

On what line would it actually block?
Can you build SSH.NET from source and do some troubleshooting yourself, or can you provide me access to a Cisco ASA that is not in use?

@ravelund
Copy link
Author

Hello. I have already downloaded the source and can do some simple troubleshooting tommorow. I will look into it i maybe have ASA we can test on.
Also thanks for the library it works very well on the Catalysts switches.

@ravelund
Copy link
Author

Hello. Again after some stepping the command highlighted is where it hangs for eternity.
sshstop

@ravelund
Copy link
Author

Have fixed an asa you can test on. Should i send the login information to the email on your profile?

@drieseng
Copy link
Member

Yes, that's ok. I'm not sure I'll have time today, but I'll look into it asap.
Please include the command that you were trying to execute as well.
Thanks!

@pincho3xxx
Copy link

I've exactly the same problem when I try acces to Mitrastar FTTH router.

@drieseng
Copy link
Member

The problem with the ASA was that it does not properly support the "exec" channel request message.
Perhaps this is a scenario that the ASA - and similar devices - do not want to support.
Instead of executing the command you specify, it:

  • creates a shell
  • writes the command to the shell
  • continues waiting for input

I've supplied @ravelund with a solution offline, which uses our ShellStream - with its Expect capabilities - instead.

@darinkes
Copy link
Collaborator

@drieseng seems to be correct.

Found this via google:
http://www.carbon60.com/it-advice/powershell-ssh-module-nonstandard-devices-like-cisco-asa

Sounds like you have to work with Shell + Expect.

@ravelund
Copy link
Author

Also with @drieseng input i have created this function:

    /// <summary>
    /// Execute a command on a host using ssh
    /// </summary>
    /// <param name="host">Ip address or Hostname</param>
    /// <param name="port">Ssh port</param>
    /// <param name="username">Username</param>
    /// <param name="password">Password</param>
    /// <param name="commands">Commands to execute</param>
    /// <param name="sendUsernameAndPasswordAsCommand">If set to true the query will send the username and password before running main command. For cisco catalyst and Firewalls False.</param>
    /// <returns>String with line shifts</returns>
    public string ExecuteCommandSsh(string host,int port, string username, string password, string[] commands, bool sendUsernameAndPasswordAsCommand)
    {
        string returnString = "";
        try
        {
            using (var client = new SshClient(host, port, username, password))
            {
                //Create the command string
                string command = "";
                foreach (var com in commands)
                {
                    command += com + "\n";
                }
                client.Connect();
                SshCommand runcommand = client.CreateCommand(command);
                if (sendUsernameAndPasswordAsCommand)
                {
                    runcommand = client.CreateCommand($"{username}\n{password}\n{command}");


                }
                runcommand.CommandTimeout = CommandTimeOut;

                try
                {
                    runcommand.Execute();
                    returnString = runcommand.Result;
                }
                catch (SshOperationTimeoutException)
                {
                    ErrorMessage += "ExecuteCommandSsh: Command timeout.";
                    returnString = runcommand.Result;
                }


               client.Disconnect();
            }
        }
        catch (Exception e)
        {

            DidThrowError = true;
            ErrorMessage += $"ExecuteCommandSsh Exception: {e.ToString()}";

        }

        return returnString;
    } //ExecuteCommandSsh end

By suppling the commands string array with
enable
"enablepassword"
terminal pager 0
sh ver
sh run

The terminal pager 0 command on the asa skips the more question. And this works.

The only - here is that you have to wait for the command to timeout.
I have set the timeout to 30 seconds at this moment.

@drieseng
Copy link
Member

I actually sent you a basic implemention that uses ShellStream and Except.
If you prefer the code above, then that's fine by me.

@NNskelly
Copy link

Would you mind posting the workaround solution, @drieseng ? I'm encountering what looks like the same issue, but working from a prebuilt dll that I don't have the luxury of picking apart in the debugger. I'm going to try @ravelund 's solution for now, but if there's a more correct/compact implementation, that would be useful.

@ravelund
Copy link
Author

ravelund commented May 28, 2017 via email

@ravelund
Copy link
Author

ravelund commented May 28, 2017 via email

@NNskelly
Copy link

Thanks! Trying an explicitly constructed command with a timeout as per the first example still jammed up for me for the duration of the timeout, and returned without apparently having executed anything. It's nice to have the Expect solution on record as an alternative.
I ultimately sorted my use case out by realizing there was a dedicated SshNet.Sftp module already available and I didn't need to be doing commands manually to begin with. I would have gone so far as questioning my use of ftp syntax/protocol, except things as simple as a cd or a pwd were also jamming.

@li379395535
Copy link

@ravelund the second solution worked for me, Thank a lot!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants