Skip to content

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

Closed

Description

@ravelund

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

Activity

  1. drieseng commented on Jul 27, 2016

    @drieseng
    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?

  2. ravelund commented on Jul 27, 2016

    @ravelund
    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.

  3. ravelund commented on Jul 28, 2016

    @ravelund
    Author

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

  4. ravelund commented on Jul 28, 2016

    @ravelund
    Author

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

  5. drieseng commented on Jul 28, 2016

    @drieseng
    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!

  6. pincho3xxx commented on Jul 28, 2016

    @pincho3xxx

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

  7. drieseng commented on Jul 29, 2016

    @drieseng
    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.

  8. darinkes commented on Jul 29, 2016

    @darinkes
    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.

  9. ravelund commented on Jul 29, 2016

    @ravelund
    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.

  10. drieseng commented on Jul 29, 2016

    @drieseng
    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.

  11. NNskelly commented on May 26, 2017

    @NNskelly

    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.

  12. ravelund commented on May 28, 2017

    @ravelund
    Author
  13. ravelund commented on May 28, 2017

    @ravelund
    Author
  14. NNskelly commented on May 30, 2017

    @NNskelly

    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.

  15. li379395535 commented on Mar 8, 2018

    @li379395535

    @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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions