Thursday, December 10, 2009

Simple Python ping method

def ping(host):
    result = subprocess.call(["ping","-c","1",host],stdout=subprocess.PIPE,stderr=subprocess.PIPE)
    if result == 0:
        return True
    elif result == 1:
        raise Exception('Host not found')
    elif result == 2:
        raise Exception('Ping timed out')
useage:
try:
    ping('192.168.100.100')
except Exception:
    print "Ping error"
    raise

No comments: