Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- from socket import *
- import threading
- # trying to import ipcalc and failing if it's not installed
- try:
- import ipcalc
- except:
- print "I need the ipcalc Python module"
- print "Try 'pip install ipcalc' or use this link:"
- print "https://pypi.python.org/pypi/ipcalc"
- exit()
- # semaphore for printing output
- # note: will probably still need if I switch to printing sorted list
- screen_lock = threading.Semaphore(value=1)
- # setting default timeout to 5 seconds
- setdefaulttimeout(5)
- # takes a subnet in cidr notation
- # and returns as list
- def make_iplist(ip_cidr):
- ip_list = []
- for ip in ipcalc.Network(str(ip_cidr)):
- ip_list.append(ip)
- return ip_list
- # takes a list of IP's, min port, and max port
- # opens a unique socket object for each one
- # returns list of the output
- def attempt_connections(ip_list, min_port, max_port, threaded=False):
- connection_list = []
- for ip in ip_list:
- for port in range(int(min_port), int(max_port) + 1):
- connection_list.append({'ip': ip, 'port': port, 'socket': 0, 'response': 0})
- c = 0
- while c < len(connection_list):
- connection_list[c]['socket'] = socket(AF_INET, SOCK_STREAM)
- c += 1
- c = 0
- while c < len(connection_list):
- try:
- connection_list[c]['socket'].connect((connection_list[c]['ip'], connection_list[c]['port']))
- connection_list[c]['socket'].send('knock knock\r\n')
- connection_list[c]['response'] = connection_list[c]['socket'].recv(1024)
- screen_lock.acquire()
- print 'Success: ' + str(connection_list[c]['ip']) + \
- ':' + str(connection_list[c]['port']) + ' response: ' + \
- str(connection_list[c]['response'])
- except:
- print 'Failed: ' + str(connection_list[c]['ip']) + \
- ':' + str(connection_list[c]['port']) + \
- ' timed out'
- finally:
- screen_lock.release()
- connection_list[c]['socket'].close()
- def main():
- ip_list = raw_input("Supply the IP:\t(CIDR works for subnets)\n>> ")
- lowest_port = raw_input("Supply the lowest port:\n>> ")
- highest_port = raw_input("Supply the highest port:\t(Can be the same as lowest)\n>> ")
- attempt_connections(make_iplist(ip_list), lowest_port, highest_port)
- exit()
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement