简体   繁体   English

卡住了pxssh。 第一次成功登录后,它没有再次登录?

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

I'm working on simple ssh login using pxssh. 我正在使用pxssh进行简单的ssh登录。 Following TJ Connor scripts from Cookbook. 遵循Cookbook中的TJ Connor脚本。

I was able to successfully get into the remote machine using pxssh and pass commands when trying from the python interpreter. 从python解释器尝试时,我能够使用pxssh成功进入远程计算机并传递命令。 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 下面的脚本也引发了EOF误解

#! /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. 在浏览了许多示例/文章之后,终于弄清楚了阻止pxssh成功进行SSH登录的原因。

The connect() method from pxssh takes in "login_timeout=10" by default. pxssh中的connect()方法默认情况下采用“ login_timeout = 10”。 In here the SSH login to the remote machine was taking more than 10 secs and thus login() was raising a ExceptionPxssh exception. 在这里,到远程计算机的SSH登录花费了10秒钟以上的时间,因此login()引发了ExceptionPxssh异常。 The exception was misleading as it said "pexpect.exceptions.EOF: End Of File (EOF). Exception style platform." 该异常说“ pexpect.exceptions.EOF:文件结束(EOF)。异常样式平台。”

Anyways on setting login_timeout=15, pxssh script gets through. 无论如何,在设置login_timeout = 15时,pxssh脚本都会通过。 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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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