我在Python中使用Pexpect编写了以下脚本:
该程序使用Pexpect模块自动与SSH服务器建立SSH会话。
import pexpect
import argparse
PROMPT = ['# ', '>>> ', '> ', '\$ ']
def send_command(child, cmd, password):
child.sendline(cmd)
ret = child.expect(PROMPT + ['password'])
if ret < len(PROMPT):
print(child.before)
else:
child.sendline(password)
child.expect(PROMPT)
print(child.before)
def connect(user, host, password):
ssh_newkey = 'Are you sure you want to continue connecting'
passwd = '[P|p]assword'
connStr = 'ssh ' + user + '@' + host
child = pexpect.spawn(connStr)
ret = child.expect([pexpect.TIMEOUT, ssh_newkey, passwd])
if ret == 0: # timeout
print('[-] Error Connecting')
return
if ret == 1: # new key
child.sendline('yes')
ret = child.expect([pexpect.TIMEOUT, passwd])
if ret == 0: # timeout
print('[-] Error Connecting')
return
child.sendline(password)
child.expect(PROMPT)
return child
def main():
parser = argparse.ArgumentParser(description='SSH-client')
parser.add_argument('user', help='SSH-user')
parser.add_argument('host', help='SSH-host')
parser.add_argument('password', help='SSH-password')
parser.add_argument('-c', '--command', default='sudo cat /etc/shadow', help='command to execute')
args = parser.parse_args()
child = connect(args.user, args.host, args.password)
send_command(child, args.command, args.password)
if __name__ == '__main__':
main()
在Eclipse和Bash shell中使用相同的命令行参数,我会得到不同的结果:
使用Eclipse一切都按预期工作,程序终止于命令输出。但是,shell给出了以下超时错误:
Traceback (most recent call last):
File "/usr/local/lib/python3.4/dist-packages/pexpect/__init__.py", line 1535, in expect_loop
c = self.read_nonblocking(self.maxread, timeout)
File "/usr/local/lib/python3.4/dist-packages/pexpect/__init__.py", line 968, in read_nonblocking
raise TIMEOUT('Timeout exceeded.')
pexpect.TIMEOUT: Timeout exceeded.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "ssh_bot.py", line 46, in <module>
main()
File "ssh_bot.py", line 42, in main
child = connect(args.user, args.host, args.password)
File "ssh_bot.py", line 32, in connect
child.expect(PROMPT)
File "/usr/local/lib/python3.4/dist-packages/pexpect/__init__.py", line 1451, in expect
timeout, searchwindowsize)
File "/usr/local/lib/python3.4/dist-packages/pexpect/__init__.py", line 1466, in expect_list
timeout, searchwindowsize)
File "/usr/local/lib/python3.4/dist-packages/pexpect/__init__.py", line 1568, in expect_loop
raise TIMEOUT(str(err) + '\n' + str(self))
pexpect.TIMEOUT: Timeout exceeded.
<pexpect.spawn object at 0x7f7e517406a0>
version: 3.3
command: /usr/bin/ssh
args: ['/usr/bin/ssh', 'nlykkei@127.0.0.1']
searcher: <pexpect.searcher_re object at 0x7f7e51740780>
buffer (last 100 chars): b': Tue Aug 11 12:32:35 2015 from localhost\r\r\n\x1b[01;32mnlykkei@nlykkei-ThinkPad-X200s\x1b[01;34m ~ $\x1b[00m '
before (last 100 chars): b': Tue Aug 11 12:32:35 2015 from localhost\r\r\n\x1b[01;32mnlykkei@nlykkei-ThinkPad-X200s\x1b[01;34m ~ $\x1b[00m '
after: <class 'pexpect.TIMEOUT'>
match: None
match_index: None
exitstatus: None
flag_eof: False
pid: 4093
child_fd: 3
导致此错误的原因是什么,为什么我不能使用相同的命令行参数从Eclipse和Bash获得相同的结果?
谢谢!