Skip to content

Instantly share code, notes, and snippets.

@hackingbutlegal
Created February 12, 2013 03:59
Show Gist options
  • Select an option

  • Save hackingbutlegal/4760136 to your computer and use it in GitHub Desktop.

Select an option

Save hackingbutlegal/4760136 to your computer and use it in GitHub Desktop.
Scan a range with nmap.
#!/usr/bin/python2
#
# DEPS: nmap, notify-send
#
import subprocess
import re
import time
import sys
import os
def diff(list1, list2):
if len(list1) < len(list2):
tmp = list2
list2 = list1
list1 = tmp
tmp = []
for e in list1:
if e not in list2:
tmp.append(e)
return tmp
# help screen
if len(sys.argv) != 3:
print '\n\t################ LAN OBSERVER ################'
print '\trun as root for better results \'nmap -sn\' :) '
print '\tusage:\t\t%s \'ip range\' \'intervall\' [sec] '%(sys.argv[0])
print '\texample:\t%s 192.168.1.1-10 15'%(sys.argv[0])
print '\t\t\twill scan every 15 sec the IP Range 192.168.1.1-10\n'
print '\t##################################################\n\n'
sys.exit()
iprange = sys.argv[1]
intervall = float(sys.argv[2])
nmap = ['nmap', '-sn', iprange]
tmpList = []
hostList = []
user = os.getlogin()
notify_send_join = 'notify-send \'host join: %s\''
notify_send_quit = 'notify-send \'host quit: %s\''
if os.getuid() == 0:
notify_send_join = 'su - '+user+' -c \"notify-send \'host join: %s\'\"'
notify_send_quit = 'su - '+user+' -c \"notify-send \'host quit: %s\'\"'
try:
while True:
output = subprocess.Popen(nmap, stdout=subprocess.PIPE).stdout.read()
tmpList = re.findall('\d*\.\d*\.\d*\.\d*', output)
if tmpList == hostList:
pass
elif len(tmpList) >= len(hostList):
for ip in tmpList:
if ip not in hostList:
hostList.append(ip)
os.system(notify_send_join%(ip))
else:
for ip in diff(hostList, tmpList):
hostList.remove(ip)
os.system(notify_send_quit%(ip))
time.sleep(intervall)
#print hostList
except(KeyboardInterrupt):
print 'KeyboardInterrupt'
sys.exit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment