2017-01-30 38 views
-2

即使登錄成功或登錄未成功,我也無法獲得以下腳本的工作方式。使用Python期望腳本Telnet到多個cisco路由器

  1. 可以使用什麼正則表達式來接受下面的所有提示?

    router1>; 
    router3>;  
    router4# 
    
  2. 如何使用正則表達式,我可以想到的所有文字之前,「失敗」和之後的「失敗」的提示,然後在結束時,我會得到> > >拒絕「權限

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() 

回答

1

我也與許多不同的腳本應用程序艱難,但最後發現PrettyGoodTerminal能順利處理腳本。它專門設計用於腳本思科路由器,因此您可能也會覺得它很有用