標準入力にタイムスタンプを付けてくれるmoreutils の ts コマンドが便利

moreutils の ts コマンドが便利だったので紹介.

通常のpingはこんな感じ

$ ping ::1
PING ::1(::1) 56 data bytes
64 bytes from ::1: icmp_seq=1 ttl=64 time=0.052 ms
64 bytes from ::1: icmp_seq=2 ttl=64 time=0.051 ms
64 bytes from ::1: icmp_seq=3 ttl=64 time=0.040 ms
64 bytes from ::1: icmp_seq=4 ttl=64 time=0.049 ms
^C
--- ::1 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3067ms
rtt min/avg/max/mdev = 0.040/0.048/0.052/0.004 ms

http(s) の疎通確認の出来る httping には --timestamp/--ts オプションがありタイムスタンプが付けられて便利です.

--timestamp or --ts
       Put a timestamp before the result-lines. Use -v to also show a date.
$ httping 192.168.1.1
PING 192.168.1.1:80 (/):
connected to 192.168.1.1:80 (234 bytes), seq=0 time= 12.53 ms
connected to 192.168.1.1:80 (234 bytes), seq=1 time= 13.34 ms
connected to 192.168.1.1:80 (234 bytes), seq=2 time= 13.16 ms
^CGot signal 2
--- http://192.168.1.1/ ping statistics ---
3 connects, 3 ok, 0.00% failed, time 2578ms
round-trip min/avg/max = 12.5/13.0/13.3 ms
$ httping --timestamp 192.168.1.1
PING 192.168.1.1:80 (/):
22:05:30.798 connected to 192.168.1.1:80 (172 bytes), seq=0 time= 14.99 ms
22:05:31.813 connected to 192.168.1.1:80 (172 bytes), seq=1 time= 14.45 ms
22:05:32.827 connected to 192.168.1.1:80 (172 bytes), seq=2 time= 13.28 ms
^CGot signal 2
--- http://192.168.1.1/ ping statistics ---
3 connects, 3 ok, 0.00% failed, time 2658ms
round-trip min/avg/max = 13.3/14.2/15.0 ms

ping コマンドのman にはそれらしいものは見当たらなかったので,こんな感じで date コマンドの結果を繋いだりしていました.

$ ping ::1 | sed -s "s/^/`date +%F\ %T`\t/"
2023-02-05 21:58:50     PING ::1(::1) 56 data bytes
2023-02-05 21:58:50     64 bytes from ::1: icmp_seq=1 ttl=64 time=0.068 ms
2023-02-05 21:58:50     64 bytes from ::1: icmp_seq=2 ttl=64 time=0.066 ms
2023-02-05 21:58:50     64 bytes from ::1: icmp_seq=3 ttl=64 time=0.042 ms
^C

同じようなことをしてくれる moreutils 内の ts コマンドがありました.

ts: timestamp standard input

$ ping ::1 | ts
Feb 05 22:01:20 PING ::1(::1) 56 data bytes
Feb 05 22:01:20 64 bytes from ::1: icmp_seq=1 ttl=64 time=0.121 ms
Feb 05 22:01:21 64 bytes from ::1: icmp_seq=2 ttl=64 time=0.118 ms
Feb 05 22:01:22 64 bytes from ::1: icmp_seq=3 ttl=64 time=0.115 ms
^C

フォーマット( strftime(3) )を指定して自分好みにも.

$ ping ::1 | ts -m "%F %T"
2023-02-05 22:02:16 PING ::1(::1) 56 data bytes
2023-02-05 22:02:16 64 bytes from ::1: icmp_seq=1 ttl=64 time=0.038 ms
2023-02-05 22:02:17 64 bytes from ::1: icmp_seq=2 ttl=64 time=0.044 ms
2023-02-05 22:02:18 64 bytes from ::1: icmp_seq=3 ttl=64 time=0.066 ms
^C

sed などで書くより良さそうです.(moreutilsの導入が必要だけど)

環境
$ dpkg-query -W iputils-ping moreutils httping
httping 2.5-5.2+b1
iputils-ping    3:20221126-1
moreutils       0.67-1
$ lsb_release -a
No LSB modules are available.
Distributor ID: Debian
Description:    Debian GNU/Linux bookworm/sid
Release:        n/a
Codename:       bookworm
$ arch
x86_64

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です