在Linux中使用tcping工具检测指定ip相关端口号的连通和延迟

1,591 阅读4分钟

前提

问题:客户在进行相关ip的端口探测的时候,出现超时情况

排查方法:使用tcping工具从不同地区分别向该ip的端口进行检测,查看其丢包情况

一、使用tcping

这个工具比较老了,有linux版和windows版的

1、安装tcping

(1)安装tcptraceroute(一款tcp追踪路由的工具)

yum -y update
yum -y install tcptraceroute bc

(2)安装tcping

cd /usr/bin  
wget -O tcping https://soft.mengclaw.com/Bash/TCP-PING
chmod +x tcping

(3)测试(如下即可使用)

root@VM-4-2-centos:~# tcping
tcpping v1.7 Richard van den Berg <richard@vdberg.org>

Usage: tcping [-d] [-c] [-C] [-w sec] [-q num] [-x count] ipaddress [port]

        -d   print timestamp before every result
        -c   print a columned result line
        -C   print in the same format as fping's -C option
        -w   wait time in seconds (defaults to 3)
        -r   repeat every n seconds (defaults to 1)
        -x   repeat n times (defaults to unlimited)

See also: man tcptraceroute

翻译过来就是

# 用法:tcpping [-d] [-c] [-C] [-w sec] [-q num] [-x count] ipaddress [port]
# -d 在每个响应时间前,打印时间戳
# -c 以列表形式显示
# -C 输出类似于fping工具中-C选项的结果
# -w 等待时间(默认 3)
# -r 每N秒重试一次(默认 1)
# -x 限定测试总时长 (默认 无限)

二、其他的开源tcping

在Github上还有几个不错的开源tcping,测试下来也可以很好用

(一)、cloverstd/tcping

1、下载(下载不下来可用当前可用的github国内镜像源下载)

wget https://github.com/cloverstd/tcping/releases/download/v0.1.1/tcping-linux-amd64-v0.1.1.tar.gz

2、解压开箱即用

tar -zxvf tcping-linux-amd64-v0.1.1.tar.gz
chmod -x tcping
mv tcping /usr/bin/tcping

3、使用

root@VM-4-2-centos:~# /root/tcping
Usage:
  tcping host port [flags]
Examples:

  1. ping over tcp
        > tcping google.com
  2. ping over tcp with custom port
        > tcping google.com 443
  3. ping over http
        > tcping -H google.com
  4. ping with URI schema
        > tcping http://hui.lu
Flags:
  -c, --counter int              ping counter (default 4)
  -D, --dns-server stringArray   Use the specified dns resolve server.
      --head                     Use POST instead of GET in http mode.
  -h, --help                     help for tcping
  -H, --http                     Use "HTTP" mode. will ignore URI Schema, force to http
  -I, --interval string          ping interval, units are "ns", "us" (or "µs"), "ms", "s", "m", "h" (default "1s")
      --post                     Use HEAD instead of GET in http mode.
  -T, --timeout string           connect timeout, units are "ns", "us" (or "µs"), "ms", "s", "m", "h" (default "1s")
      --user-agent string        Use custom UA in http mode. (default "tcping")
  -v, --version                  show the version and exit

翻译过来就是

用法:
tcping主机端口[标志]
例如:
1.通过tcp进行ping

>tcping google.com

2.使用自定义端口通过tcp进行ping

>谷歌的tcping。com 443

3.通过http进行ping

>tcping-H google.com

4.使用URI模式ping

>tcpinghttp://hui.lu

Flag:
-c --counter int             ping计数器(默认为4)
-D --dns-server stringArray  使用指定的dns解析服务器。
   --head                    在http模式下使用POST而不是GET。
-h --help                    tcping的帮助
-H --http                    使用“http”模式。将忽略URI架构,强制为http
-I --interval string         ping间隔,单位为“ns”、“us”(或“µs”)、“ms”、“s”、“m”、“h”(默认为                                  “1s”)
   --post                    在http模式下,post使用HEAD而不是GET。
-T --timeout string          连接超时,单位为“ns”、“us”(或“µs”)、“ms”、“s”、“m”、“h”(默认为                                  “1s”)
   --user-agent string       在http模式下使用自定义UA。(默认为“tcping”)
-v --version                 显示版本并退出

(二)、pouriyajamshidi/tcping

也是git上开源的项目

1、下载安装

wget https://github.com/pouriyajamshidi/tcping/releases/download/v1.12.1/tcping_Linux.zip
yum install unzip
unzip tcping_Linux.zip
chmod +x tcping
cp tcping /usr/bin/tcping

2、查看使用说明

root@VM-4-2-centos:~# /usr/bin/tcping
TCPING version 1.12.1
Try running /usr/bin/tcping like:
/usr/bin/tcping <hostname/ip> <port number>. For example:
/usr/bin/tcping www.example.com 443

[optional flags]
  -j : output in JSON format.
  -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.

翻译过来就是

 -j:以JSON格式输出。
 -r:在失败请求数<n>之后,重试解析目标主机名。e、g.-r 10用于10个故障探头。
 -u:检查更新。
 -v:显示版本。

3、执行测试

root@VM-4-2-centos:~# /usr/bin/tcping www.baidu.com 80
TCPinging www.baidu.com on port 80
Reply from www.baidu.com (112.80.248.75) on port 80 TCP_conn=1 time=8 ms
Reply from www.baidu.com (112.80.248.75) on port 80 TCP_conn=2 time=9 ms
Reply from www.baidu.com (112.80.248.75) on port 80 TCP_conn=3 time=8 ms
Reply from www.baidu.com (112.80.248.75) on port 80 TCP_conn=4 time=12 ms
Reply from www.baidu.com (112.80.248.75) on port 80 TCP_conn=5 time=12 ms
Reply from www.baidu.com (112.80.248.75) on port 80 TCP_conn=6 time=8 ms
^C
--- www.baidu.com TCPing statistics ---
6 probes transmitted, 6 received, 0.00% packet loss
successful probes:   6
unsuccessful probes: 0
last successful probe:   2022-08-25 13:25:05
last unsuccessful probe: Never failed
total uptime:   6 seconds
total downtime: 0 second
longest consecutive uptime:  6 seconds from 2022-08-25 13:25:00 to 2022-08-25 13:25:05
retried to resolve hostname 0 times
rtt min/avg/max: 8/9.50/12 ms
--------------------------------------
TCPing started at: 2022-08-25 13:25:00
TCPing ended at:   2022-08-25 13:25:06
duration (HH:MM:SS): 00:00:05

这个项目也可以用docker来启动

docker pull pouriyajamshidi/tcping:latest

然后执行

docker run -it pouriyajamshidi/tcping:latest www.baidu.com 80

附录、使用tcptraceroute来检测路由

在第一种方法里,有安装的tcptraceroute可以用来检测端口路由

使用方法:

root@VM-4-2-centos:~# tcptraceroute

tcptraceroute 1.5beta7
Copyright (c) 2001-2006 Michael C. Toren <mct@toren.net>
Updates are available from http://michael.toren.net/code/tcptraceroute/

Usage: tcptraceroute [-nNFSAE] [-i <interface>] [-f <first ttl>]
       [-l <packet length>] [-q <number of queries>] [-t <tos>]
       [-m <max ttl>] [-pP] <source port>] [-s <source address>]
       [-w <wait time>] <host> [destination port] [packet length]

举例:

root@VM-4-2-centos:~# tcptraceroute www.baidu.com 80
Selected device eth0, address 10.0.4.2, port 39367 for outgoing packets
Tracing the path to www.baidu.com (112.80.248.75) on TCP port 80 (http), 30 hops max
 1  * * *
 2  * * *
 3  * * *
 4  * * *
 5  * * *
 6  220.196.196.9  3.265 ms  2.961 ms  3.012 ms
 7  * * *
 8  * 219.158.11.2 9.680 ms  9.211 ms
 9  * * *
10  153.37.96.186  8.851 ms  8.756 ms  8.720 ms
11  * * *
12  112.80.248.75 [open]  7.918 ms  7.976 ms  7.945 ms