ssh远程机器并使用pexp运行“ls-l”

2024-04-16 12:04:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我想ssh远程机器并使用pexpect运行ls-l。我是一个学习python语言的系统工程师,不懂编程。有人能帮我一下吗。提前谢谢。

我的代码:

import pexpect
child = pexpect.spawn('/usr/bin/ssh root@192.168.32.1')
child.expect('password:', timeout=120)
child.sendline('pass123')
child.expect ('prompt# ')
#child.maxread=100000
child.sendline ('uname -a')
child.expect ('prompt# ')
print child.before, child.after

下面是运行上述代码时的错误输出。

usr/bin/python /root/PycharmProjects/IS_LAB/pexpect-test.py
Traceback (most recent call last):
  File "/root/PycharmProjects/IS_LAB/pexpect-test.py", line 36, in <module>
    child.expect ('prompt# ')
  File "/usr/lib/python2.6/site-packages/pexpect/__init__.py", line 1451, in expect
    timeout, searchwindowsize)
  File "/usr/lib/python2.6/site-packages/pexpect/__init__.py", line 1466, in expect_list
    timeout, searchwindowsize)
  File "/usr/lib/python2.6/site-packages/pexpect/__init__.py", line 1568, in expect_loop
    raise TIMEOUT(str(err) + '\n' + str(self))
pexpect.TIMEOUT: Timeout exceeded.
<pexpect.spawn object at 0x9b4110>
version: 3.3
command: /usr/bin/ssh
args: ['/usr/bin/ssh', 'root@192.168.32.1']
searcher: <pexpect.searcher_re object at 0x9b4450>
buffer (last 100 chars): 'cape.canonical.com/\r\n\r\nLast login: Tue Jun 16 10:26:18 2015 from 192.168.32.1\r\r\nroot@ubuntu14:~# '
before (last 100 chars): 'cape.canonical.com/\r\n\r\nLast login: Tue Jun 16 10:26:18 2015 from 192.168.32.1\r\r\nroot@ubuntu14:~# '
after: <class 'pexpect.TIMEOUT'>
match: None
match_index: None
exitstatus: None
flag_eof: False
pid: 13015
child_fd: 3
closed: False
timeout: 30
delimiter: <class 'pexpect.EOF'>
logfile: None
logfile_read: None
logfile_send: None
maxread: 2000
ignorecase: False
searchwindowsize: None
delaybeforesend: 0.05
delayafterclose: 0.1
delayafterterminate: 0.1

Process finished with exit code 1

Tags: inpynonechildbinusrlinetimeout
3条回答

问题是当您第一次登录到任何linux框时,它会提示您输入RSA异常。我们需要将密钥永久添加到设备中,以避免出现这种情况。因此,从源设备登录到目标设备并将ssh RSA添加到Linux框。

pexpect未找到所需的字符串(prompt#),因此引发了一个错误。

在stacktrace In中可以看到buffer值:

'cape.canonical.com/\r\n\r\nLast login: Tue Jun 16 10:26:18 2015 from 192.168.32.1\r\r\nroot@ubuntu14:~# '

你应该期待root@ubuntu14:~##,而不是prompt#

您可以debug输出pexpect

下面是您的代码的情况,它可能有助于您理解脚本无法工作的原因:

import pexpect

child = pexpect.spawn('/usr/bin/ssh root@192.168.32.1')

# This line means, "wait until you see a string that matches password:"
# in the response
child.expect('password:', timeout=120) 

child.sendline('pass123') # Send the characters pass123 and "enter"

# Wait till you see a string matching prompt#
child.expect ('prompt# ')

在这一行,您的脚本正在搜索字符串prompt#,但服务器返回的是root@ubuntu14:~#。由于这与您为脚本提供的要检查的内容不匹配,它引发了一个异常,这基本上意味着“我等待字符串与您的模式匹配的超时时间,但没有找到它。”

要解决此问题,可以输入脚本要搜索的确切提示字符串,如下所示:

child.sendline('pass123') # Send the characters pass123 and "enter"

# Wait till you see a string matching ~#
child.expect('~#')
child.sendline ('uname -a')

child.expect ('~#')
print child.before, child.after

或者简单地暂停脚本几秒钟:

import time

child.sendline('pass123') # Send the characters pass123 and "enter"
time.sleep(10)

child.sendline('uname -a')
time.sleep(10)

print child.before, child.after

相关问题 更多 >