pxssh throwing End Of File (EOF). Exceptio

2019-03-01 23:39发布

问题:

I am having problem using pxssh module. My code is below:

    try:
        ssh_handle = pxssh.pxssh(timeout=None)
        ssh_handle.logfile = sys.stdout
        ssh_handle.login(host, username, password)
        index = ssh_handle.expect(['Are you sure you want to continue connecting \(yes\/no\)\? ', '.*?password:.*', '.*?\$.*'])
        if index == 0:
            ssh_handle.sendline('yes')
            ssh_handle.sendline(password)
        if index == 1:
            ssh_handle.sendline(password)
        ssh_handle.sendline('sudo -s')
        ssh_handle.sendline(password)
        return ssh_handle
    except pxssh.ExceptionPxssh as e:
        print "SSH connection to %s failed" % host
        sys.exit()

For some reason I keep getting the error:

File "/usr/lib/python2.7/dist-packages/pexpect/pxssh.py", line 313, in login
    if not self.sync_original_prompt(sync_multiplier):
  File "/usr/lib/python2.7/dist-packages/pexpect/pxssh.py", line 205, in sync_original_prompt
    b = self.try_read_prompt(sync_multiplier)
  File "/usr/lib/python2.7/dist-packages/pexpect/pxssh.py", line 168, in try_read_prompt
    prompt += self.read_nonblocking(size=1, timeout=timeout)
  File "/usr/lib/python2.7/dist-packages/pexpect/__init__.py", line 919, in read_nonblocking
    raise EOF('End Of File (EOF). Exception style platform.')
pexpect.EOF: End Of File (EOF). Exception style platform.

I am not sure what I am doing wrong and would appreciate any pointers anyone might have.

Thanks in advance, nav

回答1:

You can either do ssh_handle.expect(pexpect.EOF) and read the result in ssh_handle.before. Or try to remove the '\' from '(yes/no)\?' (i.e. do '(yes/no)?'). The last suggestion worked for me on Suse.

Link: http://pexpect.sourceforge.net/doc/