Last active
April 17, 2022 14:48
-
-
Save emrysr/fbc94f4e41d672cd62b67feab187f52f to your computer and use it in GitHub Desktop.
pings all addresses on local network (255.255.255.0 subnet)
This file contains hidden or 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 socket | |
| import subprocess | |
| import ipaddress | |
| alive = [] | |
| def get_ip(): | |
| s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) | |
| s.settimeout(0) | |
| try: | |
| # doesn't even have to be reachable | |
| s.connect(('192.168.1.1', 1)) | |
| IP = s.getsockname()[0] | |
| except Exception: | |
| IP = '127.0.0.1' | |
| finally: | |
| s.close() | |
| return IP | |
| ip = get_ip() | |
| subnet = ipaddress.ip_network(u''+ip+'/24', strict=False) | |
| print('Scanning...(wait 30s approx)') | |
| for i in subnet.hosts(): | |
| i = str(i) | |
| retval = subprocess.call(["ping", "-c1", "-n", "-i0.1", "-W1", i], stdout=subprocess.PIPE) | |
| if retval == 0: | |
| alive.append(i) | |
| for ip in alive: | |
| print(ip + " is alive") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment