编写一个脚本生成R1(路由器)并在R1上执行一些命令,然后生成R2并再次在R1上执行一些命令

时间:2014-04-26 07:04:46

标签: switch-statement expect spawn

我是新手,希望编写脚本请在这个问题上有任何帮助。我知道如何连接到路由器1但是如果添加了另一个路由器意味着如何连接到另一个路由器以及如何在它们上执行命令

1 个答案:

答案 0 :(得分:0)

#!/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

请记住为每个sendexpect操作以及-i选项添加spawn进程ID变量。