Skip to content

Instantly share code, notes, and snippets.

@BlackVikingPro
Last active October 26, 2017 01:45
Show Gist options
  • Save BlackVikingPro/3a725e754364a9c73e314fbe67b84cce to your computer and use it in GitHub Desktop.
Save BlackVikingPro/3a725e754364a9c73e314fbe67b84cce to your computer and use it in GitHub Desktop.
Check if multiple hosts are online from a list of addresses.
#!/usr/bin/env python
import os, sys, fileinput
try:
host_list = sys.argv[1]
pass
except IndexError as e:
if sys.argv[0].startswith('./'):
print "Usage: %s /path/to/list -- List that contains target IP addresses" % sys.argv[0]
sys.exit()
else:
print "Usage ./%s /path/to/list -- List that contains target IP addresses" % sys.argv[0]
sys.exit()
def ping(host):
response = os.popen("timeout 2 ping -c 1 " + host).read()
if '0% packet loss' in response:
print "Host: %s is \033[92mup!\033[0m" % host
else:
print "Host: %s is \033[91mdown\033[0m!" % host
pass
if __name__ == '__main__':
with open(host_list, "r") as tlist:
hosts = []
for host in tlist:
hosts.append(host)
pass
pass
hosts = [line.rstrip('\n') for line in open(host_list)] # erase '\n' from end of lines
hosts = filter(None, hosts) # filter blank lines
try:
print "Results may vary depending on your internet connection.\n"
for host in hosts:
ping(host)
pass
pass
except KeyboardInterrupt:
print ""
pass
@BlackVikingPro
Copy link
Author

Updated, fixed up how it checked if a server was online.
Also added a timeout so you don't have to wait 5 minutes for it to keep trying to ping a server until it finally
tells you that it is down.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment