-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScanner.py
61 lines (50 loc) · 1.58 KB
/
Scanner.py
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
import socket
import argparse
import threading
import logging
from queue import Queue
# Set up logging
logging.basicConfig(level=logging.INFO, format='%(message)s')
logger = logging.getLogger()
# Function to scan a single port
def scan_port(ip, port, timeout=1):
try:
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.settimeout(timeout)
result = s.connect_ex((ip, port))
if result == 0:
logger.info(f"Port: {port} Open")
except socket.error:
pass
# Thread worker function
def threader(q):
while True:
worker = q.get()
scan_port(target_ip, worker)
q.task_done()
# Main function
def main(target_ip, start_port, end_port, num_threads=100):
print("*" * 40)
print(f"* Scanning: {target_ip} *")
print("*" * 40)
q = Queue()
# Start threads
for x in range(num_threads):
t = threading.Thread(target=threader, args=(q,))
t.daemon = True
t.start()
# Put ports in queue
for port in range(start_port, end_port + 1):
q.put(port)
q.join()
# Argument parsing
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Simple Port Scanner")
parser.add_argument("ip", help="Target IP address to scan")
parser.add_argument("-s", "--start", help="Start port", type=int, default=1)
parser.add_argument("-e", "--end", help="End port", type=int, default=1024)
args = parser.parse_args()
target_ip = args.ip
start_port = args.start
end_port = args.end
main(target_ip, start_port, end_port)