This python code will do it!
import subprocess import re ''' Function to check the availability of the network computer. Support computer on Windows host - name or ip remote computer (default 127.0.0.1) n - number of requests sent to the echo (default 3) ''' def is_ping(host='127.0.0.1', n='3'): # call Windows utility Ping ping = subprocess.Popen(["ping", "-n", n, host], stdout=subprocess.PIPE, stderr=subprocess.PIPE) # get echo answer from remote computer out, error = ping.communicate() # decode string in utf-8 msg = out.decode('utf-8') # availability status is True if re.findall("Reply from", msg): l = msg.split('\r\n') print(l) # get last availability status for el in l: if re.findall("Reply from", el): state = el print(el) # last availability status is True if re.findall("Reply from \d+.\d+.\d+.\d+: bytes=\d+ time<\d+ms TTL=\d+", state): print('*** {0} is available ***'.format(host)) return True # last availability status is False else: print('*** {0} is unavailable ***'.format(host)) return False # availability status is False else: print('*** {0} is unavailable ***'.format(host)) return False
For example, save is_ping in file pinger.py and calling in python console:
from pinger import is_ping is_ping('192.168.1.10', 4) ---------------------------------------- Reply from 192.168.1.10: bytes=32 time<1ms TTL=128 Reply from 192.168.1.10: bytes=32 time<1ms TTL=128 Reply from 192.168.1.10: bytes=32 time<1ms TTL=128 Reply from 192.168.1.10: bytes=32 time<1ms TTL=128 *** 192.168.1.10 is available *** True
Реклама