2016-03-03 81 views
0

我不是一個硬核腳本的人,但試圖學習。我剛開始期望腳本來自動化思科路由器上的任務。請溫柔,並促使我在正確的方向。我會然後再做相應的研究。多個ssh到思科路由器使用期望腳本

要求:爲2個不同的思科路由器產生2個ssh會話,並在每個思科路由器上運行獨特的命令。

當前方法:我使用常規bash腳本調用此expect腳本。我可以使用兩個expect腳本來實現這個需求,但是我希望使用一個expect腳本來執行此操作。

示例: #設置變量 組路由器1 [LINDEX $ argv的0] 組路由器2 [LINDEX $的argv 1] 組的用戶名[LINDEX $的argv 2] 組密碼[LINDEX $的argv 3]

spawn ssh -o StrictHostKeyChecking=no $username\@$router1 

expect "*assword" 
send "$enablepassword\n" 
expect "#" 
send "command on router1" 
expect "#" 

close 

#i want to close this ssh session and spawn ssh process to router2 


spawn ssh -o StrictHostKeyChecking=no $username\@$router2 
#i tried this  simply in the same script and it doesn't work,mostly  because #it is not correct 

expect "*assword" 
send "$enablepassword\n" 
expect "#" 
send "command on router2" 
expect "#" 
+0

您是否需要同時與兩臺路由器進行交互?或者,按順序進行交互就足夠了? – Dinesh

+0

按順序與它們互動足夠好 –

回答

1

我認爲你應該使用spawn_id全局變量,它有助於與多個ssh或telnet會話進行交互。 您的代碼應該如下所示:

spawn ssh -o StrictHostKeyChecking=no $username\@$router1 
set R1 $spawn_id 
expect -i $R1 "*assword" 
send -i $R1 "$enablepassword\n" 
expect -i $R1 "#" 
send -i $R1 "command on router1" 
expect -i $R1 "#" 
send -i $R11 "exit\r" 


spawn ssh -o StrictHostKeyChecking=no $username\@$router2 
set R2 $spawn_id 

expect -i $R2 "*assword" 
send -i $R2 "$enablepassword\n" 
expect -i $R2 "#" 
send -i $R2 "command on router2" 
expect "#" 
+0

如果那樣做的話那麼真棒......我會給它一個鏡頭......謝謝.. –