From 933a0d64c4a7efb4877b382e24ddbe2fa388d376 Mon Sep 17 00:00:00 2001 From: Mazzart Date: Wed, 30 Oct 2019 22:31:53 +0200 Subject: [PATCH 1/2] Add solution for port-sniffer homework --- submissions/mazzart/port-sniffer/sniffer.py | 76 +++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 submissions/mazzart/port-sniffer/sniffer.py diff --git a/submissions/mazzart/port-sniffer/sniffer.py b/submissions/mazzart/port-sniffer/sniffer.py new file mode 100644 index 0000000..48999b0 --- /dev/null +++ b/submissions/mazzart/port-sniffer/sniffer.py @@ -0,0 +1,76 @@ +import argparse +import socket +import sys + + +def argument_parser(): + """Initialize argument parser""" + + script_description = "Sniffer is tool which allows user to easily " \ + "search for opened TCP ports on particular host" + + parser = argparse.ArgumentParser(description=script_description) + parser.add_argument('--host', type=str, nargs='?', required=True, + help='enter domain name or IP address to search') + parser.add_argument('--ports', type=str, nargs='?', default='0-65535', + help='limit the range of ports to scan, ' + 'use the format: -') + + return parser + + +def ports_scanner(host: str, ports: str) -> list: + """Scans and saves open TCP ports""" + + buffer = [] + start, end = ports.split('-') + start, end = int(start), int(end) + + try: + for port in range(start, end+1): + with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock: + sock.settimeout(0.3) + conn = sock.connect_ex((host, port)) + if conn == 0: + print('.', end='') + buffer.append(port) + else: + continue + + except KeyboardInterrupt: + print("You printed Ctrl+C") + sys.exit() + except socket.gaierror: + print("Hostname could not be resolved") + sys.exit() + except socket.error as error: + print(error) + sys.exit() + + return buffer + + +def show_ports(ports_list: list): + """Prints ports info""" + + if ports_list: + print() + for port in ports_list: + print(port, end=',') + print(' ports are opened') + else: + print('There are no open ports') + + +def main(): + """Script entry point""" + + args = argument_parser().parse_args() + argv = vars(args) + host, ports = argv['host'], argv['ports'] + open_ports = ports_scanner(host, ports) + show_ports(open_ports) + + +if __name__ == "__main__": + main() From 810e7f9cf74b4a78c4d162441171ef42df7767ae Mon Sep 17 00:00:00 2001 From: Mazzart Date: Wed, 18 Dec 2019 10:54:19 +0200 Subject: [PATCH 2/2] Added status codes --- submissions/mazzart/port-sniffer/sniffer.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/submissions/mazzart/port-sniffer/sniffer.py b/submissions/mazzart/port-sniffer/sniffer.py index 48999b0..c060005 100644 --- a/submissions/mazzart/port-sniffer/sniffer.py +++ b/submissions/mazzart/port-sniffer/sniffer.py @@ -38,14 +38,14 @@ def ports_scanner(host: str, ports: str) -> list: continue except KeyboardInterrupt: - print("You printed Ctrl+C") - sys.exit() + print("\nYou printed Ctrl+C") + sys.exit(1) except socket.gaierror: print("Hostname could not be resolved") - sys.exit() + sys.exit(1) except socket.error as error: print(error) - sys.exit() + sys.exit(1) return buffer @@ -61,6 +61,8 @@ def show_ports(ports_list: list): else: print('There are no open ports') + sys.exit(0) + def main(): """Script entry point"""