2012-07-18 77 views
0

任何幫助,將不勝感激。我需要將繼承的期望腳本從Telnet轉換爲僅SSH登錄。首先,我是一個繼承我們所有期望腳本模板的路由器人員,寫了一段時間。到目前爲止,幾乎沒有修改,他們已經能夠順利運行。我們的客戶希望擺脫Telnet,因此幾個月前我們準備好了所有的Cisco路由器和交換機,以支持Telnet和SSH。到現在爲止,我們的腳本一直很好。但是,Telnet支持和服務器很快就會消失,我需要弄清楚如何重新配置​​所有腳本模板以在僅限SSH的環境中工作。將Expect腳本從Telnet切換到SSH只有

所以,這裏有一個簡單的模板的例子得到SH版本輸出:

#!/usr/local/bin/expect -f 
# 
set force_conservative 0 ;# set to 1 to force conservative mode even if 
          ;# script wasn't run conservatively originally 
if {$force_conservative} { 
     set send_slow {1 .1} 
     proc send {ignore arg} { 
       sleep .1 
       exp_send -s -- $arg 
     } 
} 

#################################################################### 
# Info for command line arguments 

set argv [ concat "script" $argv ] 

set router [ lindex $argv 1 ] 


#################################################################### 
set timeout 15 
set send_slow {1 .05} 
spawn telnet $router 
match_max 100000 
expect Username: 
sleep .1 
send -s -- "user\r" 
expect Password: 
sleep .1 
send -s -- "pass\r" 
expect * 
send -s -- "\r" 
expect * 
sleep .2 
send -s -- "sh ver\r" 
expect * 
sleep .2 
send -s -- "end\r" 
expect * 
sleep .2 
send -s -- "wr\r" 
expect * 
sleep .2 
send -s -- "exit\r" 
expect * 
sleep .2 

expect eof 

回答

0

相反的:

spawn telnet $router 
match_max 100000 
expect Username: 
sleep .1 
send -s -- "user\r" 
expect Password: 
sleep .1 
send -s -- "pass\r" 

嘗試使用:

spawn ssh -M [email protected]$router 
while 1 { 
     expect { 
       "no)?"   {send "yes\r"} 
       "sername:"  {send "username\r"} 
       "assword:"  {send "password\r"} 
       ">"    {break} 
       "denied"  {send_user "Can't login\r"; exit 1} 
       "refused"  {send_user "Connection refused\r"; exit 2} 
       "failed"  {send_user "Host exists. Check ssh_hosts file\r"; exit 3} 
       timeout   {send_user "Timeout problem\r"; exit 4} 
       } 
     }