OP 23 January, 2023 - 03:48 PM
(This post was last modified: 23 January, 2023 - 04:23 PM by krinko. Edited 2 times in total.)
import socket
import argparse
import threading
from tqdm import tqdm
def scan_port(host, port):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(5)
result = sock.connect_ex((host, port))
if result == 0:
return port
else:
return None
def scan_ports(host, start, end, thread_count):
open_ports = []
with tqdm(total=end-start) as pbar:
with threading.Semaphore(thread_count):
for port in range(start, end):
thread = threading.Thread(target=scan_port, args=(host, port))
thread.start()
pbar.update(1)
open_ports.append(scan_port(host, port))
return [port for port in open_ports if port is not None]
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='IP Port Scanner')
parser.add_argument('host', type=str, help='The host to scan')
parser.add_argument('-s', '--start', type=int, default=1, help='The starting port')
parser.add_argument('-e', '--end', type=int, default=65535, help='The ending port')
parser.add_argument('-t', '--threads', type=int, default=10, help='The number of threads to use')
parser.add_argument('-f', '--file', type=str, help='The file to save the results to')
args = parser.parse_args()
open_ports = scan_ports(args.host, args.start, args.end, args.threads)
if args.file:
with open(args.file, 'w') as f:
for port in open_ports:
f.write(str(port) + '\n')
else:
print('Open Ports:')
for port in open_ports:
print(port)
Example of command :
If you enjoy it, give me a like ^^
import argparse
import threading
from tqdm import tqdm
def scan_port(host, port):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(5)
result = sock.connect_ex((host, port))
if result == 0:
return port
else:
return None
def scan_ports(host, start, end, thread_count):
open_ports = []
with tqdm(total=end-start) as pbar:
with threading.Semaphore(thread_count):
for port in range(start, end):
thread = threading.Thread(target=scan_port, args=(host, port))
thread.start()
pbar.update(1)
open_ports.append(scan_port(host, port))
return [port for port in open_ports if port is not None]
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='IP Port Scanner')
parser.add_argument('host', type=str, help='The host to scan')
parser.add_argument('-s', '--start', type=int, default=1, help='The starting port')
parser.add_argument('-e', '--end', type=int, default=65535, help='The ending port')
parser.add_argument('-t', '--threads', type=int, default=10, help='The number of threads to use')
parser.add_argument('-f', '--file', type=str, help='The file to save the results to')
args = parser.parse_args()
open_ports = scan_ports(args.host, args.start, args.end, args.threads)
if args.file:
with open(args.file, 'w') as f:
for port in open_ports:
f.write(str(port) + '\n')
else:
print('Open Ports:')
for port in open_ports:
print(port)
Example of command :
Code:
python scanner.py example.com -s 1 -e 1024 -t 50 -f results.txt