Skip to content

Instantly share code, notes, and snippets.

@lucaspar
Last active October 4, 2019 20:54
Show Gist options
  • Save lucaspar/4bc6af13d8fd803887652fc30a33da90 to your computer and use it in GitHub Desktop.
Save lucaspar/4bc6af13d8fd803887652fc30a33da90 to your computer and use it in GitHub Desktop.
[ SCR ] LAN IPs discovery with python using nmap :trollface:
#!/usr/bin/python3
# List all hosts' IP adresses in the
# local network reachable by nmap -sn
import os
import re
import sys
if __name__ == "__main__":
addresses = []
# ip from first arg or default
IP = sys.argv[1] if len(sys.argv) > 1 else '192.168.0.0'
# a regex pattern for ip addresses
re_pattern = r'\b25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?\.25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?\.25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?\.25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?\b'
# runs nmap
nmap_result = os.popen('nmap -sn ' + IP + '/24 | grep \'Nmap scan report\'').read().splitlines()
# extracts all ips in response
for i, line in enumerate(nmap_result):
filtered = re.findall(r'[0-9]+(?:\.[0-9]+){3}', line)
filtered = re.findall(re_pattern, line)
addresses.append('.'.join(filtered))
# output
for addr in addresses:
print(addr)
exit(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment