Got stuck with pxssh. It doesn't login again after the first successful login?
<p>I'm working on simple ssh login using pxssh. Following T.J. Connor scripts from Cookbook.</p>

<p>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.</p>

<pre><code>&gt;&gt;&gt; from pexpect import pxssh
&gt;&gt;&gt; s = pxssh.pxssh()
&gt;&gt;&gt; s.login("192.168.1.107", "shineesh", "password123")
True
&gt;&gt;&gt; s
&lt;pexpect.pxssh.pxssh object at 0xb72d772c&gt;
&gt;&gt;&gt; s.sendline("ifconfig")
9
&gt;&gt;&gt; s.prompt()
True
&gt;&gt;&gt; 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)
</code></pre>

<p>Now when I try the same thing again, I get this</p>

<pre><code>&gt;&gt;&gt; s = pxssh.pxssh()
&gt;&gt;&gt; s.login("192.168.1.107", "shineesh", "password123")
Traceback (most recent call last):
File "&lt;stdin&gt;", line 1, in &lt;module&gt;
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.
&gt;&gt;&gt; 
</code></pre>

<p>The script below also throws an EOF exeception </p>

<pre><code>#! /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()
</code></pre>

<p>-----Output Below--------------</p>

<pre><code>[-] Error Connecting
End Of File (EOF). Exception style platform.
</code></pre>

<hr>

<p>Again followed a few threads/references from</p>

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

<p>----------New Script--------------</p>

<pre><code>#! /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()
</code></pre>

<p>-------Output-------------</p>

<pre><code>[-] Error Connecting
End Of File (EOF). Exception style platform.
</code></pre>

<hr>

<p>Where am I going wrong here ?</p>

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

<p>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."</p>

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

<hr>

<pre><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()
</code></pre>

<hr>

python python-2.7 ssh pexpect pxssh