2013-01-21 44 views
1

我試圖在使用expect登錄到遠程交換機後ping 4個不同的IP地址,登錄部分工作正常,並且第2個ping命令(發送);但它會跳過第3個ping命令並轉到第4個ping。在顯示ping摘要之前的大部分時間都會退出腳本。預計ping腳本跳過發送步驟

我的腳本:

#!/usr/bin/expect 
spawn telnet 192.168.xx.xx 
expect "Username:" 
send "username\r" 
expect "Password:" 
send "password\r" 
expect ">" 
send "ping -c 20 192.168.1.10\r" 
expect ">" 
send "ping -c 20 192.168.10.22\r" 
expect ">" 
send "ping -c 20 192.168.10.33\r" 
expect ">" 
send "ping -c 20 192.168.11.10\r" 
expect ">" 
send "quit\r" # command to exit from switch 
expect eof 

我試圖發送命令將「睡眠4」每過,但結果是一樣的。

輸出:

<switch>ping -c 20 192.168.1.20 
    PING 192.168.1.20: 56 data bytes, press CTRL_C to break 
    Reply from 192.168.1.20: bytes=56 Sequence=1 ttl=255 time=1 ms 
    Reply from 192.168.1.20: bytes=56 Sequence=2 ttl=255 time=1 ms 
    Reply from 192.168.1.20: bytes=56 Sequence=3 ttl=255 time=101 ms 
    Reply from 192.168.1.20: bytes=56 Sequence=4 ttl=255 time=2 ms 
    Reply from 192.168.1.20: bytes=56 Sequence=5 ttl=255 time=2 ms 
    Reply from 192.168.1.20: bytes=56 Sequence=6 ttl=255 time=2 ms 
    Reply from 192.168.1.20: bytes=56 Sequence=7 ttl=255 time=102 ms 
    Reply from 192.168.1.20: bytes=56 Sequence=8 ttl=255 time=2 ms 
    Reply from 192.168.1.20: bytes=56 Sequence=9 ttl=255 time=3 ms 
    Reply from 192.168.1.20: bytes=56 Sequence=10 ttl=255 time=1 ms 
    Reply from 192.168.1.20: bytes=56 Sequence=11 ttl=255 time=108 ms 
    Reply from 192.168.1.20: bytes=56 Sequence=12 ttl=255 time=1 ms 
    Reply from 192.168.1.20: bytes=56 Sequence=13 ttl=255 time=1 ms 
    Reply from 192.168.1.20: bytes=56 Sequence=14 ttl=255 time=2 ms 
    Reply from 192.168.1.20: bytes=56 Sequence=15 ttl=255 time=130 ms 
    Reply from 192.168.1.20: bytes=56 Sequence=16 ttl=255 time=1 ms 
    Reply from 192.168.1.20: bytes=56 Sequence=17 ttl=255 time=2 ms 
    Reply from 192.168.1.20: bytes=56 Sequence=18 ttl=255 time=1 ms 
    Reply from 192.168.1.20: bytes=56 Sequence=19 ttl=255 time=160 ms 
    Reply from 192.168.1.20: bytes=56 Sequence=20 ttl=255 time=1 ms 

    --- 192.168.1.20 ping statistics --- 
    20 packet(s) transmitted 
    20 packet(s) received 
    0.00% packet loss 
    round-trip min/avg/max = 1/31/160 ms 

<switch>ping -c 20 192.168.10.22 
    PING 192.168.10.22: 56 data bytes, press CTRL_C to break 
    Reply from 192.168.10.22: bytes=56 Sequence=1 ttl=255 time=1 ms 
    Reply from 192.168.10.22: bytes=56 Sequence=2 ttl=255 time=12 ms 
    Reply from 192.168.10.22: bytes=56 Sequence=3 ttl=255 time=1 ms 
    Reply from 192.168.10.22: bytes=56 Sequence=4 ttl=255 time=35 ms 
    Reply from 192.168.10.22: bytes=56 Sequence=5 ttl=255 time=2 ms 
    Reply from 192.168.10.22: bytes=56 Sequence=6 ttl=255 time=16 ms 
    Reply from 192.168.10.22: bytes=56 Sequence=7 ttl=255 time=2 ms 
    Reply from 192.168.10.22: bytes=56 Sequence=8 ttl=255 time=29 ms 
    Reply from 192.168.10.22: bytes=56 Sequence=9 ttl=255 time=4 ms 
    Reply from 192.168.10.22: bytes=56 Sequence=10 ttl=255 time=39 ms 
    Reply from 192.168.10.22: bytes=56 Sequence=11 ttl=255 time=1 ms 
    Reply from 192.168.10.22: bytes=56 Sequence=12 ttl=255 time=57 ms 
    Reply from 192.168.10.22: bytes=56 Sequence=13 ttl=255 time=1 ms 
    Reply from 192.168.10.22: bytes=56 Sequence=14 ttl=255 time=1 ms 
    Reply from 192.168.10.22: bytes=56 Sequence=15 ttl=255 time=1 ms 
    Reply from 192.168.10.22: bytes=56 Sequence=16 ttl=255 time=1 ms 
    Reply from 192.168.10.22: bytes=56 Sequence=17 ttl=255 time=1 ms 
    Reply from 192.168.10.22: bytes=56 Sequence=18 ttl=255 time=2 ms 
    Reply from 192.168.10.22: bytes=56 Sequence=19 ttl=255 time=1 ms 
    Reply from 192.168.10.22: bytes=56 Sequence=20 ttl=255 time=2 ms 

    --- 192.168.10.22 ping statistics --- 
    20 packet(s) transmitted 
    20 packet(s) received 
    0.00% packet loss 
    round-trip min/avg/max = 1/10/57 ms 

<switch>ping -c 20 192.168.11.10 
    PING 192.168.11.10: 56 data bytes, press CTRL_C to break 
    Reply from 192.168.11.10: bytes=56 Sequence=1 ttl=255 time=4 ms 
    Reply from 192.168.11.10: bytes=56 Sequence=2 ttl=255 time=1 ms 
    Reply from 192.168.11.10: bytes=56 Sequence=3 ttl=255 time=44 ms 
    Reply from 192.168.11.10: bytes=56 Sequence=4 ttl=255 time=1 ms 
    Reply from 192.168.11.10: bytes=56 Sequence=5 ttl=255 time=57 ms 
    Reply from 192.168.11.10: bytes=56 Sequence=6 ttl=255 time=3 ms 
    Reply from 192.168.11.10: bytes=56 Sequence=7 ttl=255 time=4 ms 
    Reply from 192.168.11.10: bytes=56 Sequence=8 ttl=255 time=4 ms 
    Reply from 192.168.11.10: bytes=56 Sequence=9 ttl=255 time=3 ms 
    Reply from 192.168.11.10: bytes=56 Sequence=10 ttl=255 time=3 ms 
    Reply from 192.168.11.10: bytes=56 Sequence=11 ttl=255 time=3 ms 
    Reply from 192.168.11.10: bytes=56 Sequence=12 ttl=255 time=2 ms 
    Reply from 192.168.11.10: bytes=56 Sequence=13 ttl=255 time=2 ms 
    Reply from 192.168.11.10: bytes=56 Sequence=14 ttl=255 time=1 ms 
    Reply from 192.168.11.10: bytes=56 Sequence=15 ttl=255 time=1 ms 
    Reply from 192.168.11.10: bytes=56 Sequence=16 ttl=255 time=1 ms 
    Reply from 192.168.11.10: bytes=56 Sequence=17 ttl=255 time=1 ms 
    Reply from 192.168.11.10: bytes=56 Sequence=18 ttl=255 time=1 ms 
    Reply from 192.168.11.10: bytes=56 Sequence=19 ttl=255 time=1 ms 
    Reply from 192.168.11.10: bytes=56 Sequence=20 ttl=255 time=1 ms 
    [[email protected] script]$ 

正如你所看到的,它跳過第3平(多次檢查)和4平夏日之前退出。任何人都可以請幫助解決此問題?提前致謝。

回答

1

您可能會遇到超時問題。 Expect的默認超時時間爲10秒。 20坪將需要超過約。

在腳本的開頭使用下面的命令:

set timeout 60

將其設置爲1分鐘。

+0

非常感謝!超時修復它。解決這個問題讓我非常沮喪。乾杯..! – Rousseau

+0

不用客氣 @glenn jackman:感謝您修復這些值。 – pn8830

0

我不知道它是否超時。你可以嘗試像

send "password\r" 
expect ">" 
foreach ip {192.168.1.10 192.168.10.22 192.168.10.33 192.168.11.10} { 
    send "ping -c 20 $ip\r" 
    expect { 
     timeout {puts "Timed out waiting for response from $ip"} 
     ">" 
    } 
} 
send "quit\r" 

您可以設置timeout值,如果你發現你得到超時,例如set timeout 20實驗。

另外,我發現這個命令對開發預期腳本非常有用:exp_internal 1 - 將它添加到腳本的頂部。

+0

謝謝你..你的「foreach」循環是一個偉大的提示:) – Rousseau