-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathportscanner.py
More file actions
32 lines (27 loc) · 1.04 KB
/
portscanner.py
File metadata and controls
32 lines (27 loc) · 1.04 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
import asyncio
import socket
async def scan_port(host, port, timeout=1):
"""Scan a single TCP port asynchronously."""
try:
conn = asyncio.open_connection(host, port)
reader, writer = await asyncio.wait_for(conn, timeout=timeout)
# Optional: Banner grabbing
try:
writer.write(b"\n")
await writer.drain()
banner = await asyncio.wait_for(reader.read(100), timeout=1)
banner = banner.decode(errors="ignore").strip()
except:
banner = "No banner"
print(f"[OPEN] {host}:{port} - {banner}")
writer.close()
await writer.wait_closed()
except (asyncio.TimeoutError, ConnectionRefusedError, OSError):
pass # Port closed or filtered
async def main():
host = "scanme.nmap.org" # Example target
ports = range(20, 1025) # Common ports
tasks = [scan_port(host, port) for port in ports]
await asyncio.gather(*tasks)
if __name__ == "__main__":
asyncio.run(main())