我正在尝试使用 pexpect 库连接交换机瞻博网络以获取一些接口信息,这是我的代码:
import pexpect
child = pexpect.spawn('ssh root@10.171.23.246')
child.expect('login as: ')
child.sendline('root')
child.expect('password:')
child.sendline(mypassword)
child.expect('% ')
child.sendline('cli')
child.expect('> ')
child.sendline('show interface')
这是结果:
Traceback (most recent call last):
File "first_test.py", line 4, in <module>
child.expect('login as: ')
File "/usr/lib/python2.7/site-packages/pexpect.py", line 1311, in
expect
return self.expect_list(compiled_pattern_list, timeout,
searchwindowsize)
File "/usr/lib/python2.7/site-packages/pexpect.py", line 1325, in
expect_list
return self.expect_loop(searcher_re(pattern_list), timeout,
searchwindowsize)
File "/usr/lib/python2.7/site-packages/pexpect.py", line 1409, in
expect_loop
raise TIMEOUT (str(e) + '\n' + str(self))
pexpect.TIMEOUT: Timeout exceeded in read_nonblocking().
<pexpect.spawn object at 0x7fb21cfd5a50>
version: 2.3 ($Revision: 399 $)
command: /usr/bin/ssh
args: ['/usr/bin/ssh', 'root@10.171.23.246']
searcher: searcher_re:
0: re.compile("login as: ")
buffer (last 100 chars): root@10.171.23.246's password:
before (last 100 chars): root@10.171.23.246's password:
after: <class 'pexpect.TIMEOUT'>
match: None
match_index: None
exitstatus: None
flag_eof: False
pid: 20230
child_fd: 3
closed: False
看起来,密码没有发送到交换机。如果不清楚,我是一个新的 python 用户对不起。如何正确连接到交换机?