Skip to content

Instantly share code, notes, and snippets.

@robert-mcdermott
Last active May 4, 2022 20:57
Show Gist options
  • Save robert-mcdermott/bff4836656ec0ed1c0f7fc291b438afa to your computer and use it in GitHub Desktop.
Save robert-mcdermott/bff4836656ec0ed1c0f7fc291b438afa to your computer and use it in GitHub Desktop.
#!/usr/bin/python
import os, socket, sys, time
socket.setdefaulttimeout(10)
start = time.time()
subnets = ['140.107.42','140.107.43','140.107.88','140.107.89','140.107.134',\
'140.107.135','140.107.52','140.107.53','140.107.152','140.107.153',\
'140.107.170','140.107.171']
scanips = []; hosts = []; vhosts = []; free = []; vfree = []; reclaim = []; squatters = []
for subnet in subnets:
for i in range(1,255):
scanips.append('%s.%s' % (subnet, i))
print('Resolving hostnames...')
for host in scanips:
try:
h = socket.gethostbyaddr(host)
name = h[0]
ip = h[2][0]
if not 'DHCP' in name: hosts.append((name,ip))
except:
free.append(host)
print('Checking if hosts are alive...')
for host in hosts:
if '100% packet loss' in os.popen('ping -c 1 -W 1 %s' % host[1]).read():
reclaim.append(host)
else:
vhosts.append(host)
print('Verifying free IPs and looking for squatters...')
for ip in free:
if '100% packet loss' in os.popen('ping -c 1 -W 1 %s' % ip).read():
vfree.append(ip)
else:
squatters.append(ip)
stop = time.time()
et = stop - start
t = time.localtime()
ts = "%s-%02d-%02d_%02d.%02d" % (t[0], t[1], t[2], t[3], t[4])
print('Done!')
print('\nInterrogated %s IPs in %0.2f minutes (%0.2f IPs per second)' % (len(scanips), et / 60, len(scanips)/et))
print('+---------------------------------+')
print('| verified registered hosts: %-4s |' % len(vhosts))
print('| verified free IPs : %-4s |' % len(vfree))
print('| stale registered hosts : %-4s |' % len(reclaim))
print('| IP squatters : %-4s |' % len(squatters))
print('+---------------------------------+\n')
print('see generated reports for details\n')
vh = open('verified-hosts-%s.csv' % ts, 'w')
for h in vhosts: vh.write('%s, %s\n' % h)
vh.close()
vf = open('verified-free-ips-%s.csv' % ts, 'w')
for f in vfree: vf.write('%s\n' % f)
vf.close()
st = open('stale-hosts-%s.csv' % ts, 'w')
for s in reclaim: st.write('%s, %s\n' % s)
st.close()
sq = open('ip-squatters-%s.csv' % ts, 'w')
for q in squatters: sq.write('%s\n' % q)
sq.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment