ping – matoken's blog https://matoken.org/blog Is there no plan B? Sun, 13 Oct 2024 21:11:03 +0000 ja hourly 1 https://wordpress.org/?v=6.9.1 https://matoken.org/blog/wp-content/uploads/2025/03/cropped-1865f695c4eecc844385acef2f078255036adccd42c254580ea3844543ab56d9-32x32.jpeg ping – matoken's blog https://matoken.org/blog 32 32 TCP portに対して疎通確認をするtcping https://matoken.org/blog/2024/10/14/tcping-to-check-communication-with-tcp-port/ https://matoken.org/blog/2024/10/14/tcping-to-check-communication-with-tcp-port/#respond Sun, 13 Oct 2024 21:10:56 +0000 https://matoken.org/blog/?p=4131

tcping logo3
tcping という TCP port で ping のような動作をするツールを知ったので試してみました.

導入はreleasesページから環境に合ったバイナリをダウンロード,docker,go install,brew,source からmake が紹介されています.今回は go install を利用しました.

go install で導入
$ go install github.com/pouriyajamshidi/tcping@latest
$ which tcping
/home/matoken/go/bin/tcping
$ ls -l `!!`
ls -l `which tcping `
-rwxrwxr-x 1 matoken matoken 8897290 Oct 13 06:13 /home/matoken/go/bin/tcping
help
$ tcping --help

TCPING version 1.21.2

Try running tcping like:
tcping <hostname/ip> <port number>. For example:
tcping www.example.com 443

[optional flags]
  -4 : use IPv4 only.
  -6 : use IPv6 only.
  -j : output in JSON format.
  --pretty : use indentation when using json output format. No effect without the -j flag.
  -r : retry resolving target's hostname after <n> number of failed requests. e.g. -r 10 for 10 failed probes.
  -u : check for updates.
  -v : show version.
httpd に対して tcping
$ python -m http.server -b 127.0.0.1 -d `mktemp -d` 8000 &
[1] 3553818
Serving HTTP on 0.0.0.0 port 8000 (http://0.0.0.0:8000/) ...

$ tcping localhost 8000
TCPinging localhost on port 8000
Reply from localhost (127.0.0.1) on port 8000 TCP_conn=1 time=0.136 ms
Reply from localhost (127.0.0.1) on port 8000 TCP_conn=2 time=0.148 ms
Reply from localhost (127.0.0.1) on port 8000 TCP_conn=3 time=0.224 ms
Reply from localhost (127.0.0.1) on port 8000 TCP_conn=4 time=0.851 ms
^C
--- localhost (127.0.0.1) TCPing statistics ---
4 probes transmitted on port 8000 | 4 received, 0.00% packet loss
successful probes:   4
unsuccessful probes: 0
last successful probe:   2024-10-14 04:45:57
last unsuccessful probe: Never failed
total uptime:   4 seconds
total downtime: 0 second
longest consecutive uptime:   4 seconds from 2024-10-14 04:45:54 to 2024-10-14 04:45:57
retried to resolve hostname 0 times
rtt min/avg/max: 0.136/0.340/0.851 ms
--------------------------------------
TCPing started at: 2024-10-14 04:45:54
TCPing ended at:   2024-10-14 04:45:57
duration (HH:MM:SS): 00:00:03

$ kill %1
sshd に対して tcping
$ tcping localhost 22
TCPinging localhost on port 22
Reply from localhost (::1) on port 22 TCP_conn=1 time=0.250 ms
Reply from localhost (::1) on port 22 TCP_conn=2 time=0.146 ms
Reply from localhost (::1) on port 22 TCP_conn=3 time=0.116 ms
Reply from localhost (::1) on port 22 TCP_conn=4 time=0.125 ms
^C
--- localhost (::1) TCPing statistics ---
4 probes transmitted on port 22 | 4 received, 0.00% packet loss
successful probes:   4
unsuccessful probes: 0
last successful probe:   2024-10-14 04:46:06
last unsuccessful probe: Never failed
total uptime:   4 seconds
total downtime: 0 second
longest consecutive uptime:   4 seconds from 2024-10-14 04:46:03 to 2024-10-14 04:46:06
retried to resolve hostname 0 times
rtt min/avg/max: 0.116/0.159/0.250 ms
--------------------------------------
TCPing started at: 2024-10-14 04:46:03
TCPing ended at:   2024-10-14 04:46:06
duration (HH:MM:SS): 00:00:03
json での出力
$ tcping -j localhost 22 | jq .
{
  "type": "start",
  "message": "TCPinging localhost on port 22",
  "timestamp": "2024-10-14T04:48:32.714879161+09:00",
  "hostname": "localhost",
  "port": 22
}
{
  "type": "probe",
  "message": "Reply from localhost (127.0.0.1) on port 22",
  "timestamp": "2024-10-14T04:48:32.715343413+09:00",
  "addr": "127.0.0.1",
  "hostname": "localhost",
  "is_ip": false,
  "port": 22,
  "success": true,
  "latency": 0.19192,
  "total_successful_probes": 1
}
^C
tcping 中の tcpdump
$ sudo tcpdump -i lo tcp port 8000
tcpdump: verbose output suppressed, use -v[v]... for full protocol decode
listening on lo, link-type EN10MB (Ethernet), snapshot length 262144 bytes
05:13:01.427935 IP6 localhost.40168 > localhost.http-alt: Flags [S], seq 1037512037, win 65476, options [mss 65476,sackOK,TS val 3135501668 ecr 0,nop,wscale 7], length 0
05:13:01.427970 IP6 localhost.http-alt > localhost.40168: Flags [R.], seq 0, ack 1037512038, win 0, length 0
05:13:02.428466 IP6 localhost.40170 > localhost.http-alt: Flags [S], seq 1664447724, win 65476, options [mss 65476,sackOK,TS val 3135502669 ecr 0,nop,wscale 7], length 0
05:13:02.428497 IP6 localhost.http-alt > localhost.40170: Flags [R.], seq 0, ack 1664447725, win 0, length 0
05:13:03.428880 IP6 localhost.43762 > localhost.http-alt: Flags [S], seq 3418125052, win 65476, options [mss 65476,sackOK,TS val 3135503669 ecr 0,nop,wscale 7], length 0
05:13:03.428896 IP6 localhost.http-alt > localhost.43762: Flags [R.], seq 0, ack 3418125053, win 0, length 0
05:13:04.429298 IP6 localhost.43778 > localhost.http-alt: Flags [S], seq 1854699496, win 65476, options [mss 65476,sackOK,TS val 3135504670 ecr 0,nop,wscale 7], length 0
05:13:04.429373 IP6 localhost.http-alt > localhost.43778: Flags [R.], seq 0, ack 1854699497, win 0, length 0

という感じで TCP port を使って疎通確認ができます.
似たものに httping などもありますが httpd 用なので今回の tcping のほうが汎用性がありますね.

$ httping www.matoken.org -l
PING www.matoken.org:443 (/):
connected to 153.121.44.87:443 (290 bytes), seq=0 time=358.74 ms
connected to 153.121.44.87:443 (290 bytes), seq=1 time=291.96 ms
connected to 153.121.44.87:443 (290 bytes), seq=2 time=343.24 ms
connected to 153.121.44.87:443 (290 bytes), seq=3 time=301.26 ms
^CGot signal 2
--- https://www.matoken.org/ ping statistics ---
4 connects, 4 ok, 0.00% failed, time 4776ms
round-trip min/avg/max = 292.0/323.8/358.7 ms
環境
$ tcping -v
TCPING version 1.21.2
$ dpkg-query -W python3 httping
httping 2.5-5.2+b2
python3 3.12.6-1
$ lsb_release -dr
Description:    Debian GNU/Linux trixie/sid
Release:        n/a
$ arch
x86_64
]]>
https://matoken.org/blog/2024/10/14/tcping-to-check-communication-with-tcp-port/feed/ 0
pingでピングーが出てくるpinguコマンド https://matoken.org/blog/2022/06/12/pingu-command-that-comes-out-with-ping/ https://matoken.org/blog/2022/06/12/pingu-command-that-comes-out-with-ping/#respond Sat, 11 Jun 2022 22:02:45 +0000 http://matoken.org/blog/?p=3658

可愛い
てことで手元でも動かしてみました.

$ go install github.com/sheepla/pingu@latest
Note

installしてから気づいたのですが,GitHubのreleaseにいくつかのバイナリがありました.
releaseに無いものはこんな感じで

$ git clone https://github.com/sheepla/pingu && cd pingu
$ GOARCH="arm" GOOS="linux" go build -o pingu_linux-arm main.go
$ GOARCH="amd64" GOOS="openbsd" go build -o pingu_openbsd-amd64 main.go
$ file ./pingu_*
./pingu_linux-arm:     ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), statically linked, Go BuildID=murva-OzxIcI_YixZ4Ug/cNCKM_axMOq6C247-ffX/ZvpTdtm8O3oDXPd__Lpl/cHptxGGRawpNVtoik-SP, not stripped
./pingu_openbsd-amd64: ELF 64-bit LSB executable, x86-64, version 1 (OpenBSD), dynamically linked, interpreter /usr/libexec/ld.so, for OpenBSD, Go BuildID=Q-bDgKXcAqG8C7jI1-AU/tFPAi4Y1vzFywav4YxWJ/RlLmE89x6Zh42bIsaKyQ/oVjfJQxr0rf9Z5byo_ZF, not stripped

早速実行するとpermission deniedで失敗します.

$ pingu localhost
PING localhost (127.0.0.1) type `Ctrl-C` to abort
[ERROR] an error occurred when running ping: socket: permission denied

SUID を設定してみたり,

$ cp -a $(which pingu) .
$ sudo chown 0.0 ./pingu
$ sudo chmod u+s ./pingu
$ ./pingu localhost
PING localhost (127.0.0.1) type `Ctrl-C` to abort
[ERROR] an error occurred when running ping: socket: permission denied

sudoでも駄目です.

$ sudo ~matoken/go/bin/pingu localhost
PING localhost (127.0.0.1) type `Ctrl-C` to abort
[ERROR] an error occurred when running ping: socket: permission denied

検索してみるとこのページを見つけました.

ping コマンドが特別で通常は net.ipv4.ping_group_range の権限が必要なようです.
prettyping は動くのになと思ったけどこれは ping コマンドを呼んでいるので動くのですね.

net.ipv4.ping_group_range を確認すると 1 0 になっています.

$ sysctl -a 2>&1 | grep ping
net.ipv4.ping_group_range = 1   0
$ cat /proc/sys/net/ipv4/ping_group_range
1       0
0 0 に設定することで gid 0 で実行可能に
$ sudo id -g
0
$ sudo sysctl net.ipv4.ping_group_range="0 0"
net.ipv4.ping_group_range = 0 0
$ sudo ~matoken/go/bin/pingu localhost
PING localhost (127.0.0.1) type `Ctrl-C` to abort
 ...        .     ...   ..    ..     .........            seq=0 32bytes from 127.0.0.1: ttl=64 time=242.773µs
 ...     ....          ..  ..      ... .....  .. ..       seq=1 32bytes from 127.0.0.1: ttl=64 time=241.504µs
^C
───── localhost ping statistics ─────
PACKET STATISTICS: 2 transmitted => 2 received (0% loss)
ROUND TRIP: min=241.504µs avg=242.139µs max=242.773µs stddev=634ns
0 1000 に設定することで gid 1000 の一般ユーザでも実行可能に
$ sudo sysctl net.ipv4.ping_group_range="0 $(id -g)"
net.ipv4.ping_group_range = 0 1000
$ pingu localhost
PING localhost (127.0.0.1) type `Ctrl-C` to abort
 ...        .     ...   ..    ..     .........            seq=0 32bytes from 127.0.0.1: ttl=64 time=149.711µs
 ...     ....          ..  ..      ... .....  .. ..       seq=1 32bytes from 127.0.0.1: ttl=64 time=235.975µs
 ...    .......      ...         ... . ..... #######      seq=2 32bytes from 127.0.0.1: ttl=64 time=195.434µs
.....  ........ .###############.....  ... ##########.  . seq=3 32bytes from 127.0.0.1: ttl=64 time=179.31µs
 .... ........#####################.  ... ###########     seq=4 32bytes from 127.0.0.1: ttl=64 time=201.388µs
      ....... ######################.... ############     seq=5 32bytes from 127.0.0.1: ttl=64 time=242.148µs
.    .  .... ########################... ###########      seq=6 32bytes from 127.0.0.1: ttl=64 time=219.127µs
   ..   ....#########################.. .###########      seq=7 32bytes from 127.0.0.1: ttl=64 time=163.007µs
    .       #########################.   .##########      seq=8 32bytes from 127.0.0.1: ttl=64 time=136.878µs
   ....     .########################.      ########      seq=9 32bytes from 127.0.0.1: ttl=64 time=203.288µs
  .....      .  ####################.        #######.     seq=10 32bytes from 127.0.0.1: ttl=64 time=128.612µs
^C
───── localhost ping statistics ─────
PACKET STATISTICS: 11 transmitted => 11 received (0% loss)
ROUND TRIP: min=128.612µs avg=186.807µs max=242.148µs stddev=36.88µs
1001 1000 の様におかしな範囲に設定すると動かなくなります.
$ sudo sysctl net.ipv4.ping_group_range="1001 $(id -g)"
net.ipv4.ping_group_range = 1001 1000
$ pingu localhost
PING localhost (127.0.0.1) type `Ctrl-C` to abort
[ERROR] an error occurred when running ping: socket: permission denied
表 1. そのへんの端末の設定を見てみました
ディストリビューション uname -r net.ipv4.ping_group_range pingu

Debian sid amd64

5.17.0-3-amd64

1 0

NG

Debian bullseye amd64

5.10.0-14-amd64

1 0

NG

Debian buster amd64

5.10.0-0.bpo.12-amd64

1 0

NG

Raspberry Pi OS bullseye armhf

5.15.32-v7+

0 2147483647

OK

Ubuntu 22.10 amd64(development branch)

5.15.0-33-generic

0 2147483647

OK

Note
sysctlは再起動でもとに戻るので永続化したい場合は /etc/sysctl.conf, /etc/sysctl.d/* 辺りに書いておきましょう.

pingu動いたけど色なしのテキストデータだとよくわからないのでスクリーンショットを.

pingu01

ちなみに端末幅が狭いとこんな感じになるので広げてあげましょう(上の画像はcols 111)

pingu02

不安定な回線だと行が減りピングーが縮みます(自宅でのpovo 2.0回線😔)

pingu03

不安定な回線だと prettyping がいいですね.

追記)

Asciinemaで録画してみました.

環境
$ pingu --version
pingu: v???-rev???
$ ls ~/go/pkg/mod/github.com/sheepla/
pingu@v0.0.1
$ dpkg-query -W linux-image-`uname -r` golang iputils-ping
golang:amd64    2:1.18~3
iputils-ping    3:20211215-1
linux-image-5.17.0-3-amd64      5.17.11-1
$ lsb_release -dr
Description:    Debian GNU/Linux bookworm/sid
Release:        unstable
]]>
https://matoken.org/blog/2022/06/12/pingu-command-that-comes-out-with-ping/feed/ 0
カラフルでかわいい prettyping https://matoken.org/blog/2021/04/01/colorful-and-cute-prettyping/ https://matoken.org/blog/2021/04/01/colorful-and-cute-prettyping/#respond Thu, 01 Apr 2021 13:24:54 +0000 http://matoken.org/blog/?p=3174

pingのカラフルなラッパーの prettyping というものを知りました.

少し試してみました.

$ curl -O https://raw.githubusercontent.com/denilsonsa/prettyping/master/prettyping
$ install -m 755 ./prettyping ~/bin/
$ ls -l ~/bin/prettyping
-rwxr-xr-x 1 pi pi 23396 Apr  1 22:08 /home/pi/bin/prettyping
$ ~/bin/prettyping 1.1.1.1
0 ▁ 10 ▂ 20 ▃ 30 ▄ 40 ▅ 50 ▆ 60 ▇ 70 █ 80 ▁ 90 ▂ 100 ▃ 110 ▄ 120 ▅ 130 ▆ 140 ▇ 150 █ 160 ▁ 170 ▂
180 ▃ 190 ▄ 200 ▅ 210 ▆ 220 ▇ 230 █ ∞
PING 1.1.1.1 (1.1.1.1) 56(84) bytes of data.
▂▅█▁▄▂█
 0/  7 ( 0%) lost;   76/ 107/ 178ms; last:   76ms
 0/  7 ( 0%) lost;   76/ 107/ 178/  27ms (last 7)

カラーで見るときれいです.

prettyping

安定した回線だと緑で面白くないので不安定な回線や遠くのアドレスを指定するといいかもしれません.今回は電波状況の悪い携帯回線で試しました.(-120dBM前後の圏外になったり戻ったりな場所)


https://www.youtube.com/watch?v=gL87DBohRaQ
環境
$ dpkg-query -W iputils-ping curl coreutils
coreutils	8.30-3
curl	7.64.0-4+deb10u1
iputils-ping	3:20180629-2+deb10u2
$ lsb_release -dr
Description:	Raspbian GNU/Linux 10 (buster)
Release:	10
$ arch
armv7l
$ cat /proc/device-tree/model ;echo
Raspberry Pi 3 Model B Rev 1.2
]]>
https://matoken.org/blog/2021/04/01/colorful-and-cute-prettyping/feed/ 0