diff --git a/pscanner/__about__.py b/pscanner/__about__.py index 757f5b4..e1c96e6 100644 --- a/pscanner/__about__.py +++ b/pscanner/__about__.py @@ -1,4 +1,4 @@ # SPDX-FileCopyrightText: 2022-present U.N. Owen # # SPDX-License-Identifier: MIT -__version__ = '0.0.1' +__version__ = "0.0.1" diff --git a/pscanner/__init__.py b/pscanner/__init__.py index a2fd857..5d04701 100644 --- a/pscanner/__init__.py +++ b/pscanner/__init__.py @@ -12,12 +12,15 @@ GREEN = Fore.GREEN RESET = Fore.RESET GRAY = Fore.LIGHTBLACK_EX + def is_host_alive(host: str) -> bool: if ping(host, timeout=1, count=1).success(): return True else: print(f"{GRAY}{host:15} is not alive {RESET}") - return False + return False + + def is_port_open(host: str, port: int) -> bool: try: s = socket.socket() @@ -31,14 +34,16 @@ def is_port_open(host: str, port: int) -> bool: finally: s.close() + def is_subnet(ip: str) -> bool: - if '/' in ip: + if "/" in ip: return True else: return False + def hosts_in_subnet(network: str) -> List: if not is_subnet: print(f"{network} is not a network") - - return list(ipaddress.ip_network(network).hosts()) \ No newline at end of file + + return list(ipaddress.ip_network(network).hosts()) diff --git a/pscanner/__main__.py b/pscanner/__main__.py index 404e5f1..d208c9a 100644 --- a/pscanner/__main__.py +++ b/pscanner/__main__.py @@ -3,7 +3,7 @@ # SPDX-License-Identifier: MIT import sys -if __name__ == '__main__': +if __name__ == "__main__": from .cli import pscanner sys.exit(pscanner()) diff --git a/pscanner/cli/__init__.py b/pscanner/cli/__init__.py index 5340046..55cddd9 100644 --- a/pscanner/cli/__init__.py +++ b/pscanner/cli/__init__.py @@ -6,16 +6,19 @@ from pscanner import is_port_open, is_subnet, hosts_in_subnet, is_host_alive from ..__about__ import __version__ -@click.group(context_settings={'help_option_names': ['-h', '--help']}, invoke_without_command=True) -@click.version_option(version=__version__, prog_name='pscanner') -@click.argument('host') -@click.argument('port') +@click.group( + context_settings={"help_option_names": ["-h", "--help"]}, + invoke_without_command=True, +) +@click.version_option(version=__version__, prog_name="pscanner") +@click.argument("host") +@click.argument("port") @click.pass_context def pscanner(ctx: click.Context, host, port): if is_subnet(host): for ip in hosts_in_subnet(host): if is_host_alive(str(ip)): is_port_open(str(ip), port) - else: - if is_host_alive(host): - is_port_open(host, port) \ No newline at end of file + else: + if is_host_alive(host): + is_port_open(host, port) diff --git a/pyproject.toml b/pyproject.toml index cc2331c..75d8b81 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -25,6 +25,7 @@ classifiers = [ ] dependencies = [ "click", + "pythonping" ] dynamic = ["version"] diff --git a/tests/test_pscanner.py b/tests/test_pscanner.py index 950adb4..7605a00 100644 --- a/tests/test_pscanner.py +++ b/tests/test_pscanner.py @@ -1,12 +1,15 @@ import pscanner import pytest + def test_is_port_open(): pass + def test_is_subnet(): - assert pscanner.is_subnet('192.168.1.0') == False - assert pscanner.is_subnet('192.168.0/24') == True + assert pscanner.is_subnet("192.168.1.0") == False + assert pscanner.is_subnet("192.168.0/24") == True + def test_hosts_in_subnet(): - assert len(pscanner.hosts_in_subnet('192.168.0.0/29')) == 6 \ No newline at end of file + assert len(pscanner.hosts_in_subnet("192.168.0.0/29")) == 6