我正在嘗試編寫一個小腳本,用於通過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
謝謝大家的任何指導。
標準輸出和文件數據是相同的,都有日期和時間,所以有什麼問題? –
最後的彙總統計。我們用它們來評估的最終決定。我需要看到數據包丟失和平均時間。 – Ogre55
嘗試做 '&>> ./$ {deal} _pingtest.txt',而不是'> ./$ {deal} _pingtest.txt' – py9