Advertisement
Guest User

Untitled

a guest
Oct 8th, 2015
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 KB | None | 0 0
  1. # python 2.7.6.
  2. # portScanner.py
  3.  
  4. import socket
  5. from datetime import datetime
  6. import sys
  7.  
  8. # Here we are scanning your own terminal
  9. # Replace this with gethostbyname("host") to scan a remote host
  10.  
  11. # scanServer = "localhost"
  12. scanServer = socket.gethostname()
  13. scanServerIP = socket.gethostbyname(scanServer)
  14.  
  15. time_start = datetime.now()
  16. print("Scanning IP {0} started at {1}".format(scanServerIP, time_start))
  17.  
  18. try:
  19. # Scanning from port range. Scan of each port might take a second
  20. for port in range (1, 200):
  21. # We opt for TCP connections using socket_stream, as against UDP connections using socket_dgram
  22. sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  23.  
  24. # Using connect_ex does not raise exception for normal connection denial
  25. # Useful to skip the port and move on to next port
  26. result = sock.connect_ex((scanServerIP, port))
  27. if(result == 0):
  28. print("Port {0} is open".format(port))
  29. sock.close()
  30.  
  31. except KeyboardInterrupt:
  32. print("Stopping....")
  33. sys.exit()
  34.  
  35. except socket.error as e:
  36. print e
  37. sys.exit()
  38.  
  39. time_end = datetime.now()
  40. print("Scanning completed at {0}".format(time_end))
  41. print("Total time took for scanning: {0}".format(time_end - time_start))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement