-2
即使登錄成功或登錄未成功,我也無法獲得以下腳本的工作方式。使用Python期望腳本Telnet到多個cisco路由器
可以使用什麼正則表達式來接受下面的所有提示?
router1>; router3>; router4#
如何使用正則表達式,我可以想到的所有文字之前,「失敗」和之後的「失敗」的提示,然後在結束時,我會得到> > >拒絕「權限
Current failed Login: ########
User Access Verification
Username: asdsad
Password:
% Authentication failed
Current Successful Login ###########
[[email protected] 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>
我下面的腳本################
問題有關思科路由器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()