Created
January 4, 2016 17:58
-
-
Save michaelkrieg/9b6b6e15c5bd070b5191 to your computer and use it in GitHub Desktop.
ping a CIDR network and find alive and not alive hosts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
einfach NETWORK_CIDR direkt setzen, dann das Skript wie gewohnt ausführen.