侧边栏壁纸
Linux入门自学网博主等级

每日学一条Linux命令,终成Linux大神

  • 累计撰写 725 篇文章
  • 累计创建 143 个标签
  • 累计收到 0 条评论

目 录CONTENT

文章目录

Linux fping命令教程:如何高效地检测网络主机的存活状态(附实例详解和注意事项)

Linux fping命令介绍

fping是一个全称为fast ping的命令,它用来检测网络主机是否存活,也就是是否能够响应ICMP回显请求。fping与ping的不同之处在于,你可以在命令行上指定任意数量的目标,或者指定一个包含要ping的目标列表的文件。fping不会像ping那样向一个目标发送直到超时或回复为止,而是以轮询的方式向每个目标发送一个ping包,并移动到下一个目标。

Linux fping命令适用的Linux版本

fping命令可以在大多数Linux发行版中安装,使用包管理工具即可,例如:

[linux@bashcommandnotfound.cn ~]$ sudo apt install fping # 在Debian/Ubuntu上安装
[linux@bashcommandnotfound.cn ~]$ sudo yum install fping # 在CentOS/RHEL上安装
[linux@bashcommandnotfound.cn ~]$ sudo dnf install fping # 在Fedora 22+上安装
[linux@bashcommandnotfound.cn ~]$ sudo pacman -S fping # 在Arch Linux上安装

也可以从源代码包安装最新版本的fping(4.0),使用以下命令:

[linux@bashcommandnotfound.cn ~]$ wget https://fping.org/dist/fping-4.0.tar.gz
[linux@bashcommandnotfound.cn ~]$ tar -xvf fping-4.0.tar.gz
[linux@bashcommandnotfound.cn ~]$ cd fping-4.0/
[linux@bashcommandnotfound.cn ~]$ ./configure
[linux@bashcommandnotfound.cn ~]$ make && make install

Linux fping命令的基本语法

fping命令的基本语法格式如下:

fping [options] [targets...]

其中,options是一些可选的参数,用来控制fping的行为,targets是要ping的目标,可以是IP地址或主机名,也可以是一个文件名(-表示从标准输入读取)。

Linux fping命令的常用选项或参数说明

fping命令有很多选项或参数,可以用fping -h查看帮助信息,这里列举一些常用的:

选项参数说明
-4只ping IPv4地址
-6只ping IPv6地址
-bBYTES发送的ping数据的大小,单位是字节(默认是56)
-BN设置指数退避因子为N(默认是1.5)
-cN计数模式:向每个目标发送N个ping包
-fFILE从文件中读取目标列表(-表示从标准输入读取)
-g生成目标列表(只有在没有-f指定时才有效)(给出起始和结束IP地址,或者一个CIDR地址)(例如:fping -g 192.168.1.0 192.168.1.255 或 fping -g 192.168.1.0/24)
-HN设置IP TTL值(跳数)
-IIFACE绑定到特定的接口
-l循环模式:无限发送ping包
-m使用提供的主机名的所有IP地址(例如IPv4和IPv6),与-A一起使用
-M设置不分片标志
-ON设置服务类型(tos)标志在ICMP包上
-pMSEC向一个目标发送ping包的间隔(单位是毫秒)(在循环和计数模式下,默认是1000毫秒)
-rN重试次数(默认是3)
-R随机数据包数据(为了防止链路数据压缩)
-SIP设置源地址
-tMSEC单个目标的初始超时(默认是500毫秒,除非使用-l/-c/-C,那么就是-p周期,最多2000毫秒)

Linux fping命令的实例

下面是一些fping命令的实例,展示了如何使用不同的选项和参数。

实例1:fping多个IP地址

这个实例会同时fping多个IP地址,并显示它们的状态是存活还是不可达。

[linux@bashcommandnotfound.cn ~]$ fping 50.116.66.139 173.194.35.35 98.139.183.24
50.116.66.139 is alive
173.194.35.35 is unreachable
98.139.183.24 is unreachable

实例2:fping一段IP地址

这个实例会fping一个指定的IP地址段,并显示它们的状态。

[linux@bashcommandnotfound.cn ~]$ fping -s -g 192.168.0.1 192.168.0.9
192.168.0.1 is alive
192.168.0.2 is alive
ICMP Host Unreachable from 192.168.0.2 for ICMP Echo sent to 192.168.0.3
ICMP Host Unreachable from 192.168.0.2 for ICMP Echo sent to 192.168.0.3
ICMP Host Unreachable from 192.168.0.2 for ICMP Echo sent to 192.168.0.3
ICMP Host Unreachable from 192.168.0.2 for ICMP Echo sent to 192.168.0.4
192.168.0.3 is unreachable
192.168.0.4 is unreachable
  8 9 targets
  2 alive
  2 unreachable
  0 unknown addresses
  4 timeouts (waiting for response)
  9 ICMP Echos sent
  2 ICMP Echo Replies received
  2 other ICMP received
  0.10 ms (min round trip time)
  0.21 ms (avg round trip time)
  0.32 ms (max round trip time)
  4.295 sec (elapsed real time)

实例3:fping整个网络

这个实例会fping整个网络,并重复一次(-r 1)。由于输出太多,这里只显示部分。

[linux@bashcommandnotfound.cn ~]$ fping -g -r 1 192.168.0.0/24
192.168.0.1 is alive
192.168.0.2 is alive
ICMP Host Unreachable from 192.168.0.2 for ICMP Echo sent to 192.168.0.3
ICMP Host Unreachable from 192.168.0.2 for ICMP Echo sent to 192.168.0.4
...
192.168.0.254 is alive
192.168.0.255 is alive

实例4:从文件中读取目标列表

这个实例会从一个文件中读取要fping的目标列表,并显示它们的状态。假设文件名是fping.txt,内容如下:

173.194.35.35
98.139.183.24

则命令和输出如下:

[linux@bashcommandnotfound.cn ~]$ fping -f fping.txt
173.194.35.35 is unreachable
98.139.183.24 is unreachable

实例5:显示目标的地址

这个实例会显示目标的地址,而不是主机名。如果目标有多个IP地址,例如IPv4和IPv6,可以使用-m选项来显示所有的地址。

[linux@bashcommandnotfound.cn ~]$ fping -n -m google.com
google.com (172.217.160.78) is alive
google.com (2a00:1450:400a:801::200e) is alive

实例6:显示目标的地址

这个实例会显示目标的地址,而不是主机名。

[linux@bashcommandnotfound.cn ~]$ fping -n 50.116.66.139
50.116.66.139 (li466-139.members.linode.com) is alive

实例7:显示目标的回显时间

这个实例会显示目标的回显时间,单位是毫秒。

[linux@bashcommandnotfound.cn ~]$ fping -e 50.116.66.139
50.116.66.139 is alive (0.11 ms)

实例8:显示目标的回显时间和IP TTL值

这个实例会显示目标的回显时间和IP TTL值。

[linux@bashcommandnotfound.cn ~]$ fping -e -T 50.116.66.139
50.116.66.139 is alive (0.11 ms) (ttl = 51)

实例9:显示目标的回显时间和序列号

这个实例会显示目标的回显时间和序列号。

[linux@bashcommandnotfound.cn ~]$ fping -e -q 50.116.66.139
50.116.66.139 : [0], 0.11 ms (ttl=51)
50.116.66.139 : [1], 0.11 ms (ttl=51)
50.116.66.139 : [2], 0.11 ms (ttl=51)
50.116.66.139 : [3], 0.11 ms (ttl=51)
50.116.66.139 : [4], 0.11 ms (ttl=51)
50.116.66.139 : [5], 0.11 ms (ttl=51)
50.116.66.139 : [6], 0.11 ms (ttl=51)
50.116.66.139 : [7], 0.11 ms (ttl=51)
50.116.66.139 : [8], 0.11 ms (ttl=51)
50.116.66.139 : [9], 0.11 ms (ttl=51)
50.116.66.139 : xmt/rcv/%loss = 10/10/0%, min/avg/max = 0.11/0.11/0.11

实例10:显示目标的回显时间和序列号(简洁模式)

这个实例会显示目标的回显时间和序列号,但是只显示一行。

[linux@bashcommandnotfound.cn ~]$ fping -e -q -C 10 50.116.66.139
50.116.66.139 : 0.11 0.11 0.11 0.11 0.11 0.11 0.11 0.11 0.11 0.11

实例11:显示目标的回显时间和序列号(简洁模式,带统计信息)

这个实例会显示目标的回显时间和序列号,但是只显示一行,并且在最后显示统计信息。

[linux@bashcommandnotfound.cn ~]$ fping -e -q -C 10 -s 50.116.66.139
50.116.66.139 : 0.11 0.11 0.11 0.11 0.11 0.11 0.11 0.11 0.11 0.11
50.116.66.139 : xmt/rcv/%loss = 10/10/0%, min/avg/max = 0.11/0.11/0.11
  1 targets
  1 alive
  0 unreachable
  0 unknown addresses
  0 timeouts (waiting for response)
  10 ICMP Echos sent
  10 ICMP Echo Replies received
  0 other ICMP received
  0.11 ms (min round trip time)
  0.11 ms (avg round trip time)
  0.11 ms (max round trip time)
  1.005 sec (elapsed real time)

实例12:显示目标的回显时间和序列号(简洁模式,带统计信息,带时间戳)

这个实例会显示目标的回显时间和序列号,但是只显示一行,并且在最后显示统计信息,同时在每个回显时间前面加上时间戳。

[linux@bashcommandnotfound.cn ~]$ fping -e -q -C 10 -s -D 50.116.66.139
[1639921098.000000] 50.116.66.139 : 0.11 0.11 0.11 0.11 0.11 0.11 0.11 0.11 0.11 0.11
[1639921098.000000] 50.116.66.139 : xmt/rcv/%loss = 10/10/0%, min/avg/max = 0.11/0.11/0.11
  1 targets
  1 alive
  0 unreachable
  0 unknown addresses
  0 timeouts (waiting for response)
  10 ICMP Echos sent
  10 ICMP Echo Replies received
  0 other ICMP received
  0.11 ms (min round trip time)
  0.11 ms (avg round trip time)
  0.11 ms (max round trip time)
  1.005 sec (elapsed real time)

实例13:显示目标的回显时间和序列号(简洁模式,带统计信息,带时间戳,带主机名)

这个实例会显示目标的回显时间和序列号,但是只显示一行,并且在最后显示统计信息,同时在每个回显时间前面加上时间戳,还会显示主机名。

[linux@bashcommandnotfound.cn ~]$ fping -e -q -C 10 -s -D -n 50.116.66.139
[1639921098.000000] 50.116.66.139 (li466-139.members.linode.com) : 0.11 0.11 0.11 0.11 0.11 0.11 0.11 0.11 0.11 0.11
[1639921098.000000] 50.116.66.139 (li466-139.members.linode.com) : xmt/rcv/%loss = 10/10/0%, min/avg/max = 0.11/0.11/0.11
  1 targets
  1 alive
  0 unreachable
  0 unknown addresses
  0 timeouts (waiting for response)
  10 ICMP Echos sent
  10 ICMP Echo Replies received
  0 other ICMP received
  0.11 ms (min round trip time)
  0.11 ms (avg round trip time)
  0.11 ms (max round trip time)
  1.005 sec (elapsed real time)

实例14:显示目标的回显时间和序列号(简洁模式,带统计信息,带时间戳,带主机名,带IP TTL值)

这个实例会显示目标的回显时间和序列号,但是只显示一行,并且在最后显示统计信息,同时在每个回显时间前面加上时间戳,还会显示主机名和IP TTL值。

[linux@bashcommandnotfound.cn ~]$ fping -e -q -C 10 -s -D -n -T 50.116.66.139
[1639921098.000000] 50.116.66.139 (li466-139.members.linode.com) : 0.11 (ttl=51) 0.11 (ttl=51) 0.11 (ttl=51) 0.11 (ttl=51) 0.11 (ttl=51) 0.11 (ttl=51) 0.11 (ttl=51) 0.11 (ttl=51) 0.11 (ttl=51) 0.11 (ttl=51)
[1639921098.000000] 50.116.66.139 (li466-139.members.linode.com) : xmt/rcv/%loss = 10/10/0%, min/avg/max = 0.11/0.11/0.11
  1 targets
  1 alive
  0 unreachable
...

Linux fping命令的注意事项

  • fping命令需要root权限才能运行,否则会提示fping: can't create socket (must run as root?),可以使用sudo命令来提升权限,例如:sudo fping 50.116.66.139
  • fping命令可能会被防火墙或路由器拦截,导致无法收到回显应答,这时可以尝试使用-t选项来增加超时时间,或者使用-H选项来调整IP TTL值。
  • fping命令可能会对网络造成一定的负载,尤其是在fping大量的目标时,这时可以使用-p选项来调整发送ping包的间隔,或者使用-B选项来设置指数退避因子,以减少网络拥塞。
  • 如果在命令行中没有找到fping命令,可能是因为没有安装fping包,这时可以使用包管理工具来安装,例如:sudo apt install fping。如果提示bash: fping: command not found,则表示fping命令没有在PATH环境变量中,可以使用which fping来查找fping命令的位置,然后使用绝对路径来运行,例如:/usr/bin/fping 50.116.66.139

Linux fping相关命令

0

评论区