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))