TCP portに対して疎通確認をするtcping

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

コメントを残す

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