首页 > 代码库 > fping 多线程

fping 多线程

    最近在和同事们聊天,他们说自己在写一个ping多个IP地址读取状态的情况。想多线程,比较麻烦。后来我给他说了一个软件,fping,简单。

   1. 在centos/redhat上yum直接安装

      yum install -y fping

   2. 在ubuntu上apt-get直接安装

      apt-get install -y fping


以下是执行过程:

 

[root@salt-master caimz]# cat /etc/redhat-release

CentOS release 6.8 (Final)

[root@salt-master caimz]# uname -a

Linux salt-master 2.6.32-642.6.1.el6.x86_64 #1 SMP Wed Oct 5 00:36:12 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux

[root@salt-master caimz]# yum install -y fping

Loaded plugins: fastestmirror, refresh-packagekit, security

Setting up Install Process

Loading mirror speeds from cached hostfile

 * base: mirrors.aliyun.com

 * epel: mirrors.aliyun.com

 * extras: mirrors.aliyun.com

 * updates: mirrors.aliyun.com

Resolving Dependencies

--> Running transaction check

---> Package fping.x86_64 0:2.4b2-10.el6 will be updated

---> Package fping.x86_64 0:2.4b2-16.el6 will be an update

--> Finished Dependency Resolution


Dependencies Resolved


=====================================================================================================================================

 Package                  Arch                      Version                            Repository                               Size

=====================================================================================================================================

Updating:

 fping                    x86_64                    2.4b2-16.el6                       zabbix-non-supported                     31 k


Transaction Summary

=====================================================================================================================================

Upgrade       1 Package(s)


Total download size: 31 k

Downloading Packages:

fping-2.4b2-16.el6.x86_64.rpm                                                                                 |  31 kB     00:02

Running rpm_check_debug

Running Transaction Test

Transaction Test Succeeded

Running Transaction

Warning: RPMDB altered outside of yum.

  Updating   : fping-2.4b2-16.el6.x86_64                                                                                         1/2

  Cleanup    : fping-2.4b2-10.el6.x86_64                                                                                         2/2

  Verifying  : fping-2.4b2-16.el6.x86_64                                                                                         1/2

  Verifying  : fping-2.4b2-10.el6.x86_64                                                                                         2/2


Updated:

  fping.x86_64 0:2.4b2-16.el6


Complete!

到这软件安装结束。接下来就是如何使用了。

  1. 先查看一下软件有哪些参数


   

[root@salt-master caimz]# fping -h


Usage: fping [options] [targets...]

   -a         show targets that are alive   

   -A         show targets by address

   -b n       amount of ping data to send, in bytes (default 68)

   -B f       set exponential backoff factor to f

   -c n       count of pings to send to each target (default 1)

   -C n       same as -c, report results in verbose format

   -e         show elapsed time on return packets

   -f file    read list of targets from a file ( - means stdin) (only if no -g specified)

   # -f 后面就是去读取的文件,我放了ip列表,如下例子。


   -g         generate target list (only if no -f specified)

                (specify the start and end IP in the target list, or supply a IP netmask)

                (ex. fping -g 192.168.1.0 192.168.1.255 or fping -g 192.168.1.0/24)

   -i n       interval between sending ping packets (in millisec) (default 25)

   -l         loop sending pings forever

   -m         ping multiple interfaces on target host

   -n         show targets by name (-d is equivalent)

   -p n       interval between ping packets to one target (in millisec)

                (in looping and counting modes, default 1000)

   -q         quiet (don‘t show per-target/per-ping results)

   -Q n       same as -q, but show summary every n seconds

   -r n       number of retries (default 3)

   -s         print final stats

   -S addr    set source address

   -t n       individual target initial timeout (in millisec) (default 500)

   -T n       set select timeout (default 10)

   -u         show targets that are unreachable

   -v         show version

   targets    list of targets to check (if no -f specified)

以下是翻译;

   -a         show targets that are alive    显示可ping通的目标
   -A         show targets by address       将目标以ip地址的形式显示
   -b n       amount of ping data to send, in bytes (default 56)   ping 数据包的大小。(默认为56)
   -B f       set exponential backoff factor to f  设置指数反馈因子到f 
   -c n       count of pings to send to each target (default 1) ping每个目标的次数 (默认为1)
   -C n       same as -c, report results in verbose format 同-c, 返回的结果为冗长格式
   -e         show elapsed time on return packets 显示返回数据包所费时间
   -f file    read list of targets from a file ( - means stdin) (only if no -g specified)
               从文件获取目标列表( - 表示从标准输入)(不能与 -g 同时使用)
   -g         generate target list (only if no -f specified)
               生成目标列表(不能与 -f 同时使用)
                (specify the start and end IP in the target list, or supply a IP netmask)
                (ex. fping -g 192.168.1.0 192.168.1.255 or fping -g 192.168.1.0/24)
                (可指定目标的开始和结束IP, 或者提供ip的子网掩码)
                (例:fping -g 192.168.1.0 192.168.1.255 或 fping -g 192.168.1.0/24)
   -H n       Set the IP TTL value (Time To Live hops)
                设置ip的TTL值 (生存时间)
   -i n       interval between sending ping packets (in millisec) (default 25)
               ping包之间的间隔(单位:毫秒)(默认25)
   -l         loop sending pings forever
              循环发送ping
   -m         ping multiple interfaces on target host
                ping目标主机的多个网口
   -n         show targets by name (-d is equivalent)
                将目标以主机名或域名显示(等价于 -d )
   -p n       interval between ping packets to one target (in millisec)
                对同一个目标的ping包间隔(毫秒)
                (in looping and counting modes, default 1000)
                (在循环和统计模式中,默认为1000)
   -q         quiet (don‘t show per-target/per-ping results) 安静模式(不显示每个目标或每个ping的结果)
   -Q n       same as -q, but show summary every n seconds 同-q, 但是每n秒显示信息概要
   -r n       number of retries (default 3)  当ping失败时,最大重试次数(默认为3次)
   -s         print final stats 打印最后的统计数据
   -I if      bind to a particular interface 绑定到特定的网卡
   -S addr    set source address  设置源ip地址
   -t n       individual target initial timeout (in millisec) (default 500)
               单个目标的超时时间(毫秒)(默认500)
   -T n       ignored (for compatibility with fping 2.4)  请忽略(为兼容fping 2.4)
   -u         show targets that are unreachable 显示不可到达的目标
   -O n       set the type of service (tos) flag on the ICMP packets  在icmp包中设置tos(服务类型)
   -v         show version  显示版本号
   targets    list of targets to check (if no -f specified)
                需要ping的目标列表(不能和 -f 同时使用)
-h              show this page 显示本帮助页

 2.开始测试

[root@salt-master caimz]# cat iplist

192.168.1.30

192.168.1.31

192.168.1.32

192.168.1.33

192.168.1.34

192.168.1.4

192.168.1.51

192.168.1.52

192.168.1.53

192.168.1.54

192.168.1.55

192.168.1.6

192.168.1.7


[root@salt-master caimz]# fping -f iplist

192.168.1.30 is alive

192.168.1.31 is alive

192.168.1.32 is alive

192.168.1.33 is alive

192.168.1.34 is alive

192.168.1.4 is alive

192.168.1.51 is alive

192.168.1.52 is alive

192.168.1.53 is alive

192.168.1.54 is alive

192.168.1.55 is alive

192.168.1.6 is alive

192.168.1.7 is alive

一下子就全部返回了主机ip的在线存活状态。

fping 多线程