Remote command execution in python using paramiko that supports arbitrary input

I recently decided to use paramiko to develop a remote command execution tool.

It was very easy to setup initially and ran much faster than my existing pexpect implementation but it had a problem with sudo commands because they required the password to be provided as input.

I solved the problem by using a pseudo-terminal and by creating my own ChannelFile objects for stdin and stdout/stderr. The solution should be general enough to handle any case that requires simple input but it is not as flexible as pexpect. I hope that you find it useful.

Current Version

This is similar to the older version below but it handles more than 64KiB by eliminating the stdout buffer, the stdin buffer and using recv polling. It even does a silly check for input. That check could be expanded to make it behave more like pexpect but that is for another day.

Older Version

This version will not handle more than 64KiB of output. I am not sure why.

13 thoughts on “Remote command execution in python using paramiko that supports arbitrary input”

  1. Hello Joe!

    Thanks a lot for you code.
    I was debugging my code for the last two days untill I ran into your page…

    My code was missing the .get_pty() call

    Anyhow, thanks a lot for your help.

     

     

    1. It is not clear from your description what is happening. How does it fail? Are you using a cygwin environment? Which version of windows are you using?

  2. Hi Joe!

    Great post! I am new to development and have been spending considerable time understanding the instantiation of classes and functions in your script to tune it for my needs.

    Had a couple questions:

    session.send(data) on line 242, how is that working? I understand it pushes the sudo password but couldn’t find a method “send” that was executing this?
    How is _run_send_input being used? I don’t see it referenced anywhere?

    What I am doing with this:

    I am trying to tune this and combine it with peexpect potentially to login to a Linux System that is connected to a Fortinet FW CLI and issue certain commands recording the output

    Feel free to suggest best ways to do this and very much appreciate the write up and reading of my questions above!

  3. Hi Joe,

    Thanks for the script. I used your script it works fine but at the last I get timeout error. Even if i set timeout to 100 it wait till that time and gives timeout error.

    following is the output of the script:

    Resolving deltas:  98% (8714/8889)
    Resolving deltas:  99% (8806/8889)
    Resolving deltas: 100% (8889/8889)
    Resolving deltas: 100% (8889/8889), done.

    ERROR: timeout after 100 seconds
    I  am tryign to clone a repo on a remote machine. AFter the above output it check for the connection and then print done but the script is not waiting for that line and it always timeout. I increased the timeout to very high value but then also it will give same error. this is not time -out error but its failing to read the last lines and giving this error.

    please sugges.

     

    thanks,

    Madhusudan

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.