2017-08-04 49 views
3

我正在嘗試編寫一個小腳本,用於通過ping跟蹤網絡延遲。ping總結沒有在bash腳本中顯示日期

我需要寫入一個文件並用日期和時間標記每個ping條目。我需要實時查看響應,並在ping時間過長時停止腳本。

我可以在一個文件中的Ping測試結果和總結沒有日期,代碼如下

#!/usr/bin/env bash 

echo "Enter Dealer number: " 
read deal 
echo "Enter IP address: " 
read ip 
touch ./${deal}_pingtest.txt 
ping $ip > ./${deal}_pingtest.txt & 
tail -f ./${deal}_pingtest.txt 

標準輸出結果

Enter Dealer number: 
test 
Enter IP address: 
8.8.8.8 
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data. 
64 bytes from 8.8.8.8: icmp_seq=1 ttl=58 time=4.87 ms 
64 bytes from 8.8.8.8: icmp_seq=2 ttl=58 time=5.36 ms 
64 bytes from 8.8.8.8: icmp_seq=3 ttl=58 time=8.30 ms 
64 bytes from 8.8.8.8: icmp_seq=4 ttl=58 time=4.48 ms 
^C 

文件導致

PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data. 
64 bytes from 8.8.8.8: icmp_seq=1 ttl=58 time=4.87 ms 
64 bytes from 8.8.8.8: icmp_seq=2 ttl=58 time=5.36 ms 
64 bytes from 8.8.8.8: icmp_seq=3 ttl=58 time=8.30 ms 
64 bytes from 8.8.8.8: icmp_seq=4 ttl=58 time=4.48 ms 

--- 8.8.8.8 ping statistics --- 
4 packets transmitted, 4 received, 0% packet loss, time 2999ms 
rtt min/avg/max/mdev = 4.488/5.758/8.309/1.506 ms 

當我加入日期到腳本文件結果從不顯示統計 帶時間戳的腳本

#!/usr/bin/env bash 

echo "Enter Dealer number: " 
read deal 
echo "Enter IP address: " 
read ip 
touch ./${deal}_pingtest.txt 
ping $ip | while read pong; do echo "$(date +%Y-%m-%d\|%H:%M:%S): $pong"; done > ./${deal}_pingtest.txt & 
tail -f ./${deal}_pingtest.txt 

標準輸出結果

Enter Dealer number: 
test 
Enter IP address: 
8.8.8.8 
2017-08-04|11:31:29: PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data. 
2017-08-04|11:31:29: 64 bytes from 8.8.8.8: icmp_seq=1 ttl=58 time=4.71 ms 
2017-08-04|11:31:30: 64 bytes from 8.8.8.8: icmp_seq=2 ttl=58 time=4.53 ms 
2017-08-04|11:31:31: 64 bytes from 8.8.8.8: icmp_seq=3 ttl=58 time=4.85 ms 
2017-08-04|11:31:32: 64 bytes from 8.8.8.8: icmp_seq=4 ttl=58 time=5.11 ms 
2017-08-04|11:31:33: 64 bytes from 8.8.8.8: icmp_seq=5 ttl=58 time=4.51 ms 
^C 

文件導致

2017-08-04|11:31:29: PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data. 
2017-08-04|11:31:29: 64 bytes from 8.8.8.8: icmp_seq=1 ttl=58 time=4.71 ms 
2017-08-04|11:31:30: 64 bytes from 8.8.8.8: icmp_seq=2 ttl=58 time=4.53 ms 
2017-08-04|11:31:31: 64 bytes from 8.8.8.8: icmp_seq=3 ttl=58 time=4.85 ms 
2017-08-04|11:31:32: 64 bytes from 8.8.8.8: icmp_seq=4 ttl=58 time=5.11 ms 
2017-08-04|11:31:33: 64 bytes from 8.8.8.8: icmp_seq=5 ttl=58 time=4.51 ms 
2017-08-04|11:31:34: 64 bytes from 8.8.8.8: icmp_seq=6 ttl=58 time=4.89 ms 

謝謝大家的任何指導。

+0

標準輸出和文件數據是相同的,都有日期和時間,所以有什麼問題? –

+0

最後的彙總統計。我們用它們來評估的最終決定。我需要看到數據包丟失和平均時間。 – Ogre55

+0

嘗試做 '&>> ./$ {deal} _pingtest.txt',而不是'> ./$ {deal} _pingtest.txt' – py9

回答

2

我假設你正在使用ctrl-C在鍵盤中斷該腳本。您需要編寫代碼以便ping命令被中斷併發出其摘要信息,但捕獲輸出ping的shell仍然能夠捕獲該摘要並將其發送到輸出文件。

這看起來像trap內置到bash的工作。

調整您的原始腳本:

#!/usr/bin/env bash 

read -p "Enter Dealer number: " deal 
read -p "Enter IP address: " ip 

trap INT 
ping $ip | while read pong; do echo "$(date +%Y-%m-%d\|%H:%M:%S): $pong"; done > "$deal"_pingtest.txt & 
tail -f "$deal"_pingtest.txt 

信號,bash手冊頁的部分:被Bash運行非內置命令已信號處理程序設置爲從shell繼承的值的家長。

上述trap命令意味着運行while循環殼將不會到鍵盤中斷(信號INT)做出響應,但是非內置ping命令將具有其配置重置爲默認值時bash啓動它,所以由鍵盤信號中斷。 ping然後發出其摘要並退出,並且shell繼續捕獲所有輸出。

你也可以構建的東西,這樣你就不能依靠一個後臺進程/ tail組合:

#!/usr/bin/env bash 

read -p 'Enter Dealer number: ' deal 
read -p 'Enter IP address: ' ip 

trap '' INT 

ping "$ip" | 
while read pong; do 
    echo "$(date '+%Y-%m-%d|%H:%M:%S'): $pong" 
done | tee "$deal"_pingtest.txt 

注意在這兩個我用的是read內置及其-p選項提示輸入。

+0

此答案適用於我個人的環境。 – Ogre55

2

ping將只顯示,如果以殺死或SIGINTSIGQUIT統計(或者,如果達成了-c count定義的偵測的數目,但你沒有使用)。從man ping

當指定數量的數據包已發送(和接收)或如果程序以SIGINT終止,則會顯示一個簡短的摘要。無需使用SIGQUIT信號終止過程就可以獲得較短的當前統計數據。

所以,如果你想統計程序,一定要殺死ping這樣的:

pkill ping -SIGINT 
+0

我現在明白爲什麼這樣做會失敗,而且我通常有5到10個實例這個運行,所以這種殺死方法不會爲我工作。 – Ogre55