-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaps.py
More file actions
70 lines (55 loc) · 2.19 KB
/
aps.py
File metadata and controls
70 lines (55 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import asyncio
async def scan_port(ip, port):
"""
Scans a specific port on the given IP address.
If the port is open, it prints the result.
"""
try:
# Attempt to open a connection to the target IP and port
reader, writer = await asyncio.open_connection(ip, port)
print(f"[OPEN] Port {port} is open on {ip}")
writer.close()
await writer.wait_closed()
except:
# Ignore exceptions (closed or inaccessible ports)
pass
async def scan_ports(ip, start_port, end_port, max_concurrent_tasks=10000):
"""
Scans a range of ports on the given IP address with a limit on concurrent tasks.
Args:
ip (str): Target IP address.
start_port (int): Starting port number.
end_port (int): Ending port number.
max_concurrent_tasks (int): Maximum number of concurrent tasks.
"""
semaphore = asyncio.Semaphore(max_concurrent_tasks) # Limit concurrent tasks
async def sem_task(port):
async with semaphore:
await scan_port(ip, port)
# Create tasks for all ports in the range
tasks = [sem_task(port) for port in range(start_port, end_port + 1)]
# Run all tasks concurrently
await asyncio.gather(*tasks)
def main():
"""
Main function to handle user input and initiate the asynchronous port scanner.
"""
print("Welcome to the Async Port Scanner!")
# Get target IP address from user
ip = input("Enter the target IP address: ").strip()
# Get port range from user
try:
start_port = int(input("Enter the starting port (e.g., 1): ").strip())
end_port = int(input("Enter the ending port (e.g., 65535): ").strip())
# Validate port range
if start_port < 1 or end_port > 65535 or start_port > end_port:
raise ValueError("Invalid port range. Ports must be between 1 and 65535.")
except ValueError as e:
print(f"Error: {e}")
return
print(f"\nScanning IP: {ip} from port {start_port} to {end_port}...\n")
# Run the asynchronous scanner
asyncio.run(scan_ports(ip, start_port, end_port))
print("\nScan complete!")
if __name__ == "__main__":
main()