import os hostname = "google.com" #example response = os.system("ping -c 1 " + hostname) #and then check the response... if response == 0: print hostname, 'is up!' else: print hostname, 'is down!'
import subprocess ''' servers.txt contains ip address in following format 192.168.1.1 192.168.1.2 ''' hosts = open('servers.txt', 'r') hosts = hosts.read() hosts = hosts.strip().splitlines() for ip in hosts: result = subprocess.Popen(["ping", "-c", "1", "-n", "-W", "2", ip], stdout=hosts, stderr=hosts).wait() if result: print(ip, "inactive") else: print(ip, "active")
联系客服