我正在使用pxssh进行简单的ssh登录。关注T.J.来自Cookbook的Connor脚本。
我可以使用pxssh成功进入远程计算机,并在尝试使用python解释器时传递命令。 以下是观点。
>>> 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)
现在当我再次尝试同样的事情时,我得到了这个
>>> 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.
>>>
下面的脚本也会抛出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()
-----输出--------------
[-] Error Connecting
End Of File (EOF). Exception style platform.
再次跟随
中的几个线程/引用http://stackoverflow.com/questions/24919980/pxssh-throwing-end-of-file-eof-exception-style-platform-exception
http://pexpect.sourceforge.net/doc/
----------新剧本--------------
#! /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()
-------输出-------------
[-] Error Connecting
End Of File (EOF). Exception style platform.
我在哪里错了?
答案 0 :(得分:0)
经过大量的例子/文章后,终于想出了什么阻止了pxssh成功登录SSH。
来自pxssh的connect()方法接受&#34; login_timeout = 10&#34;默认情况下。在这里,SSH登录远程计算机的时间超过10秒,因此login()引发了ExceptionPxssh异常。该例外具有误导性,因为它说&#34; pexpect.exceptions.EOF:文件结束(EOF)。异常风格平台。&#34;
无论如何设置login_timeout = 15,pxssh脚本都会通过。以下是解决方案代码。
#! /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()