はじめに#
こんにちは!今回はpingコマンドについて解説します。
pingはネットワーク越しのホストに対して「生きてますか?」と確認するコマンドです。ネットワーク接続が正常か、サーバーが応答しているか確認するときに、めっちゃよく使います。
「呼んでみて、返事があるか確認」みたいな感じですね。ネットワーク管理者の基本技です。
pingコマンドとは#
pingは、リモートホストへのICMPエコーリクエストを送信し、応答を待つ外部コマンドです。“Packet Internet Groper"の略ですね。
ネットワーク接続の疎通確認や、ホストの生死確認、レイテンシ(遅延)の測定などができます。トラブルシューティングの最初のステップとして使うことが多いですね。
基本構文#
1
|
ping [オプション] ホスト名またはIPアドレス
|
デフォルトではCtrl+Cで停止するまで無限にpingを送信し続けます。
主なオプション#
| オプション |
説明 |
-c 数 |
指定した回数だけpingを送信(Linuxの場合) |
-i 秒 |
pingを送信する間隔(秒単位) |
-s バイト |
送信するデータサイズ |
-t TTL |
TTL値を指定 |
-w 秒 |
タイムアウト時間(ミリ秒) |
-W 秒 |
応答を待つ時間 |
使用例#
例1: 基本的なping#
実行結果:
1
2
3
4
5
6
7
8
9
|
PING google.com (142.250.185.46) 56(84) bytes of data.
64 bytes from 142.250.185.46: icmp_seq=1 ttl=119 time=18.5 ms
64 bytes from 142.250.185.46: icmp_seq=2 ttl=119 time=17.8 ms
64 bytes from 142.250.185.46: icmp_seq=3 ttl=119 time=18.2 ms
64 bytes from 142.250.185.46: icmp_seq=4 ttl=119 time=17.9 ms
--- google.com statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3007ms
rtt min/avg/max/stddev = 17.8/18.1/18.5/0.3 ms
|
google.comに4回pingを送信します。応答時間がミリ秒単位で表示されます。
例2: ローカルサーバーへのping#
1
|
ping -c 3 192.168.1.100
|
実行結果:
1
2
3
4
5
6
7
8
|
PING 192.168.1.100 (192.168.1.100) 56(84) bytes of data.
64 bytes from 192.168.1.100: icmp_seq=1 ttl=64 time=1.2 ms
64 bytes from 192.168.1.100: icmp_seq=2 ttl=64 time=1.1 ms
64 bytes from 192.168.1.100: icmp_seq=3 ttl=64 time=1.3 ms
--- 192.168.1.100 statistics ---
3 packets transmitted, 3 received, 0%packet loss, time 2001ms
rtt min/avg/max/stddev = 1.1/1.2/1.3/0.1 ms
|
ローカルネットワーク内のIPアドレスにping。応答が速いですね。
例3: ホスト名でのping#
実行結果:
1
2
3
4
5
6
7
|
PING example.com (93.184.216.34) 56(84) bytes of data.
64 bytes from 93.184.216.34: icmp_seq=1 ttl=56 time=75.3 ms
64 bytes from 93.184.216.34: icmp_seq=2 ttl=56 time=74.8 ms
--- example.com statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1005ms
rtt min/avg/max/stddev = 74.8/75.0/75.3/0.2 ms
|
ホスト名を指定してping。DNSで自動的にIPアドレスに解決されます。
例4: pingの間隔を指定#
1
|
ping -c 5 -i 2 example.com
|
実行結果:
2秒間隔でpingを送信。デフォルトは1秒です。
例5: より大きなデータサイズで送信#
1
|
ping -c 3 -s 1000 example.com
|
実行結果:
1
2
3
|
PING example.com (93.184.216.34) 1000(1028) bytes of data.
1008 bytes from 93.184.216.34: icmp_seq=1 ttl=56 time=75.4 ms
...
|
1000バイトのデータを送信。デフォルトは56バイトです。
例6: 応答タイムアウトを指定#
1
|
ping -c 3 -W 2000 slowhost.example.com
|
実行結果:
応答を2秒間待機。デフォルトは無期限です。
例7: TTLを指定して送信#
1
|
ping -c 4 -t 32 distant-server.example.com
|
実行結果:
TTL値を32に設定。TTLは経由するルーター数で減少します。
例8: 継続的なping監視#
1
|
ping example.com | grep -oP '(?<=time=).*?(?= ms)'
|
実行結果:
1
2
3
|
75.3 ms
74.8 ms
...
|
pingの応答時間だけを抽出して表示。スクリプトで処理するときに便利。
Tips・注意点#
パケットロスの確認#
10回以上ping送信して、パケットロス率を確認することが重要です。0%なら良好、1%以上なら不安定。
DNSの疎通確認#
1
2
|
ping -c 1 google.com # ホスト名で確認
ping -c 1 8.8.8.8 # IPアドレスで確認
|
ホスト名でpingが通らず、IPアドレスで通る場合はDNS問題の可能性。
ファイアウォール設定#
1
2
3
|
# 一部のシステムではICMPをブロックしている
ping example.com
# "Destination unreachable"が返ってくる場合はファイアウォール設定を確認
|
企業ネットワークではセキュリティのためpingを禁止していることもあります。
実践的な使い方#
ネットワーク接続の診断#
1
2
3
|
ping -c 4 gateway.example.com # ゲートウェイ確認
ping -c 4 8.8.8.8 # インターネット接続確認
ping -c 4 example.com # DNS確認
|
段階的に接続状態を診断。問題箇所を特定しやすくなります。
サーバーの生死確認スクリプト#
1
2
3
4
5
6
7
8
9
|
#!/bin/bash
SERVERS=("192.168.1.10" "192.168.1.20" "192.168.1.30")
for server in "${SERVERS[@]}"; do
if ping -c 1 -W 2 "$server" > /dev/null 2>&1; then
echo "$server is UP"
else
echo "$server is DOWN"
fi
done
|
複数サーバーの稼働状況を確認するスクリプト。
レイテンシ監視#
1
|
ping -c 100 example.com | grep avg | awk '{print $4}' | cut -d'/' -f2
|
100回のpingから平均応答時間だけを抽出。パフォーマンス監視に利用できます。
継続的なping監視(タイムスタンプ付き)#
1
|
ping example.com | while IFS= read -r line; do echo "$(date '+%Y-%m-%d %H:%M:%S') $line"; done
|
pingの出力にタイムスタンプを追加。トラブル発生時刻の特定に便利です。
まとめ#
pingコマンドのポイント:
- リモートホストの疎通確認を行う基本コマンド
- -c 数: 指定回数だけpingを送信
- -i 秒: ping送信の間隔を指定
- -s バイト: 送信データサイズを指定
- -W ミリ秒: 応答待機時間を指定
- パケットロス率で接続品質を判断
- よく使う組み合わせ:
ping -c 4, ping -c 1 -W 2
ネットワークトラブル時は、まずpingで疎通確認。これが基本です。応答時間が大きいと遅延が大きい証拠。ネットワーク管理者必須のコマンドですね!