Guest User

Untitled

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