2013-01-20 70 views
1

我運行TCL預計發送腳本文件,並執行作爲./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 
} 

這個作品很好,直到路由器重新加載,當路由器重新加載時,這個腳本停止,因爲spawn id不打開,我想恢復腳本(我不知道到底需要多少時間來重新加載,因爲它大部分時間都在變化)。自動恢復腳本

謝謝

回答

0

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

我不知道重裝後路由器正常運行的確切時間,(不同時間到時間)查驗管理IP的思想,然後如果平了成功,然後登錄到路由器再次, –

+0

你不必知道確切的時間。你可以從平均水平開始。你可以創建另一個proc,執行'ping'直到路由器響應,或者直到達到最大ping重試限制。 ping的確切實現取決於您的操作系統。 – pn8830

+0

此更新的代碼應該可以工作。 – pn8830

相關問題