Advertisement
aolivens

Pyhton Port Scanner

Sep 24th, 2013
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.25 KB | None | 0 0
  1. #=====================================#
  2. #==============MODULES================#
  3. #=====================================#
  4.  
  5. from datetime import *
  6. import socket
  7.  
  8.  
  9. #=====================================#
  10. #==============VARIABLES==============#
  11. #=====================================#
  12.  
  13. start = 0
  14. end = 0
  15. menus = ["(1) Scan by IP", "(2) Scan by Hostname", "(3) Exit            "]
  16. line = "=" * 55
  17.  
  18. #=====================================#
  19. #==============FUNCTIONS==============#
  20. #=====================================#
  21.  
  22. #defines menu
  23. def menu(parameter):
  24.     print "     +====================+"
  25.     print "     | PORT SCANNER MENU  |"
  26.     print "     |====================|"
  27.     for word in parameter:
  28.         print "     |%s|" % word
  29.         print "     |-------------------|"
  30.        
  31. def elapsed(start, end):
  32.     final = (end - start)
  33.     print "Scan Completed In: %s" % final
  34.  
  35. def scanner(ip, line):
  36.     low_port = input("Please enter a low port number: ")
  37.     high_port = input("Please enter a high port number: ")
  38.     hostname = socket.gethostbyaddr(ip) #get hostname from ip
  39.    
  40.     #change variable start to current time
  41.     global start  
  42.     start = datetime.now()
  43.    
  44.     #create info bar
  45.     print
  46.     print line * 50
  47.     print "Scanning IP: %s Hostname: %s" % (ip, hostname)
  48.     print line * 50
  49.    
  50.     #iterate through ports and see if socket connection is established
  51.     for port in range(low_port, (high_port + 1)):
  52.         s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  53.         result = s.connect_ex((ip, port))
  54.         if result == 0:
  55.             print "Port %s: Open" % port
  56.         s.close()
  57.    
  58.     #change variable end to current time
  59.     global end
  60.     end = datetime.now()
  61.    
  62. #=====================================#
  63. #=============MAIN PROGRAM============#
  64. #=====================================#
  65.  
  66. def main():
  67.     menu(menus)
  68.     choice = input("Please make a selection(1-3): ")
  69.    
  70.     #IP
  71.     if choice == 1:
  72.         ip = raw_input("Please enter an IP to scan: ")
  73.         scanner(ip, line)
  74.         elapsed(start, end)
  75.     #Hostname
  76.     elif choice == 2:
  77.         parameter = raw_input("Please enter a hostname to scan: ")
  78.         ip = socket.gethostbyname(parameter) #convert hostname to ip
  79.         scanner(ip, line)
  80.         elapsed(start, end)
  81.     #Exit
  82.     elif choice == 3:
  83.         print "Exiting Program. Goodbye"
  84.         return
  85.        
  86.     #Fault
  87.     else:
  88.         print "Invalid Selection"
  89.         return main()
  90.  
  91.        
  92. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement