【Ubuntu】一度に複数の宛先にpingを実行できるfpingの紹介

一度の複数のホスト宛にpingを実行できるfpingを紹介します。

fpingインストール

まず最初にaptを使ってfpingをインストールします。

$ sudo apt update
$ sudo apt install fping

fpingの使い方

fpingには数多くのオプションがありますが、私が普段よく使う下記を紹介します。

  • 基本の使い方: fping 宛先ホスト(IP or ホスト名)
  • 複数指定: fping 宛先ホスト1 宛先ホスト2
  • 範囲指定: fping -g 開始IP 最終IP
  • ネットワーク指定: fping -g 192.168.1.0/24
  • ファイルから読み込み:
    • fping -f ファイル
    • fping < ファイル
    • cat ファイル | fping
  • 回数指定: fping -c 送信回数 宛先ホスト
  • pingを送信し続ける: fping -l 宛先ホスト
  • 間隔指定: fping -i 送信間隔 宛先ホスト
  • ICMP Host Unreachableを表示させたくない: fping 宛先ホスト 2> /dev/null
  • aliveなホストのみ表示させたい:
    • fping -a 宛先ホスト
    • fping 宛先ホスト 2> /dev/null | grep alive
  • unreachableホストのみ表示させたい:
    • fping -u 宛先ホスト
    • fping 宛先ホスト 2> /dev/null | grep unreachable
  • 結果を順番に並べ替えたい: fping 宛先ホスト | sort
  • 統計情報表示: fping -s 宛先ホスト

基本の使い方: fping 宛先ホスト(IP or ホスト名)

fpingの後にpingを送信する相手先を、IPまたはホスト名で指定します。

pingによる疎通確認が取れたらaliveと表示されます。

$ fping 192.168.1.1                 
192.168.1.1 is alive
$ fping vpslife.server-memo.net                    
vpslife.server-memo.net is alive

疎通の確認が取れなかった場合はunreachableと表示されます。

$ fping 192.168.1.2                          
ICMP Host Unreachable from 192.168.1.254 for ICMP Echo sent to 192.168.1.2
ICMP Host Unreachable from 192.168.1.254 for ICMP Echo sent to 192.168.1.2
ICMP Host Unreachable from 192.168.1.254 for ICMP Echo sent to 192.168.1.2
ICMP Host Unreachable from 192.168.1.254 for ICMP Echo sent to 192.168.1.2
192.168.1.2 is unreachable

複数指定: fping 宛先ホスト1 宛先ホスト2

スペースで区切ることで宛先を複数指定することができます。

$ fping 192.168.1.1 192.168.1.20 192.168.1.30
192.168.1.1 is alive
192.168.1.20 is alive
192.168.1.30 is alive

ホスト名でも複数指定することが出来ます。

$ fping vpslife.server-memo.net www.server-memo.net
vpslife.server-memo.net is alive
www.server-memo.net is alive

範囲指定: fping -g 開始IP 最終IP

-g オプションを使うことで、指定した範囲のIPアドレスに対してPingを送信することが出来ます。

例えば、192.168.1.10から192.168.1.15の範囲でpingを送信する場合は、下記のように実行します。

$ fping -g 192.168.1.10 192.168.1.15
192.168.1.11 is alive
192.168.1.14 is alive
192.168.1.15 is alive
192.168.1.12 is alive
ICMP Host Unreachable from 192.168.1.254 for ICMP Echo sent to 192.168.1.13
ICMP Host Unreachable from 192.168.1.254 for ICMP Echo sent to 192.168.1.13
ICMP Host Unreachable from 192.168.1.254 for ICMP Echo sent to 192.168.1.13
ICMP Host Unreachable from 192.168.1.254 for ICMP Echo sent to 192.168.1.13
192.168.1.10 is unreachable
192.168.1.13 is unreachable

ネットワーク指定: fping -g 192.168.1.0/24

-gオプションを使用すると、ネットワーク単位でpingを送信する範囲を指定することも出来ます。

192.168.1.0/30を指定する場合は下記のように指定します。

「192.168.1.0/30」の場合192.168.1.0〜192.168.1.3が範囲となります。

最初の192.168.1.0はネットワークアドレスで、最後の192.168.1.3はブロードキャストアドレスになるため、実際にpingが送信されるのは「192.168.1.1」と「192.168.1.2」になります。

$ fping -g 192.168.1.0/30
192.168.1.1 is alive
ICMP Host Unreachable from 192.168.1.254 for ICMP Echo sent to 192.168.1.2
ICMP Host Unreachable from 192.168.1.254 for ICMP Echo sent to 192.168.1.2
ICMP Host Unreachable from 192.168.1.254 for ICMP Echo sent to 192.168.1.2
ICMP Host Unreachable from 192.168.1.254 for ICMP Echo sent to 192.168.1.2
192.168.1.2 is unreachable

ファイルから読み込み:

fping -f ファイル

-f オプションを使用すると、ファイル読み込んでfpingを実行することが出来ます。

下記のように、pingを送信したい宛先を記述したファイルを作成します。

$ cat ping_list.txt 
192.168.1.10
192.168.1.11
192.168.1.12
192.168.1.13
192.168.1.14
192.168.1.15

-fオプションで対象のファイルを読み込んでfpingを実行すると、ファイルに記載されている宛先に対してpingを送信することが出来ます。

$ fping -f ping_list.txt 
192.168.1.11 is alive
192.168.1.15 is alive
192.168.1.12 is alive
ICMP Host Unreachable from 192.168.1.254 for ICMP Echo sent to 192.168.1.10
ICMP Host Unreachable from 192.168.1.254 for ICMP Echo sent to 192.168.1.10
ICMP Host Unreachable from 192.168.1.254 for ICMP Echo sent to 192.168.1.10
ICMP Host Unreachable from 192.168.1.254 for ICMP Echo sent to 192.168.1.10
ICMP Host Unreachable from 192.168.1.254 for ICMP Echo sent to 192.168.1.14
ICMP Host Unreachable from 192.168.1.254 for ICMP Echo sent to 192.168.1.14
ICMP Host Unreachable from 192.168.1.254 for ICMP Echo sent to 192.168.1.14
ICMP Host Unreachable from 192.168.1.254 for ICMP Echo sent to 192.168.1.14
192.168.1.10 is unreachable
192.168.1.13 is unreachable
192.168.1.14 is unreachable

fping < ファイル

「<」でファイルをfpingに渡すことも出来ます。

$ fping < ping_list.txt 
192.168.1.11 is alive
192.168.1.12 is alive
192.168.1.15 is alive
ICMP Host Unreachable from 192.168.1.254 for ICMP Echo sent to 192.168.1.10
ICMP Host Unreachable from 192.168.1.254 for ICMP Echo sent to 192.168.1.10
ICMP Host Unreachable from 192.168.1.254 for ICMP Echo sent to 192.168.1.10
ICMP Host Unreachable from 192.168.1.254 for ICMP Echo sent to 192.168.1.10
ICMP Host Unreachable from 192.168.1.254 for ICMP Echo sent to 192.168.1.14
ICMP Host Unreachable from 192.168.1.254 for ICMP Echo sent to 192.168.1.14
ICMP Host Unreachable from 192.168.1.254 for ICMP Echo sent to 192.168.1.14
ICMP Host Unreachable from 192.168.1.254 for ICMP Echo sent to 192.168.1.14
192.168.1.10 is unreachable
192.168.1.13 is unreachable
192.168.1.14 is unreachable

cat ファイル | fping

catで表示させたファイルの内容をfpingに渡すという事もできます。

$ cat ping_list.txt | fping
192.168.1.11 is alive
192.168.1.12 is alive
192.168.1.15 is alive
ICMP Host Unreachable from 192.168.1.254 for ICMP Echo sent to 192.168.1.10
ICMP Host Unreachable from 192.168.1.254 for ICMP Echo sent to 192.168.1.10
ICMP Host Unreachable from 192.168.1.254 for ICMP Echo sent to 192.168.1.10
ICMP Host Unreachable from 192.168.1.254 for ICMP Echo sent to 192.168.1.10
ICMP Host Unreachable from 192.168.1.254 for ICMP Echo sent to 192.168.1.14
ICMP Host Unreachable from 192.168.1.254 for ICMP Echo sent to 192.168.1.14
ICMP Host Unreachable from 192.168.1.254 for ICMP Echo sent to 192.168.1.14
ICMP Host Unreachable from 192.168.1.254 for ICMP Echo sent to 192.168.1.14
192.168.1.10 is unreachable
192.168.1.13 is unreachable
192.168.1.14 is unreachable

回数指定: fping -c 送信回数 宛先ホスト

-c オプションを使用すると、pingを送信する回数を指定することが出来ます。

pingを3回送信したい場合は、下記のようにfpingを実行します。

$ fping -c 3 192.168.1.1
192.168.1.1 : [0], 64 bytes, 6.41 ms (6.41 avg, 0% loss)
192.168.1.1 : [1], 64 bytes, 19.2 ms (12.8 avg, 0% loss)
192.168.1.1 : [2], 64 bytes, 7.62 ms (11.1 avg, 0% loss)

192.168.1.1 : xmt/rcv/%loss = 3/3/0%, min/avg/max = 6.41/11.1/19.2

pingを送信し続ける: fping -l 宛先ホスト

pingを送信し続けたい場合は-lオプションを使用します。

「Ctrl + C」でpingの送信を停止することが出来ます。

$ fping -l 192.168.1.1
192.168.1.1 : [0], 64 bytes, 8.48 ms (8.48 avg, 0% loss)
192.168.1.1 : [1], 64 bytes, 9.15 ms (8.82 avg, 0% loss)
192.168.1.1 : [2], 64 bytes, 12.1 ms (9.91 avg, 0% loss)
192.168.1.1 : [3], 64 bytes, 9.93 ms (9.92 avg, 0% loss)
192.168.1.1 : [4], 64 bytes, 7.79 ms (9.49 avg, 0% loss)
^C  # 「Ctrl + C」でpingを停止しています
192.168.1.1 : xmt/rcv/%loss = 5/5/0%, min/avg/max = 7.79/9.49/12.1

間隔指定: fping -i 送信間隔(ミリ秒) 宛先ホスト

-i オプションを使用すると、pingを送信する間隔を指定することが出来ます。

指定する時間の単位はミリ秒なので、5秒間隔ならば5000と指定します。

pingを送信した時刻を表示させる-Dオプションと合わせて、5秒間隔(-i 5000)で3回(-c 3)pingを送信させてみます。

$ fping -D -c 3 -i 5000 192.168.1.1
[1718115170.07713] 192.168.1.1 : [0], 64 bytes, 15.5 ms (15.5 avg, 0% loss)
[1718115175.06823] 192.168.1.1 : [1], 64 bytes, 5.41 ms (10.4 avg, 0% loss)
[1718115180.08049] 192.168.1.1 : [2], 64 bytes, 16.5 ms (12.5 avg, 0% loss)

192.168.1.1 : xmt/rcv/%loss = 3/3/0%, min/avg/max = 5.41/12.5/16.5

-D オプションで表示される時刻はUNIX時間(「19701月1日午前0時(UTC)」から経過した秒数)なので、わかりやすいように変換してみると、実際に5秒間隔でpingが送信されていることがわかります。

$ date --date "@1718115170"
Tue Jun 11 23:12:50 JST 2024
$ date --date "@1718115175"
Tue Jun 11 23:12:55 JST 2024
$ date --date "@1718115180"
Tue Jun 11 23:13:00 JST 2024

ICMP Host Unreachableを表示させたくない: fping 宛先 2> /dev/null

pingが失敗した際に表示される「ICMP Host Unreachable 〜」というメッセージを表示させたくない場合の方法です。

$ fping -g 192.168.1.10 192.168.1.15
192.168.1.11 is alive
192.168.1.14 is alive
192.168.1.15 is alive
192.168.1.12 is alive
ICMP Host Unreachable from 192.168.1.254 for ICMP Echo sent to 192.168.1.13
ICMP Host Unreachable from 192.168.1.254 for ICMP Echo sent to 192.168.1.13
ICMP Host Unreachable from 192.168.1.254 for ICMP Echo sent to 192.168.1.13
ICMP Host Unreachable from 192.168.1.254 for ICMP Echo sent to 192.168.1.13
192.168.1.10 is unreachable
192.168.1.13 is unreachable

「ICMP Host Unreachable 〜」は標準エラー出力なので、「2> /dev/null」を追加してエラー出力を/dev/nullに送ることでメッセージの表示を抑えることが出来ます。

「2」と「>」の間にはスペースを入れないようにしてください。

$ fping -g 192.168.1.10 192.168.1.15 2> /dev/null
192.168.1.11 is alive
192.168.1.14 is alive
192.168.1.15 is alive
192.168.1.12 is alive
192.168.1.10 is unreachable
192.168.1.13 is unreachable

aliveなホストのみ表示させたい

fping -a 宛先

-aオプションを使用すると、aliveなホストのみを表示することが出来ます。

下記の例では「ICMP Host Unreachable 〜」のメッセージを/dev/nullに送って表示させないようにしています。

$ fping -a -g 192.168.1.10 192.168.1.15 2> /dev/null
192.168.1.11
192.168.1.15
192.168.1.12

fping 宛先 2> /dev/null | grep alive

aliveのメッセージも合わせて表示させたい場合は、fpingの結果をgrepに渡すことでaliveのみ表示させることも出来ます。

$ fping -g 192.168.1.10 192.168.1.15 2> /dev/null | grep alive
192.168.1.11 is alive
192.168.1.12 is alive
192.168.1.15 is alive

unreachableだけ表示させたい

fping -u 宛先

-uオプションを使用すると、unreachableなホストのみを表示することが出来ます。

$ fping -u -g 192.168.1.10 192.168.1.15 2> /dev/null
192.168.1.10
192.168.1.13
192.168.1.14

fping 宛先 2> /dev/null | grep unreachable

unreachableのメッセージも合わせて表示させたい場合は、fpingの結果をgrepに渡すことでunreachableのみ表示させることも出来ます。

$ fping -g 192.168.1.10 192.168.1.15 2> /dev/null | grep unreachable
192.168.1.10 is unreachable
192.168.1.13 is unreachable
192.168.1.14 is unreachable

結果を順番に並べ替えたい: fping 宛先 | sort

fpingの実行結果をsortに渡してあげると、結果をアドレス順に並べ替えて表示することが出来ます。

$ fping -g 192.168.1.10 192.168.1.15 2> /dev/null | sort
192.168.1.10 is unreachable
192.168.1.11 is alive
192.168.1.12 is alive
192.168.1.13 is unreachable
192.168.1.14 is unreachable
192.168.1.15 is alive

統計情報表示: fping -s 宛先ホスト

-sオプションを使用すると、実行結果の統計情報を最後に表示してくれます。

$ fping -g -s 192.168.1.10 192.168.1.15     
192.168.1.11 is alive
192.168.1.12 is alive
192.168.1.15 is alive
ICMP Host Unreachable from 192.168.1.254 for ICMP Echo sent to 192.168.1.10
ICMP Host Unreachable from 192.168.1.254 for ICMP Echo sent to 192.168.1.10
ICMP Host Unreachable from 192.168.1.254 for ICMP Echo sent to 192.168.1.10
ICMP Host Unreachable from 192.168.1.254 for ICMP Echo sent to 192.168.1.10
ICMP Host Unreachable from 192.168.1.254 for ICMP Echo sent to 192.168.1.14
ICMP Host Unreachable from 192.168.1.254 for ICMP Echo sent to 192.168.1.14
ICMP Host Unreachable from 192.168.1.254 for ICMP Echo sent to 192.168.1.14
ICMP Host Unreachable from 192.168.1.254 for ICMP Echo sent to 192.168.1.14
192.168.1.10 is unreachable
192.168.1.13 is unreachable
192.168.1.14 is unreachable

       6 targets
       3 alive
       3 unreachable
       0 unknown addresses

      12 timeouts (waiting for response)
      15 ICMP Echos sent
       3 ICMP Echo Replies received
       8 other ICMP received

 4.16 ms (min round trip time)
 5.25 ms (avg round trip time)
 5.87 ms (max round trip time)
        4.103 sec (elapsed real time)

コメント

タイトルとURLをコピーしました