【发布时间】:2014-06-12 00:50:34
【问题描述】:
我是新手,希望编写脚本,请任何人帮助解决这个问题。我知道如何连接到路由器 1,但如果添加了另一个路由器,则意味着如何连接到另一个路由器以及如何在它们上执行命令
【问题讨论】:
标签: switch-statement expect spawn
我是新手,希望编写脚本,请任何人帮助解决这个问题。我知道如何连接到路由器 1,但如果添加了另一个路由器,则意味着如何连接到另一个路由器以及如何在它们上执行命令
【问题讨论】:
标签: switch-statement expect spawn
#!/usr/bin/expect
set user "admin"
set pwd "lab"
set IP1 [lindex $argv 0]
set Cons1 [lindex $argv 1]
set IP2 [lindex $argv 2]
set Cons2 [lindex $argv 3]
#Login to the first router
spawn telnet $IP1 $Cons1
set rtr1 $spawn_id # Saving the spawn id into the variable 'rtr1'
#Login to the first router
spawn telnet $IP2 $Cons2
set rtr2 $spawn_id # Saving the spawn id into the variable 'rtr2'
expect -i $rtr1 "Login:" #This will expect 'Login' for the router 1
expect -i $rtr2 "Login:" #This will expect 'Login' for the router 2
send -i $rtr1 "$user\r" #This will send username to the router 1
send -i $rtr2 "$user\r" #This will send username to the router 2
expect -i $rtr1 "Password"
expect -i $rtr2 "Password"
send -i $rtr1 "$pass\r"
send -i $rtr2 "$pass\r"
expect -i $rtr1 -re ">"
expect -i $rtr2 -re ">"
#Do further operations
请记住将每个send 和expect 操作的生成进程ID 变量以及-i 选项包括在内。
【讨论】: