把你的登录程序到proc中,如果你得到EOF,再次调用它。这里有一个基本的想法,只记得可能有其他方式出错。
编辑:我已经重新编写了一些调查后的代码。这已经在Linux上通过Solaris 10工作站模拟的“路由器重启”进行了测试(对不起,没有思科重启)。正如我所提到的,ping
实现不同于OS到OS,因此可能需要更改proc Ping
以适应您的情况。
#!/usr/bin/tclsh
package require Expect
proc RouterLogin {ip user pass} {
spawn -noecho telnet $ip
set timeout 60
expect "login:"
send "$user\r"
expect "Password: "
send "$pass\r"
expect "#"
return $spawn_id
}
proc RouterPing ip {
# ping retry limit - 3
# delay between retries - 30 seconds
set limit 3
set delay 30
set attempt 1
set result false
while {$result == false && $attempt <= $limit} {
set result [Ping $ip]
incr attempt
if {!$result && $attempt <= $limit} {
# wait $delay seconds
after [expr {1000 * $delay}]
}
}
return $result
}
proc Ping ip {
set pingCmd "ping -c 1 $ip"
catch {eval exec $pingCmd} pingRes
if {[regexp "bytes from" $pingRes]} {
return true
} else {
return false
}
}
proc RouterExec {ip user pass commandList} {
set spawn_id [RouterLogin $ip $user $pass]
set timeout 30
foreach cmd $commandList {
send "$cmd\r"
expect {
"#" {
# you are good
puts "Command executed successfully"
}
eof {
# wait 5 minutes
after [expr {1000 * 60 * 5}]
if {[RouterPing $ip]} {
# ping was successful, relogin and resume
set spawn_id [RouterLogin $ip $user $pass]
} else {
# ping was not successful, abort execution
return false
}
}
timeout {
puts "INF: timeout"
return false
}
}
}
send "logout\r"
return true
}
set commandList [list command1 command2 command3]
RouterExec "42.0.1.11" "admin" "ram" $commandList
我不知道重装后路由器正常运行的确切时间,(不同时间到时间)查验管理IP的思想,然后如果平了成功,然后登录到路由器再次, –
你不必知道确切的时间。你可以从平均水平开始。你可以创建另一个proc,执行'ping'直到路由器响应,或者直到达到最大ping重试限制。 ping的确切实现取决于您的操作系统。 – pn8830
此更新的代码应该可以工作。 – pn8830