使用 expect 脚本多个 ssh 到 cisco 路由器
multiple ssh to cisco routers using expect script
我不是一个铁杆脚本专家,但是尝试 learn.I 刚开始使用 expect 脚本来在 cisco 上自动执行任务 router.Please 温柔一点,在右边轻推我 direction.I 会之后做相应的研究。
要求:在单个 expect 脚本中为 2 个不同的 cisco 路由器生成 2 个 ssh 会话,并在每个路由器上 运行 独特的命令。
当前方法:我用一个普通的expect脚本来调用这个bash script.I可以用两个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 "#"
我认为您应该使用 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 "#"
我不是一个铁杆脚本专家,但是尝试 learn.I 刚开始使用 expect 脚本来在 cisco 上自动执行任务 router.Please 温柔一点,在右边轻推我 direction.I 会之后做相应的研究。
要求:在单个 expect 脚本中为 2 个不同的 cisco 路由器生成 2 个 ssh 会话,并在每个路由器上 运行 独特的命令。
当前方法:我用一个普通的expect脚本来调用这个bash script.I可以用两个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 "#"
我认为您应该使用 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 "#"