Advertisement
Guest User

Untitled

a guest
Sep 28th, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.47 KB | None | 0 0
  1. #!/usr/bin/env python
  2. import socket
  3. import subprocess
  4. import sys
  5. from datetime import datetime
  6.  
  7.  
  8. # Ask for input
  9. remoteServer    = raw_input("\nEnter a remote host to scan: ")
  10. remoteServerIP  = socket.gethostbyname(remoteServer)
  11.  
  12. # Print a nice banner with information on which host we are about to scan
  13. print "-" * 60
  14. print "Please wait, scanning remote host", remoteServerIP
  15. print "-" * 60
  16.  
  17. # Check what time the scan started
  18. t1 = datetime.now()
  19.  
  20. # Using the range function to specify ports (here it will scans all ports between 1 and 1024)
  21.  
  22. # We also put in some error handling for catching errors
  23.  
  24. for port in range(1,65535):  
  25.     sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  26.     try:
  27.         result = sock.connect_ex((remoteServerIP, port))
  28.         try:
  29.             service=socket.getservbyport(port)
  30.         except:
  31.             pass
  32.         if result == 0:
  33.             print "Port {}:  Open".format(port),service
  34.         sock.close()
  35.  
  36.     except KeyboardInterrupt:
  37.         print "You pressed Ctrl+C"
  38.         sys.exit()
  39.  
  40.     except socket.gaierror:
  41.         print 'Hostname could not be resolved. Exiting'
  42.         sys.exit()
  43.  
  44.     except socket.error:
  45.         print "Couldn't connect to server"
  46.         sys.exit()
  47.  
  48. # Checking the time again
  49. t2 = datetime.now()
  50.  
  51. # Calculates the difference of time, to see how long it took to run the script
  52. total =  t2 - t1
  53.  
  54. # Printing the information to screen
  55. print '\nScan completed in: ', total,'\n'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement