Skip to content

CentOS7 で ping や fping を使い、ping を連続実行する

Linux 環境で pingfping を使って通信確認する際、実行回数を指定したり、無制限に(ループして)実行させたい場合があります。 今回はそういった場合のコマンドラインをメモしておきます。 テストは CentOS7 上で実施しました。

ping 編

回数指定で実行する

-c で実行回数、-i で実行間隔、-s でサイズを指定して実行します。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# ping -c 5 -i 0.1 -s 100 172.24.20.253
PING 172.24.20.253 (172.24.20.253) 100(128) bytes of data.
108 bytes from 172.24.20.253: icmp_seq=1 ttl=252 time=0.861 ms
108 bytes from 172.24.20.253: icmp_seq=2 ttl=252 time=0.858 ms
108 bytes from 172.24.20.253: icmp_seq=3 ttl=252 time=0.794 ms
108 bytes from 172.24.20.253: icmp_seq=4 ttl=252 time=0.713 ms
108 bytes from 172.24.20.253: icmp_seq=5 ttl=252 time=0.837 ms

--- 172.24.20.253 ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 400ms
rtt min/avg/max/mdev = 0.713/0.812/0.861/0.063 ms

ループ実行する

実行回数を省略するとループ実行されます。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
# ping -i 0.1 -s 100 172.24.20.253
PING 172.24.20.253 (172.24.20.253) 100(128) bytes of data.
108 bytes from 172.24.20.253: icmp_seq=1 ttl=252 time=0.767 ms
108 bytes from 172.24.20.253: icmp_seq=2 ttl=252 time=0.742 ms
108 bytes from 172.24.20.253: icmp_seq=3 ttl=252 time=0.644 ms
108 bytes from 172.24.20.253: icmp_seq=4 ttl=252 time=0.739 ms
108 bytes from 172.24.20.253: icmp_seq=5 ttl=252 time=0.769 ms
108 bytes from 172.24.20.253: icmp_seq=6 ttl=252 time=0.819 ms
108 bytes from 172.24.20.253: icmp_seq=7 ttl=252 time=3.39 ms
108 bytes from 172.24.20.253: icmp_seq=8 ttl=252 time=0.814 ms
108 bytes from 172.24.20.253: icmp_seq=9 ttl=252 time=0.776 ms
108 bytes from 172.24.20.253: icmp_seq=10 ttl=252 time=0.711 ms
^C
--- 172.24.20.253 ping statistics ---
10 packets transmitted, 10 received, 0% packet loss, time 900ms
rtt min/avg/max/mdev = 0.644/1.017/3.393/0.793 ms

fping 編

fping はデフォルトだとインストールされていません。 epel からインストールします。

インストール

yum でインストールします。

1
2
yum -y install epel-release
yum -y install fping

回数指定で実行する

-b でサイズ、-c で実行回数、-p で実行間隔を指定します。

1
2
3
4
5
6
7
8
# fping -b 100 -c 5 -p 10 172.24.20.253
172.24.20.253 : [0], 128 bytes, 0.87 ms (0.87 avg, 0% loss)
172.24.20.253 : [1], 128 bytes, 0.78 ms (0.82 avg, 0% loss)
172.24.20.253 : [2], 128 bytes, 0.74 ms (0.79 avg, 0% loss)
172.24.20.253 : [3], 128 bytes, 0.81 ms (0.80 avg, 0% loss)
172.24.20.253 : [4], 128 bytes, 0.86 ms (0.81 avg, 0% loss)

172.24.20.253 : xmt/rcv/%loss = 5/5/0%, min/avg/max = 0.74/0.81/0.87

ループ実行する

-l を指定するとループ実行されます。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# fping -b 100 -l -p 10 172.24.20.253
172.24.20.253 : [0], 128 bytes, 0.88 ms (0.88 avg, 0% loss)
172.24.20.253 : [1], 128 bytes, 0.82 ms (0.85 avg, 0% loss)
172.24.20.253 : [2], 128 bytes, 0.70 ms (0.80 avg, 0% loss)
172.24.20.253 : [3], 128 bytes, 0.72 ms (0.78 avg, 0% loss)
172.24.20.253 : [4], 128 bytes, 0.78 ms (0.78 avg, 0% loss)
172.24.20.253 : [5], 128 bytes, 0.82 ms (0.78 avg, 0% loss)
172.24.20.253 : [6], 128 bytes, 0.75 ms (0.78 avg, 0% loss)
172.24.20.253 : [7], 128 bytes, 0.79 ms (0.78 avg, 0% loss)
172.24.20.253 : [8], 128 bytes, 0.78 ms (0.78 avg, 0% loss)
172.24.20.253 : [9], 128 bytes, 0.72 ms (0.77 avg, 0% loss)
172.24.20.253 : [10], 128 bytes, 0.71 ms (0.77 avg, 0% loss)
^C
172.24.20.253 : xmt/rcv/%loss = 11/11/0%, min/avg/max = 0.70/0.77/0.88

ヘルプ

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# 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 56)
   -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
   -D         print timestamp before each output line
   -e         show elapsed time on return packets
   -f file    read list of targets from a file ( - means stdin) (only if no -g specified)
   -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)
   -H n       Set the IP TTL value (Time To Live hops)
   -i n       interval between sending ping packets (in millisec) (default 25)
   -I if      bind to a particular interface
   -l         loop sending pings forever
   -m         ping multiple interfaces on target host
   -n         show targets by name (-d is equivalent)
   -O n       set the type of service (tos) flag on the ICMP packets
   -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       ignored (for compatibility with fping 2.4)
   -u         show targets that are unreachable
   -v         show version
   targets    list of targets to check (if no -f specified)