0 前言
网络信息是电脑网络信息安全检查中的一块重要内容,Linux和基于Linux的操作系统,提供了很多的网络命令,今天我们研究最常用的ping命令。
1 ping命令 的功能、格式和选项说明
1.1 ping命令 的功能
简单来说, ping 命令 会使用 ICMP(Internet Control Message Protocol) 传输协议,向网络主机发出要求回应的信息,若网络主机的网络功能没有问题,就会回应该信息,从而得知该主机运作正常。
具体来说,ping 命令使用 ICMP 协议的强制ECHO_REQUEST数据报从主机或网关引出 ICMP ECHO_RESPONSE。ECHO_REQUEST数据报 (''pings'') 都有一个 IP 和 ICMP 标头,后跟一个结构体 timeval,然后是用于填充数据包的任意数量的 ''pad'' 字节。
ping 命令常用于检测 网络中的另一台主机或路由器是否可访问。
1.2 Linux中的ping命令支持ipV6吗?
- 在基于 Debian 的 Linux 发行版(包括 Ubuntu)上,您可以使用 ping6 命令强制 ping 使用 IPv6 而不是 IPv4。
- 在基于 Red Hat 的发行版(如 CentOS)和基于 Arch Linux 的发行版(如 Manjaro)上,使用带 -6 选项的 ping 命令来强制 IPv6。
1.3 Linux中的ping命令与Windows中的ping命令相同吗?
Linux中的ping命令与Windows中的ping命令在功能上可以说是相同的,但是两者在某些方面存在不同。例如,在默认情况下,Linux 下的ping会连续发送包,而Windows的ping命令 仅发送 4 个包。
Windows 中的ping命令格式如下:
C:\Users\purpleendurer>ping
用法: ping [-t] [-a] [-n count] [-l size] [-f] [-i TTL] [-v TOS]
[-r count] [-s count] [[-j host-list] | [-k host-list]]
[-w timeout] [-R] [-S srcaddr] [-c compartment] [-p]
[-4] [-6] target_name
选项:
-t Ping 指定的主机,直到停止。
若要查看统计信息并继续操作,请键入 Ctrl+Break;
若要停止,请键入 Ctrl+C。
-a 将地址解析为主机名。
-n count 要发送的回显请求数。
-l size 发送缓冲区大小。
-f 在数据包中设置“不分段”标记(仅适用于 IPv4)。
-i TTL 生存时间。
-v TOS 服务类型(仅适用于 IPv4。该设置已被弃用,
对 IP 标头中的服务类型字段没有任何
影响)。
-r count 记录计数跃点的路由(仅适用于 IPv4)。
-s count 计数跃点的时间戳(仅适用于 IPv4)。
-j host-list 与主机列表一起使用的松散源路由(仅适用于 IPv4)。
-k host-list 与主机列表一起使用的严格源路由(仅适用于 IPv4)。
-w timeout 等待每次回复的超时时间(毫秒)。
-R 同样使用路由标头测试反向路由(仅适用于 IPv6)。
根据 RFC 5095,已弃用此路由标头。
如果使用此标头,某些系统可能丢弃
回显请求。
-S srcaddr 要使用的源地址。
-c compartment 路由隔离舱标识符。
-p Ping Hyper-V 网络虚拟化提供程序地址。
-4 强制使用 IPv4。
-6 强制使用 IPv6。
1.4 ping命令的格式
与我们之前学的大多数linux命令不同,ping命令不支持--help选项。
我们可以使用ping 或 ping -?命令获取ping命令的格式信息。
[purpleendurer @ bash ~] ping
Usage: ping [-aAbBdDfhLnOqrRUvV64] [-c count] [-i interval] [-I interface]
[-m mark] [-M pmtudisc_option] [-l preload] [-p pattern] [-Q tos]
[-s packetsize] [-S sndbuf] [-t ttl] [-T timestamp_option]
[-w deadline] [-W timeout] [hop1 ...] destination
Usage: ping -6 [-aAbBdDfhLnOqrRUvV] [-c count] [-i interval] [-I interface]
[-l preload] [-m mark] [-M pmtudisc_option]
[-N nodeinfo_option] [-p pattern] [-Q tclass] [-s packetsize]
[-S sndbuf] [-t ttl] [-T timestamp_option] [-w deadline]
[-W timeout] destination
[purpleendurer @ bash ~] ping -?
Usage: ping [-aAbBdDfhLnOqrRUvV64] [-c count] [-i interval] [-I interface]
[-m mark] [-M pmtudisc_option] [-l preload] [-p pattern] [-Q tos]
[-s packetsize] [-S sndbuf] [-t ttl] [-T timestamp_option]
[-w deadline] [-W timeout] [hop1 ...] destination
Usage: ping -6 [-aAbBdDfhLnOqrRUvV] [-c count] [-i interval] [-I interface]
[-l preload] [-m mark] [-M pmtudisc_option]
[-N nodeinfo_option] [-p pattern] [-Q tclass] [-s packetsize]
[-S sndbuf] [-t ttl] [-T timestamp_option] [-w deadline]
[-W timeout] destination
[purpleendurer @ bash ~]
1.4.1支持ipV4的ping命令格式
ping [-aAbBdDfhLnOqrRUvV64] [-c 完成次数] [-i 间隔秒数] [-I 网络界面]
[-m mark] [-M pmtudisc_option] [-l 前置载入] [-p 范本样式] [-Q tos]
[-s 数据包大小] [-S sndbuf] [-t 存活数值] [-T 间间戳选项]
[-w 退出前等待秒数] [-W 执行前等待秒数] [hop1 ...] 目标主机名称或IP地址
1.4.2 支持ipV6的ping命令格式
ping -6 [-aAbBdDfhLnOqrRUvV] [-c 完成次数] [-i 间隔秒数] [-I 网络界面]
[-l 前置载入] [-m mark] [-M pmtudisc_option]
[-N nodeinfo_option] [-p 范本样式] [-Q tclass] [-s 数据包大小]
[-S sndbuf] [-t 存活数值] [-T 间间戳选项] [-w 退出前等待秒数]
[[-W 执行前等待秒数] 目标主机名称或IP地址
1.5 ping命令选项说明
ping命令的选项非常多,而且不同版本linux 提供的ping命令选项也不尽相同。
参数 | 描述 |
---|---|
-a | Audible ping. 可听到的ping |
-A |
|
-b | Allow pinging a broadcast address. 允许对广播地址进行ping |
-B |
|
-c count |
|
-d |
|
-F flow label |
|
-f |
|
-i interval |
|
-I interface address |
|
-l preload |
|
-L |
|
-n |
|
-p pattern |
|
-Q tos |
|
-q |
|
-R |
|
-r |
|
-s packetsize |
|
-S sndbuf |
|
-t ttl | Set the IP Time to Live. 设置IP的生存时间(TTL) |
-T timestamp option |
|
-M hint |
|
-U |
|
-v | Verbose output. 详细输出 |
-V | Show version and exit. 显示版本并退出 |
-w deadline |
|
-W timeout |
|
2 ping命令实例
2.1 ping -a g.cn
[purpleendurer @ bash ~] ping -a g.cn
PING g.cn (114.250.67.34) 56(84) bytes of data.
64 bytes from 114.250.67.34 (114.250.67.34): icmp_seq=1 ttl=116 time=4.41 ms
64 bytes from 114.250.67.34 (114.250.67.34): icmp_seq=2 ttl=116 time=4.36 ms
64 bytes from 114.250.67.34 (114.250.67.34): icmp_seq=3 ttl=116 time=4.34 ms
64 bytes from 114.250.67.34 (114.250.67.34): icmp_seq=4 ttl=116 time=4.38 ms
64 bytes from 114.250.67.34 (114.250.67.34): icmp_seq=5 ttl=116 time=4.34 ms
64 bytes from 114.250.67.34 (114.250.67.34): icmp_seq=6 ttl=116 time=4.36 ms
64 bytes from 114.250.67.34 (114.250.67.34): icmp_seq=7 ttl=116 time=4.36 ms
^C
--- g.cn ping statistics ---
7 packets transmitted, 7 received, 0% packet loss, time 6007ms
rtt min/avg/max/mdev = 4.342/4.368/4.414/0.041 ms
[purpleendurer @ bash ~]
1.2 ping -A g.cn :自适应 ping g.cn
[purpleendurer @ bash ~] ping -A g.cn
PING g.cn (114.250.70.34) 56(84) bytes of data.
64 bytes from 114.250.70.34 (114.250.70.34): icmp_seq=1 ttl=117 time=5.04 ms
64 bytes from 114.250.70.34 (114.250.70.34): icmp_seq=2 ttl=117 time=4.76 ms
64 bytes from 114.250.70.34 (114.250.70.34): icmp_seq=3 ttl=117 time=4.76 ms
64 bytes from 114.250.70.34 (114.250.70.34): icmp_seq=4 ttl=117 time=4.77 ms
64 bytes from 114.250.70.34 (114.250.70.34): icmp_seq=5 ttl=117 time=4.76 ms
64 bytes from 114.250.70.34 (114.250.70.34): icmp_seq=6 ttl=117 time=4.81 ms
64 bytes from 114.250.70.34 (114.250.70.34): icmp_seq=7 ttl=117 time=4.77 ms
64 bytes from 114.250.70.34 (114.250.70.34): icmp_seq=8 ttl=117 time=4.79 ms
64 bytes from 114.250.70.34 (114.250.70.34): icmp_seq=9 ttl=117 time=4.98 ms
64 bytes from 114.250.70.34 (114.250.70.34): icmp_seq=10 ttl=117 time=4.81 ms
64 bytes from 114.250.70.34 (114.250.70.34): icmp_seq=11 ttl=117 time=4.77 ms
64 bytes from 114.250.70.34 (114.250.70.34): icmp_seq=12 ttl=117 time=4.78 ms
^C
--- g.cn ping statistics ---
12 packets transmitted, 12 received, 0% packet loss, time 2210ms
rtt min/avg/max/mdev = 4.760/4.820/5.045/0.123 ms, ipg/ewma 200.918/4.862 ms
[purpleendurer @ bash ~]
可以看到,ping -A 返回个信息的速度明显快于 ping -a。
1.3 ping -b g.cn:使用广播方式ping g.cn
[purpleendurer @ bash ~] ping -b g.cn
PING g.cn (114.250.70.34) 56(84) bytes of data.
64 bytes from 114.250.70.34 (114.250.70.34): icmp_seq=1 ttl=117 time=4.83 ms
64 bytes from 114.250.70.34 (114.250.70.34): icmp_seq=2 ttl=117 time=4.81 ms
64 bytes from 114.250.70.34 (114.250.70.34): icmp_seq=3 ttl=117 time=4.80 ms
64 bytes from 114.250.70.34 (114.250.70.34): icmp_seq=4 ttl=117 time=4.76 ms
64 bytes from 114.250.70.34 (114.250.70.34): icmp_seq=5 ttl=117 time=4.80 ms
64 bytes from 114.250.70.34 (114.250.70.34): icmp_seq=6 ttl=117 time=4.86 ms
64 bytes from 114.250.70.34 (114.250.70.34): icmp_seq=7 ttl=117 time=4.80 ms
^C
--- g.cn ping statistics ---
7 packets transmitted, 7 received, 0% packet loss, time 6011ms
rtt min/avg/max/mdev = 4.766/4.813/4.862/0.095 ms
[purpleendurer @ bash ~]
1.4 ping -B g.cn
[purpleendurer @ bash ~] ping -B g.cn
PING g.cn (114.250.67.34) from 172.25.109.185 : 56(84) bytes of data.
64 bytes from 114.250.67.34 (114.250.67.34): icmp_seq=1 ttl=116 time=4.01 ms
64 bytes from 114.250.67.34 (114.250.67.34): icmp_seq=2 ttl=116 time=4.36 ms
64 bytes from 114.250.67.34 (114.250.67.34): icmp_seq=3 ttl=116 time=3.94 ms
^C
--- g.cn ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2002ms
rtt min/avg/max/mdev = 3.943/4.108/4.364/0.197 ms
[purpleendurer @ bash ~]
1.5 ping -c 2 g.cn:向g.cn发送2个ICMP包
[purpleendurer @ bash ~] ping -c 2 g.cn
PING g.cn (114.250.70.34) 56(84) bytes of data.
64 bytes from 114.250.70.34 (114.250.70.34): icmp_seq=1 ttl=117 time=4.82 ms
64 bytes from 114.250.70.34 (114.250.70.34): icmp_seq=2 ttl=117 time=4.82 ms
--- g.cn ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1001ms
rtt min/avg/max/mdev = 4.822/4.822/4.822/0.000 ms
[purpleendurer @ bash ~]
1.6 ping -d g.cn
[purpleendurer @ bash ~] ping -d g.cn
PING g.cn (114.250.67.34) 56(84) bytes of data.
64 bytes from 114.250.67.34 (114.250.67.34): icmp_seq=1 ttl=116 time=3.95 ms
64 bytes from 114.250.67.34 (114.250.67.34): icmp_seq=2 ttl=116 time=3.97 ms
64 bytes from 114.250.67.34 (114.250.67.34): icmp_seq=3 ttl=116 time=3.92 ms
64 bytes from 114.250.67.34 (114.250.67.34): icmp_seq=4 ttl=116 time=3.89 ms
64 bytes from 114.250.67.34 (114.250.67.34): icmp_seq=5 ttl=116 time=3.92 ms
^C
--- g.cn ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4005ms
rtt min/avg/max/mdev = 3.896/3.933/3.976/0.062 ms
[purpleendurer @ bash ~]
1.7 ping -f g.cn:洪水 ping g.cn (只有超级用户才能使用此选项)
[purpleendurer @ bash ~] ping -f g.cn
PING g.cn (114.250.67.34) 56(84) bytes of data.
ping: cannot flood; minimal interval allowed for user is 200ms
[purpleendurer @ bash ~] sudo ping -f g.cn
PING g.cn (114.250.65.34) 56(84) bytes of data.
.^C
--- g.cn ping statistics ---
5304 packets transmitted, 5303 received, 0% packet loss, time 28698ms
rtt min/avg/max/mdev = 5.285/5.390/12.965/0.268 ms, pipe 2, ipg/ewma 5.411/5.371 ms
[purpleendurer @ bash ~]
1.8 ping -i 2 -c 3 g.cn:间隔2秒钟向g.cn发送3个ICMP包
[purpleendurer @ bash ~] ping -i 2 -c 3 g.cn
PING g.cn (114.250.65.34) 56(84) bytes of data.
64 bytes from 114.250.65.34 (114.250.65.34): icmp_seq=1 ttl=117 time=5.49 ms
64 bytes from 114.250.65.34 (114.250.65.34): icmp_seq=2 ttl=117 time=5.38 ms
64 bytes from 114.250.65.34 (114.250.65.34): icmp_seq=3 ttl=117 time=5.40 ms
--- g.cn ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 4005ms
rtt min/avg/max/mdev = 5.389/5.428/5.491/0.096 ms
[purpleendurer @ bash ~]
1.9 ping -q g.cn :以静默方式ping g.cn
[purpleendurer @ bash ~] ping -q g.cn
PING g.cn (114.250.65.34) 56(84) bytes of data.
^C
--- g.cn ping statistics ---
8 packets transmitted, 8 received, 0% packet loss, time 7010ms
rtt min/avg/max/mdev = 5.386/5.410/5.453/0.023 ms
[purpleendurer @ bash ~]
命令返回的信息除了启动时和完成时的总结行外,不显示任何内容。
1.10 ping -s 99 g.cn:以99个字节ping g.cn
[purpleendurer @ bash ~] ping -s 99 g.cn
PING g.cn (114.250.65.34) 99(127) bytes of data.
107 bytes from 114.250.65.34 (114.250.65.34): icmp_seq=1 ttl=117 time=5.43 ms
107 bytes from 114.250.65.34 (114.250.65.34): icmp_seq=2 ttl=117 time=5.44 ms
107 bytes from 114.250.65.34 (114.250.65.34): icmp_seq=3 ttl=117 time=5.39 ms
^C
--- g.cn ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2003ms
rtt min/avg/max/mdev = 5.396/5.427/5.449/0.088 ms
[purpleendurer @ bash ~]
1.11 ping -t 16 g.cn: 以TTL 值为16向g.cn发送ICMP包
[purpleendurer @ bash ~] ping -t 16 g.cn
PING g.cn (114.250.65.34) 56(84) bytes of data.
64 bytes from 114.250.65.34 (114.250.65.34): icmp_seq=1 ttl=117 time=5.39 ms
64 bytes from 114.250.65.34 (114.250.65.34): icmp_seq=2 ttl=117 time=5.40 ms
64 bytes from 114.250.65.34 (114.250.65.34): icmp_seq=3 ttl=117 time=5.38 ms
64 bytes from 114.250.65.34 (114.250.65.34): icmp_seq=4 ttl=117 time=5.39 ms
^C
--- g.cn ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3004ms
rtt min/avg/max/mdev = 5.382/5.393/5.407/0.090 ms
[purpleendurer @ bash ~]
1.12 ping -T <时间戳> g.cn:设置特殊的 IP 时间戳选项来ping g.cn
[purpleendurer @ bash ~] ping -T tsandaddr g.cn
PING g.cn (114.250.66.34) 56(124) bytes of data.
^C
--- g.cn ping statistics ---
8 packets transmitted, 0 received, 100% packet loss, time 7166ms
[purpleendurer @ bash ~] ping -T tsonly g.cn
PING g.cn (114.250.66.34) 56(124) bytes of data.
^C
--- g.cn ping statistics ---
5 packets transmitted, 0 received, 100% packet loss, time 4091ms
[purpleendurer @ bash ~]
1.13 ping -v g.cn:启用详细模式ping g.cn
[purpleendurer @ bash ~] ping -v g.cn
PING g.cn (114.250.65.34) 56(84) bytes of data.
64 bytes from 114.250.65.34 (114.250.65.34): icmp_seq=1 ttl=117 time=5.51 ms
64 bytes from 114.250.65.34 (114.250.65.34): icmp_seq=2 ttl=117 time=5.40 ms
64 bytes from 114.250.65.34 (114.250.65.34): icmp_seq=3 ttl=117 time=5.37 ms
64 bytes from 114.250.65.34 (114.250.65.34): icmp_seq=4 ttl=117 time=5.45 ms
64 bytes from 114.250.65.34 (114.250.65.34): icmp_seq=5 ttl=117 time=5.35 ms
64 bytes from 114.250.65.34 (114.250.65.34): icmp_seq=6 ttl=117 time=5.41 ms
^C
--- g.cn ping statistics ---
6 packets transmitted, 6 received, 0% packet loss, time 5006ms
rtt min/avg/max/mdev = 5.358/5.420/5.516/0.100 ms
[purpleendurer @ bash ~]
好像没有显示什么更详细的信息
3 参考资料
ping(8) - Linux man page (die.net)