web-dev-qa-db-ja.com

すべてのping結果にタイムスタンプを付けるにはどうすればよいですか?

Ping はデフォルトでこれを返します:

64 bytes from 203.173.50.132: icmp_seq=0 ttl=244 time=57.746 ms

タイムスタンプを追加する方法はありますか?

例えば、

Mon 21 May 2012 15:15:37 EST | 64 bytes from 203.173.50.132: icmp_seq=0 ttl=244 time=57.746 ms

OS X v10.7(Lion)を使用していますが、これには BSD バージョンのpingがあります。

57
John Mee

AWKにstrftime()がない場合:

ping Host | Perl -nle 'print scalar(localtime), " ", $_'

ファイルにリダイレクトするには、標準のシェルリダイレクトを使用して、出力バッファリングをオフにします。

ping Host | Perl -nle 'BEGIN {$|++} print scalar(localtime), " ", $_' > outputfile

タイムスタンプにISO8601形式が必要な場合:

ping Host | Perl -nle 'use Time::Piece; BEGIN {$|++} print localtime->datetime, " ", $_' > outputfile
77

何らかの理由でPerlベースのソリューションをファイルにリダイレクトできなかったので、検索を続けて、これを行うbashのみの方法を見つけました。

ping www.google.fr | while read pong; do echo "$(date): $pong"; done

Wed Jun 26 13:09:23 CEST 2013: PING www.google.fr (173.194.40.56) 56(84) bytes of data.
Wed Jun 26 13:09:23 CEST 2013: 64 bytes from zrh04s05-in-f24.1e100.net (173.194.40.56): icmp_req=1 ttl=57 time=7.26 ms
Wed Jun 26 13:09:24 CEST 2013: 64 bytes from zrh04s05-in-f24.1e100.net (173.194.40.56): icmp_req=2 ttl=57 time=8.14 ms

クレジットは https://askubuntu.com/a/137246

88
richk

man pingから:

   -D     Print timestamp (unix time + microseconds as in gettimeofday) before each line.

次のようなものが生成されます。

[1337577886.346622] 64 bytes from 4.2.2.2: icmp_req=1 ttl=243 time=47.1 ms

次に、ping応答からタイムスタンプを解析し、dateを使用して必要な形式に変換できます。

  1. 端末出力:

    ping -i 5 google.com | xargs -L 1 -I '{}' date '+%Y-%m-%d %H:%M:%S: {}'

  2. ファイル出力:

    ping -i 5 google.com | xargs -L 1 -I '{}' date '+%Y-%m-%d %H:%M:%S: {}' > test.txt

  3. ターミナル+ファイル出力:

    ping -i 5 google.com | xargs -L 1 -I '{}' date '+%Y-%m-%d %H:%M:%S: {}' | tee test.txt

  4. ファイル出力の背景:

    Nohup ping -i 5 google.com | xargs -L 1 -I '{}' date '+%Y-%m-%d %H:%M:%S: {}' > test.txt &

13
xuanyuanaosheng

元の提出物は、各行の日付を評価しなかったため、正しくありませんでした。修正が行われました。

これを試して

 ping google.com | xargs -L 1 -I '{}' date '+%+: {}'

次の出力を生成します

Thu Aug 15 10:13:59 PDT 2013: PING google.com (74.125.239.103): 56 data bytes
Thu Aug 15 10:13:59 PDT 2013: 64 bytes from 74.125.239.103: icmp_seq=0 ttl=55 time=14.983 ms
Thu Aug 15 10:14:00 PDT 2013: 64 bytes from 74.125.239.103: icmp_seq=1 ttl=55 time=17.340 ms
Thu Aug 15 10:14:01 PDT 2013: 64 bytes from 74.125.239.103: icmp_seq=2 ttl=55 time=15.898 ms
Thu Aug 15 10:14:02 PDT 2013: 64 bytes from 74.125.239.103: icmp_seq=3 ttl=55 time=15.720 ms
Thu Aug 15 10:14:03 PDT 2013: 64 bytes from 74.125.239.103: icmp_seq=4 ttl=55 time=16.899 ms
Thu Aug 15 10:14:04 PDT 2013: 64 bytes from 74.125.239.103: icmp_seq=5 ttl=55 time=16.242 ms
Thu Aug 15 10:14:05 PDT 2013: 64 bytes from 74.125.239.103: icmp_seq=6 ttl=55 time=16.574 ms

-L 1オプションにより、xargsは単語ではなく一度に1行を処理します。

8
Bromo

OS Xでは、単に--Apple-timeオプションを使用できます。

ping -i 2 --Apple-time www.Apple.com

次のような結果を生成します。

10:09:55.691216 64 bytes from 72.246.225.209: icmp_seq=0 ttl=60 time=34.388 ms
10:09:57.687282 64 bytes from 72.246.225.209: icmp_seq=1 ttl=60 time=25.319 ms
10:09:59.729998 64 bytes from 72.246.225.209: icmp_seq=2 ttl=60 time=64.097 ms
6
Nicolas Grison

これを試して:

ping www.google.com | while read endlooop; do echo "$(date): $endlooop"; done

次のようなものを返します。

Wednesday 18 January  09:29:20 AEDT 2017: PING www.google.com (216.58.199.36) 56(84) bytes of data.
Wednesday 18 January  09:29:20 AEDT 2017: 64 bytes from syd09s12-in-f36.1e100.net (216.58.199.36): icmp_seq=1 ttl=57 time=2.86 ms
Wednesday 18 January  09:29:21 AEDT 2017: 64 bytes from syd09s12-in-f36.1e100.net (216.58.199.36): icmp_seq=2 ttl=57 time=2.64 ms
Wednesday 18 January  09:29:22 AEDT 2017: 64 bytes from syd09s12-in-f36.1e100.net (216.58.199.36): icmp_seq=3 ttl=57 time=2.76 ms
Wednesday 18 January  09:29:23 AEDT 2017: 64 bytes from syd09s12-in-f36.1e100.net (216.58.199.36): icmp_seq=4 ttl=57 time=1.87 ms
Wednesday 18 January  09:29:24 AEDT 2017: 64 bytes from syd09s12-in-f36.1e100.net (216.58.199.36): icmp_seq=5 ttl=57 time=2.45 ms
4
nutria

結果をawkにパイプします:

 ping Host | awk '{if($0 ~ /bytes from/){print strftime()"|"$0}else print}'
3

Macosでできること

ping --Apple-time 127.0.0.1

出力は次のようになります

16:07:11.315419 64 bytes from 127.0.0.1: icmp_seq=0 ttl=64 time=0.064 ms
16:07:12.319933 64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.157 ms
16:07:13.322766 64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.066 ms
16:07:14.324649 64 bytes from 127.0.0.1: icmp_seq=3 ttl=64 time=0.148 ms
16:07:15.328743 64 bytes from 127.0.0.1: icmp_seq=4 ttl=64 time=0.092 ms
3
Clintm
ping -D -n -O -i1 -W1 8.8.8.8

または多分

while true; do \
    ping -n -w1 -W1 -c1 8.8.8.8 \
    | grep -E "rtt|100%" \
    | sed -e "s/^/`date` /g"; \
    sleep 1; \
done
1
Thomas Szteliga

また、データベースミラーリングのタイムアウトの問題のネットワークの問題を監視するためにも必要です。次のようにコマンドコードを使用します。

ping -t Google.com|cmd /q /v /c "(pause&pause)>nul & for /l %a in () do (set /p "data=" && echo(!date! !time! !data!)&ping -n 2 Google.com>nul" >C:\pingtest.txt

Google.comをサーバー名に変更するだけです。それは私にとって完璧に機能します。終了したらこれを停止することを忘れないでください。 pingtest.txtファイルは、毎秒1 KBずつ増加します(約)。

Raymond.ccをありがとう。 https://www.raymond.cc/blog/timestamp-ping-with-hrping/

1
DBALUKE HUANG

そのような出力が必要になる時間のタイムスタンプまたは間隔を指定しなかったため、無限ループであると考えました。必要に応じて適宜変更できます。

while true
do
   echo -e "`date`|`ping -n -c 1 <IP_TO_PING>|grep 'bytes from'`"
   sleep 2
done
1
Venkat Madhav

~/.bashrcファイルに関数を作成して、コンソールでpingコマンドping-tを取得できます。

function ping-t { ping "$1" | while read pong; do echo "$(date): $pong"; done; }

これで、コンソールでこれを呼び出すことができます。

ping-t example.com

Sa 31.Mär12:58:31 CEST 2018:PING example.com(93.184.216.34)56(84)バイトのデータ。
Sa 31.Mär12:58:31 CEST 2018:93.184.216.34(93.184.216.34)から64バイト:icmp_seq = 1 ttl = 48 time = 208 ms
Sa 31.Mär12:58:32 CEST 2018:93.184.216.34(93.184.216.34)から64バイト:icmp_seq = 2 ttl = 48 time = 233 ms

0
rubo77

この行を試してください。

while sleep 1;do echo "$(date +%d-%m-%y-%T) $(ping -c 1 whatever.com | gawk 'FNR==2{print "Response from:",$4,$8}')" | tee -a /yourfolder/pingtest.log;done

ctrl-c thoでキャンセルする必要があります。

0
nDCasT