いろいろ備忘録日記

主に .NET とか Go とか Flutter とか Python絡みのメモを公開しています。

pingしたときにタイムスタンプをつけて出力したい (ping, moreutils, ts)

概要

以下、個人用メモです。忘れないうちにメモメモ。。

ping コマンドには -D というオプションがあって、これを指定するとタイムスタンプを出力してくれます。

ただ、この出力内容がヘルプ見ると以下のようになっています。

以下、ping(8) の man より抜粋

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

なので、以下のような出力となります。

$ ping -c 3 -D google.co.jp
PING google.co.jp (216.58.197.227) 56(84) bytes of data.
[1621168200.486448] 64 bytes from nrt13s49-in-f227.1e100.net (216.58.197.227): icmp_seq=1 ttl=113 time=41.1 ms
[1621168201.502384] 64 bytes from nrt13s49-in-f227.1e100.net (216.58.197.227): icmp_seq=2 ttl=113 time=54.7 ms
[1621168202.940270] 64 bytes from nrt13s49-in-f227.1e100.net (216.58.197.227): icmp_seq=3 ttl=113 time=489 ms

--- google.co.jp ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2004ms
rtt min/avg/max/mdev = 41.084/195.071/489.418/208.208 ms

うーん、後から見直したときにパッと分からないですね・・w

ということで、これを見やすい方法に変えるやり方です。

moreutils パッケージの中の ts コマンドを使う

moreutils というのは、以下のようなパッケージです。 

$ apt info moreutils
Package: moreutils
Version: 0.65-1
Priority: optional
Section: utils
Maintainer: Nicolas Schier <nicolas@fjasle.eu>
Installed-Size: 214 kB
Depends: libc6 (>= 2.17), perl:any, libipc-run-perl, libtime-duration-perl, libtimedate-perl
Conflicts: lckdo
Replaces: lckdo
Homepage: https://joeyh.name/code/moreutils/
Tag: devel::lang:perl, devel::library, implemented-in::c,
 implemented-in::perl, interface::commandline, role::devel-lib,
 role::program, scope::utility, works-with::text
Download-Size: 74.4 kB
APT-Manual-Installed: yes
APT-Sources: https://deb.debian.org/debian sid/main arm64 Packages
Description: additional Unix utilities
 This is a growing collection of the Unix tools that nobody thought
 to write long ago, when Unix was young.
 .
 So far, it includes the following utilities:
  - chronic: runs a command quietly unless it fails
  - combine: combine the lines in two files using boolean operations
  - errno: look up errno names and descriptions
  - ifdata: get network interface info without parsing ifconfig output
  - ifne: run a program if the standard input is not empty
  - isutf8: check if a file or standard input is utf-8
  - lckdo: execute a program with a lock held
  - mispipe: pipe two commands, returning the exit status of the first
  - parallel: run multiple jobs at once
  - pee: tee standard input to pipes
  - sponge: soak up standard input and write to a file
  - ts: timestamp standard input
  - vidir: edit a directory in your text editor
  - vipe: insert a text editor into a pipe
  - zrun: automatically uncompress arguments to command

標準じゃないけど、ちょっと便利なスクリプト集みたいな感じです。

この中に ts コマンドがあって、以下のようなコマンドです。

  - ts: timestamp standard input

要は、入力に対して、タイムスタンプを付与して出力するコマンドです。

こんな感じ。

$ echo 'helloworld' | ts
May 16 21:40:44 helloworld

インストールは、普通に apt で。

$ sudo apt install moreutils
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
  libio-pty-perl libipc-run-perl libtime-duration-perl
The following NEW packages will be installed:
  libio-pty-perl libipc-run-perl libtime-duration-perl moreutils
0 upgraded, 4 newly installed, 0 to remove and 0 not upgraded.
Need to get 226 kB of archives.
After this operation, 624 kB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 https://deb.debian.org/debian sid/main arm64 libio-pty-perl arm64 1:1.15-2 [36.0 kB]
Get:2 https://deb.debian.org/debian sid/main arm64 libipc-run-perl all 20200505.0-1 [102 kB]
Get:3 https://deb.debian.org/debian sid/main arm64 libtime-duration-perl all 1.21-1 [13.7 kB]
Get:4 https://deb.debian.org/debian sid/main arm64 moreutils arm64 0.65-1 [74.4 kB]
Fetched 226 kB in 3s (90.0 kB/s)     
debconf: delaying package configuration, since apt-utils is not installed
Selecting previously unselected package libio-pty-perl.
(Reading database ... 52306 files and directories currently installed.)
Preparing to unpack .../libio-pty-perl_1%3a1.15-2_arm64.deb ...
Unpacking libio-pty-perl (1:1.15-2) ...
Selecting previously unselected package libipc-run-perl.
Preparing to unpack .../libipc-run-perl_20200505.0-1_all.deb ...
Unpacking libipc-run-perl (20200505.0-1) ...
Selecting previously unselected package libtime-duration-perl.
Preparing to unpack .../libtime-duration-perl_1.21-1_all.deb ...
Unpacking libtime-duration-perl (1.21-1) ...
Selecting previously unselected package moreutils.
Preparing to unpack .../moreutils_0.65-1_arm64.deb ...
Unpacking moreutils (0.65-1) ...
Setting up libio-pty-perl (1:1.15-2) ...
Setting up libipc-run-perl (20200505.0-1) ...
Setting up libtime-duration-perl (1.21-1) ...
Setting up moreutils (0.65-1) ...
Processing triggers for man-db (2.9.4-2) ...

では、今度は、さっきのpingの結果を ts コマンドにパイプでつなげます。タイプスタンプの出力は ts コマンドにしてもらうので、-D オプションはつけずに実行。

$ ping -c 3 google.co.jp | ts
May 16 21:38:30 PING google.co.jp (216.58.197.227) 56(84) bytes of data.
May 16 21:38:30 64 bytes from nrt13s49-in-f227.1e100.net (216.58.197.227): icmp_seq=1 ttl=113 time=273 ms
May 16 21:38:31 64 bytes from nrt13s49-in-f227.1e100.net (216.58.197.227): icmp_seq=2 ttl=113 time=56.9 ms
May 16 21:38:32 64 bytes from nrt13s49-in-f227.1e100.net (216.58.197.227): icmp_seq=3 ttl=113 time=421 ms
May 16 21:38:32 
May 16 21:38:32 --- google.co.jp ping statistics ---
May 16 21:38:32 3 packets transmitted, 3 received, 0% packet loss, time 2003ms
May 16 21:38:32 rtt min/avg/max/mdev = 56.921/250.500/421.188/149.589 ms

ちゃんとタイムスタンプ付いてくれていますが、日本人には馴染みづらい形式ですので、よく見る形式にします。

$ ping -c 3 google.co.jp | ts '%Y-%m-%d %H:%M:%S'
2021-05-16 21:42:11 PING google.co.jp (216.58.197.227) 56(84) bytes of data.
2021-05-16 21:42:11 64 bytes from nrt13s49-in-f227.1e100.net (216.58.197.227): icmp_seq=1 ttl=113 time=380 ms
2021-05-16 21:42:12 64 bytes from nrt13s49-in-f227.1e100.net (216.58.197.227): icmp_seq=2 ttl=113 time=42.6 ms
2021-05-16 21:42:13 64 bytes from nrt13s49-in-f227.1e100.net (216.58.197.227): icmp_seq=3 ttl=113 time=49.5 ms
2021-05-16 21:42:13 
2021-05-16 21:42:13 --- google.co.jp ping statistics ---
2021-05-16 21:42:13 3 packets transmitted, 3 received, 0% packet loss, time 2004ms
2021-05-16 21:42:13 rtt min/avg/max/mdev = 42.579/157.492/380.436/157.669 ms

moreutilsのコマンドさん達は、どれも小技が効いていて結構便利ですよ。

vidir とか sponge とか vipe とかは、個人的によく使います。

参考情報

ostechnix.com

qiita.com

moreutilsの各コマンドについて分かりやすい説明をしてくださっている記事です。

superuser.com


過去の記事については、以下のページからご参照下さい。

  • いろいろ備忘録日記まとめ

devlights.github.io

サンプルコードは、以下の場所で公開しています。

  • いろいろ備忘録日記サンプルソース置き場

github.com

github.com

github.com