irenicus09

LAN Host Discovery Script [Python]

May 18th, 2012
407
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.57 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. __license__ = 'GPL-3'
  4. __version__ = '0.1'
  5. __author__  = 'irenicus09'
  6. __date__    = '18/05/2012'
  7.  
  8. """
  9. This simple script relies on Nmap to discover hosts on LAN,
  10. finds hosts using the Ping scan technique, parses & presents data
  11. in a clear, readable format for commandline users.
  12.  
  13. Note: Edit the value of IP Range as required.
  14. """
  15.  
  16. import subprocess, os
  17. from clint.textui import colored
  18.  
  19. # Edit the following values as required
  20. ipRange = '192.168.0.1/24'
  21. fileName = 'Hosts.txt'
  22.  
  23. # Don't edit this, list for storing result
  24. hostList = []
  25.  
  26. def FindHosts():
  27.     if ((os.path.exists(fileName)) == False):
  28.         cmd = subprocess.Popen("nmap -sP %s | grep -o -P '(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})' >> %s" % (ipRange, fileName), shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
  29.         cmd.communicate()
  30.     else:
  31.         os.remove(fileName)
  32.         FindHosts()
  33.  
  34. def PrintHosts():
  35.     try:
  36.         fileObject = open(fileName, 'r+')
  37.         hostList   = fileObject.readlines()
  38.         fileObject.close()
  39.  
  40.         count = 0
  41.  
  42.         print colored.green('=')*50
  43.         length = '\t\t%d HOSTS FOUND' % len(hostList)
  44.         print colored.cyan(length)
  45.         print colored.green('=')*50
  46.  
  47.         for i in hostList:
  48.             count += 1
  49.             value = '%s)'.ljust(4) % count
  50.             i = '%s'.ljust(15) % (i.strip())
  51.             print colored.green(value) + colored.red(i)
  52.     except (Exception):
  53.         print '\n[!] Error opening file %s' % fileName
  54.  
  55.  
  56. if __name__ == '__main__':
  57.     FindHosts()
  58.     PrintHosts()
Advertisement
Add Comment
Please, Sign In to add comment