리눅스
리눅스 ping 명령어 시간 출력
snoopybox
2017. 6. 20. 00:30
리눅스에서 아래와 같이 ping 명령어를 사용하면 timestamp는 출력되지 않는다.
[root@CentOS6 ~]# ping 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=56 time=37.2 ms 64 bytes from 8.8.8.8: icmp_seq=2 ttl=56 time=35.0 ms 64 bytes from 8.8.8.8: icmp_seq=3 ttl=56 time=35.2 ms 64 bytes from 8.8.8.8: icmp_seq=4 ttl=56 time=36.8 ms 64 bytes from 8.8.8.8: icmp_seq=5 ttl=56 time=34.9 ms
일반적으로 ping을 사용하는 경우는 네트워크 연결이 원활하지 않을 때 모니터링 용도로 사용하기 때문에, timestamp 출력은 어떻게 보면 필수라 할 수 있다. 하지만 우리가 원하는 옵션은 ping에 존재하지 않는다. 따라서 아래와 같이 while, read, date 등의 명령어를 조합하여 timestamp를 출력해보자.
[root@CentOS6 ~]# ping 8.8.8.8 | while read line; do echo "$(date +'%F %T.%3N') $line"; done 2017-06-20 00:23:04.389 PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data. 2017-06-20 00:23:04.389 64 bytes from 8.8.8.8: icmp_seq=1 ttl=56 time=34.9 ms 2017-06-20 00:23:05.394 64 bytes from 8.8.8.8: icmp_seq=2 ttl=56 time=38.6 ms 2017-06-20 00:23:06.395 64 bytes from 8.8.8.8: icmp_seq=3 ttl=56 time=37.9 ms 2017-06-20 00:23:07.399 64 bytes from 8.8.8.8: icmp_seq=4 ttl=56 time=39.4 ms 2017-06-20 00:23:08.400 64 bytes from 8.8.8.8: icmp_seq=5 ttl=56 time=39.4 ms
마지막으로... 위와 같이 화면에만 출력하면 정작 문제가 발생했던 시각을 나중에 찾아서 확인하기 힘들다. 따라서 text 파일로 저장하면서 동시에 화면에 출력하는게 좋겠다. tee 명령어를 활용하면 된다.
[root@CentOS6 ~]# ping 8.8.8.8 | while read line; do echo "$(date +'%F %T.%3N') $line"; done | tee ping.txt 2017-06-20 00:23:21.943 PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data. 2017-06-20 00:23:21.944 64 bytes from 8.8.8.8: icmp_seq=1 ttl=56 time=38.5 ms 2017-06-20 00:23:22.942 64 bytes from 8.8.8.8: icmp_seq=2 ttl=56 time=34.9 ms 2017-06-20 00:23:23.944 64 bytes from 8.8.8.8: icmp_seq=3 ttl=56 time=34.7 ms 2017-06-20 00:23:24.946 64 bytes from 8.8.8.8: icmp_seq=4 ttl=56 time=34.8 ms 2017-06-20 00:23:25.948 64 bytes from 8.8.8.8: icmp_seq=5 ttl=56 time=34.8 ms
[root@CentOS6 ~]# cat ping.txt 2017-06-20 00:23:21.943 PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data. 2017-06-20 00:23:21.944 64 bytes from 8.8.8.8: icmp_seq=1 ttl=56 time=38.5 ms 2017-06-20 00:23:22.942 64 bytes from 8.8.8.8: icmp_seq=2 ttl=56 time=34.9 ms 2017-06-20 00:23:23.944 64 bytes from 8.8.8.8: icmp_seq=3 ttl=56 time=34.7 ms 2017-06-20 00:23:24.946 64 bytes from 8.8.8.8: icmp_seq=4 ttl=56 time=34.8 ms 2017-06-20 00:23:25.948 64 bytes from 8.8.8.8: icmp_seq=5 ttl=56 time=34.8 ms