|
| 1 | +import socket |
| 2 | +import subprocess |
| 3 | +import sys |
| 4 | +from datetime import datetime |
| 5 | + |
| 6 | +subprocess.call('cls', shell=True) |
| 7 | + |
| 8 | +# Ask for input |
| 9 | +remoteServer = input("Enter 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 |
| 21 | + |
| 22 | +# We also put in some error handling for catching errors |
| 23 | +try: |
| 24 | + for port in range(1,1025): |
| 25 | + sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 26 | + result = sock.connect_ex((remoteServerIP, port)) |
| 27 | + if result == 0: |
| 28 | + print ("Port {}: Open".format(port)) |
| 29 | + sock.close() |
| 30 | +except KeyboardInterrupt: |
| 31 | + print ("You pressed Ctrl+C") |
| 32 | + sys.exit() |
| 33 | + |
| 34 | +except socket.gaierror: |
| 35 | + print ('Hostname could not be resolved. Exiting') |
| 36 | + sys.exit() |
| 37 | + |
| 38 | +except socket.error: |
| 39 | + print ("Couldn't connect to server") |
| 40 | + sys.exit() |
| 41 | + |
| 42 | +# Checking the time again |
| 43 | +t2 = datetime.now() |
| 44 | + |
| 45 | +# Calculates the difference of time, to see how long it took to run the script |
| 46 | +total = t2 - t1 |
| 47 | + |
| 48 | +print ('Scanning Completed in: ', total) |
0 commit comments