把你的登錄程序到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