Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import socket
- import subprocess
- import sys
- from datetime import datetime
- INTERVAL = 3600
- # portscanner code from http://www.pythonforbeginners.com/code-snippets-source-code/port-scanner-in-python
- def portScanner():
- counter = 100
- openPorts = []
- while counter <= 108:
- try:
- #print(counter)
- deviceIP = "192.168.1." + str(counter)
- openPorts.append(deviceIP)
- # Clears command prompt
- #subprocess.call('clear', shell=True)
- # Sets the socket to the desired device IP
- remoteServerIP = socket.gethostbyname(deviceIP)
- print(deviceIP)
- #-------Unused code from portScanner---------
- # Print a nice banner with information on which host we are about to scan
- #print ("-" * 60)
- #print ("Please wait, scanning remote host", remoteServerIP)
- #print ("-" * 60)
- # Check what time the scan started
- #t1 = datetime.now()
- # Using the range function to specify ports (here it will scans all ports between 1 and 1024)
- # We also put in some error handling for catching errors
- # initializes the array that will be used to track current open ports on our socket
- # checks ports 1-1025 to see if they are open and adds them to the list if they are
- port = 1
- while port <= 700:
- print("Port checked:", port)
- sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- sock.settimeout(8)
- #sock = socket.create_connection((remoteServerIP, port), 15)
- #print("Sock initialized")
- #sock.setdefaulttimeout(6)
- result = sock.connect_ex((remoteServerIP, port))
- #print(result)
- #print("Sock connected")
- if result == 0:
- print("Hit if!")
- #print ("Port {}: Open".format(port))
- openPorts.append(port)
- sock.close()
- #print("Did this work?")
- port += 1
- counter += 1
- except KeyboardInterrupt:
- print (" Keyboard Interrupt")
- sys.exit()
- #pass
- except socket.gaierror:
- print ('Hostname could not be resolved. Exiting')
- #sys.exit()
- counter+=1
- pass
- except socket.error:
- print ("Couldn't connect to server")
- #sys.exit()
- counter+=1
- pass
- except socket.timeout:
- print("Connection timed out")
- counter+=1
- pass
- # unused: tracks the total time that portScanner takes to run
- # Checking the time again
- #t2 = datetime.now()
- # Calculates the difference of time, to see how long it took to run the script
- #total = t2 - t1
- # Printing the information to screen
- #print ('Scanning Completed in: ', total)
- return openPorts
- # this def uses the port scanner to compare current open ports to the previous test
- # it creates a log if the ports do not match
- def openPortChecker(log, openPorts):
- counter = 100
- currPorts = []
- while counter <= 108:
- try:
- deviceIP = "192.168.1." + id
- currPorts.append(deviceIP)
- remoteServerIP = socket.gethostbyname(deviceIP)
- port = 1
- while port <= 700:
- sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- sock.settimeout(2)
- result = sock.connect_ex((remoteServerIP, port))
- if result == 0:
- currPorts.append(port)
- sock.close()
- port += 1
- counter += 1
- except KeyboardInterrupt:
- print ("KeyboardInterrupt: Ctrl+C")
- sys.exit()
- except socket.gaierror:
- print ('Hostname could not be resolved. Exiting')
- counter+=1
- #sys.exit()
- pass
- except socket.error:
- print ("Couldn't connect to server")
- counter+=1
- #sys.exit()
- pass
- except socket.timeout:
- print("Connection timed out")
- counter+=1
- pass
- # if the lists don't match a log containing the new open ports is created
- if openPorts != currPorts:
- fileName = "portlog_" + str(date) + "_" + str(time) + ".txt"
- logFile = open(fileName)
- logFile.write("-"*8 + "Open Ports" + "-"*8)
- for port in currPorts:
- logFile.write("Port: " + port)
- logFile.close()
- print("New log created:", date, time)
- openPorts = currPorts
- return fileName
- else:
- return log
- def main():
- #print("Hello")
- openPorts = portScanner()
- print("Did it work?")
- originDate = date
- originTime = time
- #interval = 3600
- fileName = "portlog_" + str(originDate) + "_" + str(originTime) + ".txt"
- logFile = open(fileName)
- for port in openPorts:
- logFile.write("Port: " + port)
- logFile.close()
- print("New log created:", date, time)
- while originDate == date:
- if (time - originTime) % INTERVAL == 0:
- fileName = openPortChecker(fileName, openPorts)
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement