|
1 | 1 | import socket
|
2 | 2 | import subprocess
|
3 | 3 | import sys
|
| 4 | +import asyncio |
4 | 5 | from datetime import datetime
|
5 | 6 |
|
6 | 7 | subprocess.call('cls', shell=True)
|
|
17 | 18 | # Check what time the scan started
|
18 | 19 | t1 = datetime.now()
|
19 | 20 |
|
20 |
| -# Using the range function to specify ports (here it will scans all ports |
| 21 | +async def scan_port(port): |
| 22 | + sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 23 | + sock.setblocking(False) |
| 24 | + try: |
| 25 | + await asyncio.wait_for( |
| 26 | + asyncio.get_event_loop().sock_connect(sock, (remoteServerIP, port)), |
| 27 | + timeout=1 |
| 28 | + ) |
| 29 | + print("Port {}: Open".format(port)) |
| 30 | + except (socket.timeout, ConnectionRefusedError): |
| 31 | + pass |
| 32 | + finally: |
| 33 | + sock.close() |
21 | 34 |
|
22 |
| -# We also put in some error handling for catching errors |
23 |
| -try: |
| 35 | +async def main(): |
| 36 | + tasks = [] |
24 | 37 | 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() |
| 38 | + tasks.append(scan_port(port)) |
| 39 | + await asyncio.gather(*tasks) |
| 40 | + |
| 41 | +try: |
| 42 | + asyncio.run(main()) |
30 | 43 | except KeyboardInterrupt:
|
31 | 44 | print("You pressed Ctrl+C")
|
32 | 45 | sys.exit()
|
|
45 | 58 | # Calculates the difference of time, to see how long it took to run the script
|
46 | 59 | total = t2 - t1
|
47 | 60 |
|
48 |
| -print('Scanning Completed in: ', total) |
| 61 | +print('Scanning Completed in:', total) |
0 commit comments