Python: How to access ssh with pexpect?

November 25th, 2007 mysurface Posted in python | Hits: 209073 | 24 Comments »

I have research on how to access ssh using scripting language for quite sometimes. For python, there are three libs is in my list:

  • Twisted – an event-driven networking engine written in Python.
  • Paramiko – implements the SSH2 protocol for secure connections to remote machines.
  • Pexpect – spawn a child application and control it as if a human were typing commands.
  • Three libraries have used different approach to gain access to ssh, and finally I choose pexpect for the simplicity. This post will briefly shows you how easy you can access ssh with pexpect lib.

    Pexpect allows you to wait for certain STDOUT, and reacts back by sending your lines back to the ‘shell’. There are three situation while accessing ssh:

    1. Ssh to the remote site for the first time will trigger this:

    The authenticity of host 'myremote (' can't be established.
    RSA key fingerprint is 4d:78:9b:c2:6d:d7:dc:c2:96:f2:95:69:6a:08:f5:1b.
    Are you sure you want to continue connecting (yes/no)?

    So you are suppose to answer yes.

    2. Next it will ask you for password:

    mysurface@myremote's password:

    3. No password is needed at all, because you have already have the key.

    To cater all thiese three scenarios, lets look at the python script

    #!/usr/bin/env python
    import pexpect
    ssh_newkey = 'Are you sure you want to continue connecting'
    # my ssh command line
    p=pexpect.spawn('ssh mysurface@ uname -a')
    if i==0:
        print "I say yes"
    if i==1:
        print "I give password",
    elif i==2:
        print "I either got key or connection timeout"
    print p.before # print out the result

    If connection failed, EOF will be captured and you message as below will be print.

    ssh: connect to host port 22: No route to host

    Else, results of uname -a will be print out by print p.before.

    What If I wanna stay connect to ssh, and able to send my input myself?
    You need to enable interactive mode,

    #!/usr/bin/env python
    import pexpect
    import struct, fcntl, os, sys, signal
    def sigwinch_passthrough (sig, data):
        # Check for buggy platforms (see pexpect.setwinsize()).
        if 'TIOCGWINSZ' in dir(termios):
            TIOCGWINSZ = termios.TIOCGWINSZ
            TIOCGWINSZ = 1074295912 # assume
        s = struct.pack ("HHHH", 0, 0, 0, 0)
        a = struct.unpack ('HHHH', fcntl.ioctl(sys.stdout.fileno(), TIOCGWINSZ , s))
        global global_pexpect_instance
    ssh_newkey = 'Are you sure you want to continue connecting'
    p=pexpect.spawn('ssh mysurface@')
    if i==0:
        print "I say yes"
    if i==1:
        print "I give password",
    elif i==2:
        print "I either got key or connection timeout"
    elif i==3: #timeout
    global global_pexpect_instance
    global_pexpect_instance = p
    signal.signal(signal.SIGWINCH, sigwinch_passthrough)

    It seems complicated, yes it is. Pexpect bundle with a lots of examples that illustrate on how to make use of it. The method sigwinch_passthrough is rip out from one of the examples -

    Download pexpect and start to experience it :D

    Pexpect does not have native support for windows yet, but it works fine on cygwin under windows.

    [tags]pexpect, expect, python ssh, cygwin, python tutorial[/tags]

    24 Responses to “Python: How to access ssh with pexpect?”

    1. Thanks! That was super helpful.

    2. In 2010 there are more options: subprocess, pexpect, paramiko, python-libssh2, twisted.

    3. How can I use above code to write a script, which will run a script in a remote machine via ssh (as described above) and get a variable value to use in script?

      local_machine remote_machine
      [] []

      Please help.

    4. pexpect has another module that makes this much simplier.
      pxssh. It handles all of your extra logic like new key
      password prompting and the like. Note Centos5 and the like has a version of pexpect that fails doing the below code.

      Here is your example using pxssh module:

      import sys
      import pxssh
      import getpass

      user = ‘root’
      host = sys.argv[1]
      except IndexError:
      print ‘Must have host name or ip address’
      password = ‘yourpassword’
      s = pxssh.pxssh()
      s.force_password = True
      s.login (host, user, password, login_timeout=20)
      s.sendline (‘uptime’) # run a command
      s.prompt() # match the prompt
      print s.before,s.after
      except pxssh.ExceptionPxssh, e:
      print “pxssh failed on login.”
      print str(e)
      except OSError:
      print “End session to ” + user + “@” + host

    5. Wow. How really bad that the comment section wacks the formating of the provided code such that it won’t run.

    6. Will this solution work with crontab?

      Then you need to add ssh -i to find your .ssh/id_rsa file

    7. Hi,

      If I use the simple code below to try a simplet connection test:

      import pxssh
      import getpass
      client = pxssh.pxssh()
      hostname = raw_input(‘hostname: ‘)
      username = raw_input(‘username: ‘)
      password = getpass.getpass(‘password: ‘)
      client.login(hostname, username, password)
      print client.before
      except pxssh.ExceptionPxssh, e:
      print “pxssh failed on login.”
      print str(e)

      I receive the error:

      Traceback (most recent call last):
      File “”, line 43, in
      File “/usr/lib/python2.6/site-packages/”, line 243, in login
      if not self.synch_original_prompt():
      File “/usr/lib/python2.6/site-packages/”, line 134, in synch_original_prompt
      self.read_nonblocking(size=10000,timeout=1) # GAS: Clear out the cache before getting the prompt
      File “/usr/lib/python2.6/site-packages/”, line 824, in read_nonblocking
      raise TIMEOUT (‘Timeout exceeded in read_nonblocking().’)
      pexpect.TIMEOUT: Timeout exceeded in read_nonblocking().

      Can you help me?

    8. This is a really great read for me, Have to admit you are 1 in the best bloggers I ever saw.Thanks for posting this informative article.

    9. There are specific breeds lower susceptible to chocolate however never give
      your dog chocolate by choice. Often described as “
      a large dog in the body of a small dog,” it is similar to the Norwegian Buhund
      and related to modern Welshi Corgis as well as Shetland Sheepdogs.
      This time frame accounts for approximately 2,000 generations of humans sincee the great
      split, during which time modern genetic features developed independently.

    10. When I run the second script with my server details I get the following

      Traceback (most recent call last):
      File “”, line 1, in
      File “.\”, line 429, in __init__
      self._spawn (command, args)
      File “.\”, line 514, in _spawn
      command_with_path = which(self.command)
      File “.\”, line 1782, in which
      pathlist = string.split (p, os.pathsep)
      AttributeError: ‘module’ object has no attribute ‘split’

      I am running Python 3.3 on Windows 7

      Can anybody help resolve my issue

    11. I’m not sure where you are getting your info, but good topic.
      I needs to spend some time learning much more or understanding more.
      Thanks for wonderful information I was looking for this info for my mission.

    12. Nice blog here! Also your site loads up fast!
      What web host are you using? Can I get your
      affiliate link to your host? I wish my web site loaded up as fast as yours lol

    13. He might try to convince you by giving you false promises about quality
      but do not be moved by it and do not sign any agreement also and neither should
      you give any advance. Merchants that sell through Amazon also complain that its very hard to
      kick the habit. Only when a customer is searching for a product that Amazon doesnt stock will they find, or even consider, your offer because Amazon owns the customer relationship.

    14. Thank you, I’ve just been searching for info about this subject for
      a long time and yours is the best I’ve found out so far.

      But, what in regards to the conclusion? Are you sure concerning the supply?

    15. This therapy for hair loss has also proved to extremely helpful.
      The primary role of adult stem cells in a living organism is to maintain and repair the tissue in which they are found.
      Learn more about the author, Gregg Rio, and other subjects
      at his websites propertyinsantacruz.

    16. For more information about writing, networking, publishing, and book promotion, or to sign up for free email delivery of WITS newsletter,
      please visit. Embryonic stem cells can be created solely for research on stem cells or
      embryos may be remaining from other processes, most likely in vitro
      fertilization. The Advantages of the stem cell
      Therapy are as follows:.

    17. Hi to every , because I am really eager of reading
      this webpage’s post to be updated daily. It includes good information.

    18. An interesting discussion is worth comment. I think that you ought to write more on this
      subject, it may not be a taboo subject but generally people do not talk about such
      issues. To the next! Cheers!!

    19. When ever i try to go to enable mode on ASA using sendline, it appends it with a character on the lft side. Here is program output in debug mode:

      Type help or ‘?’ for a list of available commands.
      asabox/sec/act> enable
      Password: abcdefgh
      (+) Querying configuration(please be patient)

      asabox/sec/act# terminal pager 0
      terminal pager 0

      asabox/sec/act# show run
      -show run ####### if you see here show starts with -, not sure why ?
      ERROR: % Invalid input detected at ‘^’ marker.
      asabox/sec/act# (+) Grabbing for asabox
      (+) Config saved to asabox-RunningConfigBackup-1427647110.TXT

      Here is the actual code i am using:

      print “(+) Sending SSH Password”
      print “(+) Querying configuration(please be patient)”
      child.sendline(“terminal pager 0”)
      child.expect (“.# “)
      child.sendline(‘show run’)
      child.expect(“.*#”, timeout=200)

      Could you please advise ???
      Best Regards

    20. […] You can also use pexcept for this, it’s known to work with SSH – example usage. […]

    21. I like the helpful information you provide to your articles. I?l bookmark your blog and check once more right here frequently. I am relatively certain I will be informed a lot of new stuff proper right here! Good luck for the following!

    22. My programmer is trying to convince me to move to .net from PHP. I have always disliked the idea because of the expenses. But he’s tryiong none the less. I’ve been using WordPress on a variety of websites for about a year and am worried about switching to another platform. I have heard very good things about Is there a way I can import all my wordpress content into it? Any kind of help would be greatly appreciated!

    23. how to mention the option ssh -X using pxssh ?

    Leave a Reply