在pexpect脚本中运行ssh后期望正确的提示

时间:2014-08-15 02:41:48

标签: pexpect

我的设置是这样的:

Linux服务器(A)--->另一个服务器(B)--->还有另一台服务器(C)

只有A连接到LAN,而B和C位于专用网络上。 B可以通过串口线通过A到达,我在A上运行pexpect脚本连接到B.通过这个脚本,我想运行ssh root @serverC'一些命令&#39 ;

我的代码在serverB上运行命令到ssh到C的相关部分在这里:

BASH_PROMPT = re.compile('root@(localhost|cd6)')

def runExpectCmd(self, cmd, prompt=[BASH_PROMPT, pexpect.EOF], timeout=5, printOut=False):
    log.info('Running cmd %s' % cmd)
    try:
        self.client.sendline(cmd)
        log.info('Waiting %s secs for cmd to complete' % timeout)
        if self.client.expect(prompt, timeout=timeout) != 0:
           log.info('Failed to run %cmd ' % cmd)
           raise
        if printOut:
           log.info(self.client.before)
     except Exception as e:
        print 'Exception was thrown...'

def testServerConnection(self):

    # Execute script on server over ssh
    cmd = 'ssh %s %s "ls -l /"' % (SSH_OPTS, CD_IPADDR)
    self.runExpectCmd(cmd, printOut=True)
    i = self.client.expect([BASH_PROMPT, pexpect.EOF], timeout=10)
    if i != 0:
        log.warning('Failed to run ls on server')
        sys.exit(1)

运行上述内容的输出是

2014-08-14 19:21:21,005 - INFO - Running cmd ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no 2>/dev/null 192.168.27.2 "ls -l /tmp"
ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no 2>/dev/null 192.168.27.2 "ls -l /tmp"
2014-08-14 19:21:21,106 - INFO - Waiting 5 secs for cmd to complete
ev/null 192.168.27.2 "ls -l /tmp"null -o StrictHostKeyChecking=no 2>/d 
total 4
-rwxr-xr-x 1 root root 1863 Aug 14 21:57 provision-storage.sh
ev/null 192.168.27.2 "ls -l /tmp"- INFO - :~# ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no 2>/d 
total 4
-rwxr-xr-x 1 root root 1863 Aug 14 21:57 provision-storage.sh

~# Traceback (most recent call last):
  File "configurecd.py", line 183, in <module>
    exp.testServerConnection()
  File "configurecd.py", line 157, in testServerConnection
    i = self.client.expect([BASH_PROMPT, pexpect.EOF], timeout=10)
  File "/usr/lib/python2.7/dist-packages/pexpect.py", line 1316, in expect
    return self.expect_list(compiled_pattern_list, timeout, searchwindowsize)
  File "/usr/lib/python2.7/dist-packages/pexpect.py", line 1330, in expect_list
    return self.expect_loop(searcher_re(pattern_list), timeout, searchwindowsize)
  File "/usr/lib/python2.7/dist-packages/pexpect.py", line 1414, in expect_loop
    raise TIMEOUT (str(e) + '\n' + str(self))
pexpect.TIMEOUT: Timeout exceeded in read_nonblocking().
<fdpexpect.fdspawn object at 0x7fbc83c705d0>
version: 2.4 ($Revision: 516 $)
command: None
args: None
searcher: searcher_re:
    0: re.compile("root@(localhost|cd6)")
    1: EOF
buffer (last 100 chars): :~# 
before (last 100 chars): :~# 
after: <class 'pexpect.TIMEOUT'>
match: None
match_index: None
exitstatus: None
flag_eof: False
pid: None
child_fd: 3
closed: False
timeout: 30
delimiter: <class 'pexpect.EOF'>
logfile: <open file '<stdout>', mode 'w' at 0x7fbc83ddf150>
logfile_read: None
logfile_send: None
maxread: 2000
ignorecase: False
searchwindowsize: None
delaybeforesend: 0.05
delayafterclose: 0.1
delayafterterminate: 0.1

运行此命令结束时的提示被减少到#〜并且不包含root @(localhost | cd6)。我看到只有在ssh上运行命令时才会发生这种情况。对于在serverB上直接调用的任何其他命令,我可以安全地期望BASH_PROMPT。

有人可以解释上面的代码有什么问题,以及在运行ssh命令时如何正确地期望()正确的提示?

非常感谢

0 个答案:

没有答案