Repository where I mostly put random python scripts.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

63 lines
1.6 KiB

  1. #!/usr/bin/env python3
  2. ''' PortScan v3
  3. -----------
  4. This application scans for open ports on the designated system. It uses
  5. multiprocessing to speed up this process.
  6. '''
  7. import socket
  8. import subprocess
  9. import sys
  10. from datetime import datetime
  11. from multiprocessing import Pool
  12. def scan(port):
  13. try:
  14. sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  15. result = sock.connect_ex((target_ip, port))
  16. if result == 0:
  17. print("Port {}:\tOpen".format(port))
  18. sock.close()
  19. except socket.gaierror:
  20. print('Hostname could not be resolved.')
  21. sys.exit(0)
  22. except socket.error:
  23. print("Couldn't connect to server.")
  24. sys.exit(0)
  25. except:
  26. return
  27. if __name__ == '__main__':
  28. ports = list(range(1,4096))
  29. target = ''
  30. try:
  31. target = sys.argv[1]
  32. except:
  33. print("\nUsage:\t{} [target]\n\n\tScan for open ports on target machine.\n".format(sys.argv[0]))
  34. sys.exit(0)
  35. # Clear the screen
  36. subprocess.call('clear', shell=True)
  37. target_ip = socket.gethostbyname(target)
  38. # Print a nice banner with information on which host we are about to scan
  39. print("-" * 60)
  40. print("Please wait, scanning remote host", target_ip)
  41. print("-" * 60)
  42. # Check what time the scan started
  43. t1 = datetime.now()
  44. with Pool(processes = 8) as p:
  45. p.map(scan, ports)
  46. # Checking the time again
  47. t2 = datetime.now()
  48. # Calculates the difference of time, to see how long it took to run the script
  49. total = t2 - t1
  50. # Printing the information to screen
  51. print('Scanning Completed in: ', total)