Advertisement
Guest User

Port Analysis Tool

a guest
Jul 5th, 2017
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.45 KB | None | 0 0
  1. import socket
  2. import subprocess
  3. import sys
  4. from datetime import datetime
  5.  
  6. INTERVAL = 3600
  7.  
  8.  
  9. # portscanner code from http://www.pythonforbeginners.com/code-snippets-source-code/port-scanner-in-python
  10. def portScanner():
  11.  
  12.     counter = 100
  13.     openPorts = []
  14.    
  15.     while counter <= 108:
  16.         try:
  17.             #print(counter)
  18.             deviceIP = "192.168.1." + str(counter)
  19.             openPorts.append(deviceIP)
  20.  
  21.             # Clears command prompt
  22.             #subprocess.call('clear', shell=True)
  23.  
  24.             # Sets the socket to the desired device IP
  25.             remoteServerIP  = socket.gethostbyname(deviceIP)
  26.             print(deviceIP)
  27.             #-------Unused code from portScanner---------
  28.             # Print a nice banner with information on which host we are about to scan
  29.             #print ("-" * 60)
  30.             #print ("Please wait, scanning remote host", remoteServerIP)
  31.             #print ("-" * 60)
  32.             # Check what time the scan started
  33.             #t1 = datetime.now()
  34.             # Using the range function to specify ports (here it will scans all ports between 1 and 1024)
  35.             # We also put in some error handling for catching errors
  36.        
  37.             # initializes the array that will be used to track current open ports on our socket
  38.            
  39.        
  40.             # checks ports 1-1025 to see if they are open and adds them to the list if they are
  41.             port = 1
  42.             while port <= 700:
  43.                 print("Port checked:", port)
  44.                 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  45.                 sock.settimeout(8)
  46.                 #sock = socket.create_connection((remoteServerIP, port), 15)
  47.                 #print("Sock initialized")
  48.                 #sock.setdefaulttimeout(6)
  49.                 result = sock.connect_ex((remoteServerIP, port))
  50.                 #print(result)
  51.                 #print("Sock connected")
  52.                 if result == 0:
  53.                     print("Hit if!")
  54.                     #print ("Port {}:    Open".format(port))
  55.                     openPorts.append(port)
  56.                 sock.close()
  57.                 #print("Did this work?")
  58.                 port += 1
  59.             counter += 1
  60.  
  61.         except KeyboardInterrupt:
  62.             print (" Keyboard Interrupt")
  63.             sys.exit()
  64.             #pass
  65.  
  66.         except socket.gaierror:
  67.             print ('Hostname could not be resolved. Exiting')
  68.             #sys.exit()
  69.             counter+=1
  70.             pass
  71.  
  72.         except socket.error:
  73.             print ("Couldn't connect to server")
  74.             #sys.exit()
  75.             counter+=1
  76.             pass
  77.  
  78.         except socket.timeout:
  79.             print("Connection timed out")
  80.             counter+=1
  81.             pass
  82.  
  83.         # unused: tracks the total time that portScanner takes to run
  84.         # Checking the time again
  85.         #t2 = datetime.now()
  86.         # Calculates the difference of time, to see how long it took to run the script
  87.         #total =  t2 - t1
  88.         # Printing the information to screen
  89.         #print ('Scanning Completed in: ', total)
  90.            
  91.     return openPorts
  92.  
  93. # this def uses the port scanner to compare current open ports to the previous test
  94. # it creates a log if the ports do not match   
  95. def openPortChecker(log, openPorts):
  96.  
  97.     counter = 100
  98.     currPorts = []
  99.    
  100.     while counter <= 108:
  101.         try:
  102.  
  103.             deviceIP = "192.168.1." + id
  104.             currPorts.append(deviceIP)
  105.             remoteServerIP  = socket.gethostbyname(deviceIP)
  106.        
  107.        
  108.             port = 1
  109.        
  110.             while port <= 700:  
  111.                 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  112.                 sock.settimeout(2)
  113.                 result = sock.connect_ex((remoteServerIP, port))
  114.                 if result == 0:
  115.                     currPorts.append(port)
  116.                 sock.close()
  117.                 port += 1
  118.             counter += 1
  119.  
  120.         except KeyboardInterrupt:
  121.             print ("KeyboardInterrupt: Ctrl+C")
  122.             sys.exit()
  123.  
  124.         except socket.gaierror:
  125.             print ('Hostname could not be resolved. Exiting')
  126.             counter+=1
  127.             #sys.exit()
  128.             pass
  129.  
  130.         except socket.error:
  131.             print ("Couldn't connect to server")
  132.             counter+=1
  133.             #sys.exit()
  134.             pass
  135.  
  136.         except socket.timeout:
  137.             print("Connection timed out")
  138.             counter+=1
  139.             pass
  140.    
  141.     # if the lists don't match a log containing the new open ports is created
  142.     if openPorts != currPorts:
  143.         fileName = "portlog_" + str(date) + "_" + str(time) + ".txt"
  144.         logFile = open(fileName)
  145.         logFile.write("-"*8 + "Open Ports" + "-"*8)
  146.         for port in currPorts:
  147.             logFile.write("Port: " + port)
  148.         logFile.close()
  149.         print("New log created:", date, time)
  150.         openPorts = currPorts
  151.         return fileName
  152.     else:
  153.         return log
  154.  
  155.  
  156. def main():
  157.     #print("Hello")
  158.     openPorts = portScanner()
  159.     print("Did it work?")
  160.     originDate = date
  161.     originTime = time
  162.     #interval = 3600
  163.     fileName = "portlog_" + str(originDate) + "_" + str(originTime) + ".txt"
  164.     logFile = open(fileName)
  165.    
  166.     for port in openPorts:
  167.         logFile.write("Port: " + port)
  168.     logFile.close()
  169.     print("New log created:", date, time)
  170.    
  171.     while originDate == date:
  172.         if (time - originTime) % INTERVAL == 0:
  173.             fileName = openPortChecker(fileName, openPorts)
  174.  
  175. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement