2016-03-03 79 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

如果那样做的话那么真棒......我会给它一个镜头......谢谢.. –