Help us understand the problem. What is going on with this article?

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

More than 1 year has passed since last update.

結論

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.Popen(["ping", "-w", "3", "-c", "1", host], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
    ping.communicate()
    return ping.returncode == 0

参照

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

hamemi
neosystem
「通信業・メディア・ネットベンダー向けシステム」「製造業向けシステム」「金融業向けシステム」「教育機関・企業教育向けシステム」等の開発を手掛ける、独立系の総合SIerです。
http://www.neosystem.co.jp
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした