Skip to content

Instantly share code, notes, and snippets.

@michaelkrieg
Created January 4, 2016 17:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michaelkrieg/9b6b6e15c5bd070b5191 to your computer and use it in GitHub Desktop.
Save michaelkrieg/9b6b6e15c5bd070b5191 to your computer and use it in GitHub Desktop.
ping a CIDR network and find alive and not alive hosts
#!/usr/bin/env python3
import ipaddress
import subprocess
'''
complete private networks:
10.0.0.0/8
172.16.0.0/12
192.168.0.0/16
'''
NETWORK_CIDR = "192.168.19.0/24"
net = ipaddress.ip_network(NETWORK_CIDR)
count = net.num_addresses
print(u"checking {} addresses:".format(count))
sum_alive = 0
sum_not_alive = 0
for ip in net:
ip = str(ip)
code = 0
status = "alive"
try:
pingreturn = subprocess.check_output(['ping', '-o', '-c 3', '-t 1', '-q', ip])
sum_alive += 1
except subprocess.CalledProcessError as e:
code = e.returncode
status = "not alive"
sum_not_alive += 1
print(u" {0:s} is {1:s} (ping result: {2})".format(ip, status, code))
print(u"{} alive, {} not alive, {} total.".format(sum_alive, sum_not_alive, count))
@michaelkrieg
Copy link
Author

einfach NETWORK_CIDR direkt setzen, dann das Skript wie gewohnt ausführen.

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