Advertisement
Guest User

Untitled

a guest
Jul 24th, 2014
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.38 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. from socket import *
  4. import threading
  5. # trying to import ipcalc and failing if it's not installed
  6. try:
  7.     import ipcalc
  8. except:
  9.     print "I need the ipcalc Python module"
  10.     print "Try 'pip install ipcalc' or use this link:"
  11.     print "https://pypi.python.org/pypi/ipcalc"
  12.     exit()
  13.  
  14. # semaphore for printing output
  15. # note: will probably still need if I switch to printing sorted list
  16. screen_lock = threading.Semaphore(value=1)
  17.  
  18. # setting default timeout to 5 seconds
  19. setdefaulttimeout(5)
  20.  
  21. # takes a subnet in cidr notation
  22. # and returns as list
  23. def make_iplist(ip_cidr):
  24.     ip_list = []
  25.     for ip in ipcalc.Network(str(ip_cidr)):
  26.         ip_list.append(ip)
  27.     return ip_list
  28.  
  29. # takes a list of IP's, min port, and max port
  30. # opens a unique socket object for each one
  31. # returns list of the output
  32. def attempt_connections(ip_list, min_port, max_port, threaded=False):
  33.     connection_list = []
  34.  
  35.     for ip in ip_list:
  36.         for port in range(int(min_port), int(max_port) + 1):
  37.             connection_list.append({'ip': ip, 'port': port, 'socket': 0, 'response': 0})
  38.  
  39.     c = 0
  40.     while c < len(connection_list):
  41.         connection_list[c]['socket'] = socket(AF_INET, SOCK_STREAM)
  42.         c += 1
  43.  
  44.     c = 0
  45.     while c < len(connection_list):
  46.         try:
  47.             connection_list[c]['socket'].connect((connection_list[c]['ip'], connection_list[c]['port']))
  48.             connection_list[c]['socket'].send('knock knock\r\n')
  49.             connection_list[c]['response'] = connection_list[c]['socket'].recv(1024)
  50.             screen_lock.acquire()
  51.             print 'Success: ' + str(connection_list[c]['ip']) + \
  52.                 ':' + str(connection_list[c]['port']) + ' response: ' + \
  53.                 str(connection_list[c]['response'])
  54.         except:
  55.             print 'Failed: ' + str(connection_list[c]['ip']) + \
  56.                 ':' + str(connection_list[c]['port']) + \
  57.                 ' timed out'
  58.         finally:
  59.             screen_lock.release()
  60.             connection_list[c]['socket'].close()
  61.  
  62. def main():
  63.  
  64.     ip_list = raw_input("Supply the IP:\t(CIDR works for subnets)\n>> ")
  65.     lowest_port = raw_input("Supply the lowest port:\n>> ")
  66.     highest_port = raw_input("Supply the highest port:\t(Can be the same as lowest)\n>> ")
  67.  
  68.     attempt_connections(make_iplist(ip_list), lowest_port, highest_port)
  69.  
  70.     exit()
  71.  
  72. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement