Ping を実行した際、Windows のように
「Request Timed out」を表示させる方法について書いてあるBlogがあった。
複製禁止でないことを祈りつつ貼り付ける。
host=*.*.*.*; max_cnt=10; cnt=1; while [ $cnt -le $max_cnt ]; do rslt=`ping -w 1 -c 1 $host | grep 'bytes from '`; if [ $? -gt 0 ]; then echo -e "`date +'%H:%M:%S'` $host Request timed out."; else echo "`date +'%H:%M:%S'` $host`echo $rslt | cut -d ':' -f 2`"; sleep 1; fi; cnt=$(expr $cnt + 1); done;
max_cntは終了するカウント数
cntは開始するカウント数
RedHat系OSのBashで実際に打ってみた。
[root@~]# host=172.16.254.254; max_cnt=10; cnt=1; while [ $cnt -le $max_cnt ]; do rslt=`ping -w 1 -c 1 $host | grep 'bytes from '`; if [ $? -gt 0 ]; then echo -e "`date +'%H:%M:%S'` $host Request timed out."; else echo "`date +'%H:%M:%S'` $host`echo $rslt | cut -d ':' -f 2`"; sleep 1; fi; cnt=$(expr $cnt + 1); done; 15:44:02 172.16.254.254 icmp_seq=1 ttl=128 time=0.162 ms 15:44:03 172.16.254.254 icmp_seq=1 ttl=128 time=0.181 ms 15:44:04 172.16.254.254 icmp_seq=1 ttl=128 time=0.183 ms 15:44:05 172.16.254.254 icmp_seq=1 ttl=128 time=0.177 ms 15:44:06 172.16.254.254 icmp_seq=1 ttl=128 time=0.182 ms 15:44:07 172.16.254.254 icmp_seq=1 ttl=128 time=0.187 ms 15:44:08 172.16.254.254 icmp_seq=1 ttl=128 time=3.87 ms 15:44:09 172.16.254.254 icmp_seq=1 ttl=128 time=0.190 ms 15:44:10 172.16.254.254 icmp_seq=1 ttl=128 time=0.187 ms 15:44:11 172.16.254.254 icmp_seq=1 ttl=128 time=0.178 ms [root@~]#
[root@~]# host=17.1.1.9; max_cnt=10; cnt=1; while [ $cnt -le $max_cnt ]; do rslt=`ping -w 1 -c 1 $host | grep 'bytes from '`; if [ $? -gt 0 ]; then echo -e "`date +'%H:%M:%S'` $host Request timed out."; else echo "`date +'%H:%M:%S'` $host`echo $rslt | cut -d ':' -f 2`"; sleep 1; fi; cnt=$(expr $cnt + 1); done; 15:44:17 17.1.1.9 Request timed out. 15:44:18 17.1.1.9 Request timed out. 15:44:19 17.1.1.9 Request timed out. 15:44:20 17.1.1.9 Request timed out. 15:44:21 17.1.1.9 Request timed out. 15:44:22 17.1.1.9 Request timed out. 15:44:23 17.1.1.9 Request timed out. 15:44:24 17.1.1.9 Request timed out. 15:44:25 17.1.1.9 Request timed out. 15:44:26 17.1.1.9 Request timed out. [root@~]#