简体   繁体   中英

Got stuck with pxssh. It doesn't login again after the first successful login?

I'm working on simple ssh login using pxssh. Following TJ Connor scripts from Cookbook.

I was able to successfully get into the remote machine using pxssh and pass commands when trying from the python interpreter. Below is the view.

>>> from pexpect import pxssh
>>> s = pxssh.pxssh()
>>> s.login("192.168.1.107", "shineesh", "password123")
True
>>> s
<pexpect.pxssh.pxssh object at 0xb72d772c>
>>> s.sendline("ifconfig")
9
>>> s.prompt()
True
>>> print s.before
ifconfig
eth0      Link encap:Ethernet  HWaddr 3X:XX:XX:XX:XX:X4  
      UP BROADCAST MULTICAST  MTU:1500  Metric:1
      RX packets:0 errors:0 dropped:0 overruns:0 frame:0
      TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
      collisions:0 txqueuelen:1000 
      RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

Now when I try the same thing again, I get this

>>> s = pxssh.pxssh()
>>> s.login("192.168.1.107", "shineesh", "password123")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/dist-packages/pexpect/pxssh.py", line 331, in login
if not self.sync_original_prompt(sync_multiplier):
File "/usr/lib/python2.7/dist-packages/pexpect/pxssh.py", line 218, in sync_original_prompt
b = self.try_read_prompt(sync_multiplier)
File "/usr/lib/python2.7/dist-packages/pexpect/pxssh.py", line 182, in try_read_prompt
prompt += self.read_nonblocking(size=1, timeout=timeout)
File "/usr/lib/python2.7/dist-packages/pexpect/pty_spawn.py", line 455, in read_nonblocking
return super(spawn, self).read_nonblocking(size)
File "/usr/lib/python2.7/dist-packages/pexpect/spawnbase.py", line 149, in read_nonblocking
raise EOF('End Of File (EOF). Exception style platform.')
pexpect.exceptions.EOF: End Of File (EOF). Exception style platform.
>>> 

The script below also throws an EOF exeception

#! /usr/bin/python -tt
from pexpect import pxssh
import pexpect

def send_commands(s, command):
    s.sendline(command)
    s.prompt(pexpect.EOF)
    print s.before
def connect(host, username, password):
    try:
            child = pxssh.pxssh()
            child.login(host, username, password)
            return child
    except Exception, e:
            print "[-] Error Connecting\n" +  str(e)
            exit(0)

def main():

    s = connect('192.168.1.107', 'shineesh', 'password123')
    send_commands(s, 'ifconfig')

if __name__ == '__main__':
    main()

-----Output Below--------------

[-] Error Connecting
End Of File (EOF). Exception style platform.

Again followed a few threads/references from

http://stackoverflow.com/questions/24919980/pxssh-throwing-end-of-file-eof-exception-style-platform-exception
http://pexpect.sourceforge.net/doc/

----------New Script--------------

#! /usr/bin/python -tt
from pexpect import pxssh
import pexpect

def send_commands(s, command):
    s.sendline(command)
    s.prompt(pexpect.EOF)
    print s.before
def connect(host, username, password):
    try:
            child = pxssh.pxssh()
            child.login(host, username, password)
            child.expect(pexpect.EOF, timeout=None)
            return child
    except Exception, e:
            print "[-] Error Connecting\n" +  str(e)
            exit(0)

def main():

    s = connect('192.168.1.107', 'shineesh', 'password123')
    send_commands(s, 'ls -all')

if __name__ == '__main__':
    main()

-------Output-------------

[-] Error Connecting
End Of File (EOF). Exception style platform.

Where am I going wrong here ?

After going through a lot of examples/articles, finally figured what was stopping pxssh from a successful SSH login.

The connect() method from pxssh takes in "login_timeout=10" by default. In here the SSH login to the remote machine was taking more than 10 secs and thus login() was raising a ExceptionPxssh exception. The exception was misleading as it said "pexpect.exceptions.EOF: End Of File (EOF). Exception style platform."

Anyways on setting login_timeout=15, pxssh script gets through. Below is the solution code.


#! /usr/bin/python -tt
from pexpect import pxssh

def send_commands(s, command):
    s.sendline(command)
    s.prompt()
    print s.before
def connect(host, username, password):
    try:
            child = pxssh.pxssh()
            child.login(host, username, password, login_timeout=15)
            return child
    except pxssh.ExceptionPxssh, e:
            print "[-] Error Connecting\n" +  str(e)
            exit(0)
def main():

    s = connect('192.168.1.107', 'shineesh', 'password123')
    send_commands(s, 'ifconfig')

if __name__ == '__main__':
    main()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM