即使登录成功或登录失败,我也无法使用以下脚本。
我可以使用哪些正则表达式来接受下面的所有提示?
router1>;
router3>;
router4#
如何使用正则表达式,我可以在“失败”之前和“失败”提示之后看到所有文本,然后在结束时我会得到>>> “许可被拒绝
Current failed Login: ########
User Access Verification
Username: asdsad
Password:
% Authentication failed
Current Successful Login ###########
[root@localhost testuserpass]# telnet 192.168.1.31
Trying 192.168.1.31...
Connected to 192.168.1.31.
Escape character is '^]'.
User Access Verification
Username: admin
Password:
testname>
testname>
testname>
testname>
testname>
我的脚本################
有关Cisco路由器telnet登录正则表达式的问题
#! /usr/bin/python
import pexpect
import getpass
HOST = "192.168.1.31"
user = raw_input("Enter your remote account: ")
password = getpass.getpass()
child = pexpect.spawn ('telnet '+HOST)
child.expect ('Username: ')
child.sendline (user)
child.expect ('Password: ')
child.sendline (password)
//// ######I just can't get below part to work.
i = child.expect (['*Authentication*', 'Terminal type', '[#\$>] '])
if i==0:
print('Permission denied on host. Can\'t login')
child.kill(0)
print child.readline()
elif i==1:
print('Login OK... need to send terminal type.')
child.sendline('vt100')
child.expect('[#\$] ')
print child.readline()
elif i==2:
print('Login OK.')
print('Shell command prompt', child.after)
print child.readline()
答案 0 :(得分:1)
我也在努力处理许多不同的脚本应用程序,但最终找到了可以顺利处理脚本的PrettyGoodTerminal。它专门设计用于编写Cisco路由器的脚本,因此您也可能会发现它很有用