Skip to content

Instantly share code, notes, and snippets.

@mcarne
Created January 11, 2019 12:44
Show Gist options
  • Save mcarne/64a019ef372722dae1bf5b68cdcbe820 to your computer and use it in GitHub Desktop.
Save mcarne/64a019ef372722dae1bf5b68cdcbe820 to your computer and use it in GitHub Desktop.
Scan local network for hosts (IPv4)
#!/usr/bin/env python3
import subprocess
import math
import re
cmd = ['nmap', '-sn', '192.168.0.0/24']
result = subprocess.run(cmd,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
print(result.stdout.decode('utf-8'))
output_lines = result.stdout.decode('utf-8').split('\n')
final_list = []
p = re.compile('(^Nmap scan report for )(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})$')
for index in range(0, len(output_lines)):
# print(output_lines[index])
# check every 2nd line of nmap's output
if math.ceil(index / 2) == index / 2:
try:
m = p.match(output_lines[index])
# final_list.append(output_lines[index])
final_list.append(m.group(2))
except Exception as e:
print(e)
print(final_list)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment