User Tools

Site Tools


burim:python:script_ping_gw
 import subprocess, platform
 
 iproutegw = subprocess.Popen('ip route | grep default', shell=True, stdout=subprocess.PIPE)
 
 gw = str(iproutegw.stdout.read()).split()
 ipgw=str(gw[2])
 
 #print(ipgw)
 #print(type(ipgw))
 
 def ping(host):
     """
     Returns True if host responds to a ping request
     """
     import subprocess, platform
 
     # Ping parameters as function of OS
     ping_str = "-n 1" if  platform.system().lower()=="windows" else "-c 5"
     args = "ping " + " " + ping_str + " " + host
     need_sh = False if  platform.system().lower()=="windows" else True
 
     # Ping
     return subprocess.call(args, shell=need_sh) == 0
 
 # test call
 print(ping(ipgw))
 
burim/python/script_ping_gw.txt · Last modified: 2019/01/28 10:00 by burim

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki