LoginSignup
10
8

More than 5 years have passed since last update.

PINGコマンドの結果出力。行頭に日時を入れる

Posted at

pingコマンドの頭に実施してる時の日時を出力して表示させるワンライナー。
ping実施後のステータスコードを確認して文字列出力という感じですね。
ここまでくるとワンライナーだと見づらいのでコマンド化したほうがいいかも。

以前google検索で探したらどなたかのブログが出てきたのですが、コマンドは残ってるけどurlがわからないのでここでメモ。
パクリです。すみません。。。

8.8.8.8に1s間隔でping

 host="8.8.8.8" ; while true; do rslt=`ping -w 1 -c 1 $host | grep 'bytes from '`; if [ $? -gt 0 ]; then echo -e "`date +'%Y-%m-%d %H:%M:%S'` $host Request timed out."; else echo "`date +'%Y-%m-%d %H:%M:%S'` $host`echo $rslt | cut -d  ':' -f 2`"; sleep 1; fi; done;

成功出力は以下。

2015-07-31 07:50:31 8.8.8.8 icmp_seq=1 ttl=57 time=2.23 ms
2015-07-31 07:50:32 8.8.8.8 icmp_seq=1 ttl=57 time=2.29 ms
2015-07-31 07:50:33 8.8.8.8 icmp_seq=1 ttl=57 time=2.46 ms
2015-07-31 07:50:34 8.8.8.8 icmp_seq=1 ttl=57 time=2.48 ms
2015-07-31 07:50:35 8.8.8.8 icmp_seq=1 ttl=57 time=2.40 ms
2015-07-31 07:50:36 8.8.8.8 icmp_seq=1 ttl=57 time=2.35 ms
2015-07-31 07:50:37 8.8.8.8 icmp_seq=1 ttl=57 time=2.32 ms
2015-07-31 07:50:38 8.8.8.8 icmp_seq=1 ttl=57 time=2.27 ms
2015-07-31 07:50:39 8.8.8.8 icmp_seq=1 ttl=57 time=2.32 ms

失敗するとこうなる

2015-07-31 07:54:51 2.8.8.8 Request timed out.
2015-07-31 07:54:52 2.8.8.8 Request timed out.
2015-07-31 07:54:53 2.8.8.8 Request timed out.

障害テスト実施の時に、日時もわかるといいなということで。
ログに出すもよし、結果の記録の時にも時間の整合性が取れていいのかなと。

10
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
8