PING 和 TCPING:网络连通性检测的两种武器

2024-08-07 Linux 运维

检查网络通不通,最常用的命令就是 ping。但有时候 ping 通了、网页却打不开,或者 ping 不通但服务明明正常——这是怎么回事?

这就涉及到 PING(ICMP) 和 TCPING(TCP) 的根本区别。

PING 是什么

ping 基于 ICMP(Internet Control Message Protocol) 协议,工作在 网络层(Layer 3)。它发送 ICMP Echo Request 报文,目标主机回复 Echo Reply,以此判断网络连通性。

# 基本用法
ping 8.8.8.8

# Windows 下默认 ping 4 次后停止
ping -n 4 8.8.8.8

# Linux 下持续 ping,按 Ctrl+C 停止
ping 8.8.8.8

# 指定间隔(秒)和次数
ping -i 0.5 -c 10 8.8.8.8

ping 能告诉我们什么

指标 说明 正常范围
丢包率 请求无响应的比例 0%(局域网),< 1%(外网)
延迟(RTT) 往返时间 < 1ms(同机房),< 50ms(同城)
TTL 数据包存活时间(经过的路由跳数) 64/128/255 常见初始值

ping 的局限

1. ICMP 可能被防火墙拦截

很多云服务商(AWS、阿里云、腾讯云)默认在安全组中 禁 ping:

# ping 云服务器可能得到
$ ping 47.xxx.xxx.xxx
Request timeout for icmp_seq 0   # 一直超时

但这不代表服务器宕机——ICMP 报文被防火墙丢弃了,HTTP/SSH 等服务可能完全正常。

2. ICMP 无法检测端口

ping 只能检测 IP 层的可达性,无法判断某个端口是否开放:

# ping 通只能说明主机在线
ping example.com  ✅

# 但无法确认 HTTP 服务是否正常(端口 80)

3. 某些网络环境会限速或丢弃 ICMP

运营商可能对 ICMP 报文做 QoS 限速,丢包率高但实际 TCP 业务正常。

TCPING 是什么

TCPING 是基于 TCP 协议的连通性检测工具,工作在 传输层(Layer 4)。它尝试与目标主机的指定端口建立 TCP 连接(完成三次握手),如果连接成功,说明该端口可达。

# Linux 用 tcping 命令
tcping example.com 80

# 如果没有 tcping,可以用 curl 模拟
curl -s -o /dev/null -w "HTTP %{http_code} | %{time_total}s\n" http://example.com

# 或者用 nc(netcat)
nc -zv example.com 80

安装 tcping

Linux:

# Debian/Ubuntu
apt install tcping

# CentOS/RHEL(需要 EPEL)
yum install tcping

Windows:

# 下载 tcping.exe
# https://www.elifulkerson.com/projects/tcping.php
tcping.exe example.com 80

tcping 常用参数

# 持续检测,每秒一次
tcping -t example.com 80

# 指定次数
tcping -c 5 example.com 80

# 指定间隔(秒)
tcping -i 2 example.com 80

# 显示详细时间戳
tcping -d example.com 80

用其他工具实现 TCPING

nc(netcat)—— 最通用

# -z:只扫描不发送数据;-v:详细输出
nc -zv example.com 80

# 带超时
nc -zv -w 3 example.com 80

# 检查端口范围
nc -zv example.com 20-80

输出示例:

Connection to example.com (93.184.216.34) 80 port [tcp/http] succeeded!

curl —— 最直观(针对 HTTP/HTTPS)

# 只检查 HTTP 状态码和响应时间
curl -s -o /dev/null -w "HTTP %{http_code} | %{time_total}s | %{time_connect}s\n" https://example.com

参数说明:

  • %{http_code} — HTTP 状态码(200=正常)
  • %{time_total} — 总耗时
  • %{time_connect} — TCP 连接耗时

/dev/tcp(Bash 内置)—— 无需安装

Bash 支持通过 /dev/tcp 直接建立 TCP 连接:

# 尝试连接,成功返回 0
timeout 3 bash -c 'echo > /dev/tcp/example.com/80' && echo "开放" || echo "关闭"

PowerShell(Windows)

# Test-NetConnection 内置 cmdlet
Test-NetConnection example.com -Port 80

PING vs TCPING 对比

特性 PING TCPING
协议 ICMP(Layer 3) TCP(Layer 4)
检测对象 IP 可达性 端口可达性
是否受防火墙影响 ⚠️ 常被禁 ping ✅ HTTP/SSH 端口通常放行
能否检测 Web 服务 ❌ 不能 ✅ 能
是否需要指定端口 ❌ 不需要 ✅ 必须指定
安装依赖 系统内置 可能需要安装 tcping
适用场景 基础网络连通性 服务可用性检测

实战场景

场景 1:服务器明明在线,ping 却超时

# ❌ ping 超时
$ ping 47.95.xxx.xxx
Request timeout

# ✅ 用 tcping 检查 SSH 端口
$ tcping 47.95.xxx.xxx 22
Port 22 is open!

# 或者用 nc
$ nc -zv 47.95.xxx.xxx 22
Connection to 47.95.xxx.xxx 22 port [tcp/ssh] succeeded!

结论: 服务器正常,只是安全组禁了 ICMP。

场景 2:ping 通但网页打不开

# ✅ ping 通
$ ping example.com
64 bytes from 93.184.216.34: icmp_seq=1 ttl=56 time=12ms

# ❌ 网页打不开,tcping 看 80 端口
$ tcping example.com 80
Port 80 is closed

结论: 主机在线但 Web 服务挂了(Nginx 崩溃、端口没监听等)。

场景 3:延迟抖动排查

# ping 看基础延迟
$ ping -c 10 example.com
rtt min/avg/max/mdev = 10.2/45.8/120.5/32.1ms

# tcping 看实际业务端口的连接延迟
$ tcping example.com 443
Connected to example.com:443, time 12.3ms
Connected to example.com:443, time 15.7ms
Connected to example.com:443, time 11.9ms

如果 ping 延迟高但 tcping 延迟正常 → ICMP 被 QOS 限速,实际 TCP 业务不受影响。

场景 4:快速端口扫描

# 用 nc 扫描常见端口
for port in 22 80 443 3306 6379 8080; do
    nc -zv -w 2 example.com $port 2>&1 | grep -E "succeeded|open"
done

总结

情况 推荐命令 判断
基础网络是否通 ping 目标IP 不丢包=网络正常
防火墙是否禁 ping tcping 目标IP 22 SSH 通=服务器正常
Web 服务是否正常 curl -I http://域名 200=正常
某个端口是否开放 nc -zv 目标IP 端口 succeeded=开放
连接延迟 tcping 目标IP 端口 看 time 字段
全端口扫描 nmap -p 1-65535 目标IP 列出所有开放端口

一句话口诀:

Ping 看网络通不通,TCPing 看服务开没开。一个管 Layer 3,一个管 Layer 4,配合使用才能准确定位问题。