Tcl 当路由器重新加载时,产卵路由器停止,有任何方法可以在路由器重新加载后自动重新产卵
我在文件中运行tcl send expect脚本,并作为./file执行Tcl 当路由器重新加载时,产卵路由器停止,有任何方法可以在路由器重新加载后自动重新产卵,tcl,telnet,expect,Tcl,Telnet,Expect,我在文件中运行tcl send expect脚本,并作为./file执行 #!/usr/bin/expect spawn -noecho telnet 42.0.1.11 set timeout 900 expect "login:" send "admin\r" expect "Password: " send "ram\r" expect "#" for {set i 0} {$i <= 1000000000} {incr i} { some router commands } 在
#!/usr/bin/expect
spawn -noecho telnet 42.0.1.11
set timeout 900
expect "login:"
send "admin\r"
expect "Password: "
send "ram\r"
expect "#"
for {set i 0} {$i <= 1000000000} {incr i} {
some router commands
}
在路由器重新加载之前,这一切正常。当路由器重新加载时,此脚本将停止,因为spawn id未打开。我想恢复脚本。我不知道重新加载需要多少时间,因为大部分时间都在变化。有没有办法自动恢复脚本
谢谢将您的登录过程包装成一个过程,如果您收到EOF,请再次调用它。这里是基本的想法,只需记住,可能还有其他方式让事情出错 编辑:我已经重写了一些调查后的代码。这已经在Linux上进行了测试,Solaris 10工作站模拟路由器重新启动。很抱歉,这里没有Cisco重新启动。正如我所提到的,ping实现因操作系统而异,因此可能需要更改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
我不知道重新加载后路由器正常运行的确切时间,不时会考虑ping管理ip,然后如果ping成功,然后再次登录到路由器。你不必知道确切的时间。你可以从平均数开始。您可以创建另一个进程来执行ping,直到路由器响应或达到最大ping重试限制。ping的具体实现取决于您的操作系统。