LoginSignup
2
1

More than 1 year has passed since last update.

ping疎通確認結果を機械的に判定する

Last updated at Posted at 2018-03-27

結論

pingコマンドの終了ステータスが0であれば疎通成功、と判定しよう

コマンド

ping (対象ホスト) -w (タイムアウト時間(秒)) -c 1

ping オプション解説

  • -w
    • タイムアウト時間
    • 指定した時間を超えると、pingレスポンスを取得できていなくても強制的に終了する
  • -c
    • 指定した回数レスポンスを取得できたらok
    • 1を指定したからといって1回しかpingが送信されないのではない
    • pingリクエスト自体は基本1秒に1回送られている(別オプションで間隔の調整は可能)

Pythonバージョン

#! /usr/bin/python
import subprocess
def is_connectable(host):
    ping = subprocess.run(["ping", "-w", "1", "-c", "1", host], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
    return ping.returncode == 0

参照

PythonでOSのPingコマンドを叩いて死活監視する - hadacchi blog
[Python] Pingコマンド実行 : Edo::World:: cctv Blog

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1