black format

This commit is contained in:
Heath Brown
2022-11-01 11:40:52 -05:00
parent c6cd5b270f
commit 1ce01ace73
6 changed files with 28 additions and 16 deletions

View File

@@ -1,4 +1,4 @@
# SPDX-FileCopyrightText: 2022-present U.N. Owen <void@some.where> # SPDX-FileCopyrightText: 2022-present U.N. Owen <void@some.where>
# #
# SPDX-License-Identifier: MIT # SPDX-License-Identifier: MIT
__version__ = '0.0.1' __version__ = "0.0.1"

View File

@@ -12,12 +12,15 @@ GREEN = Fore.GREEN
RESET = Fore.RESET RESET = Fore.RESET
GRAY = Fore.LIGHTBLACK_EX GRAY = Fore.LIGHTBLACK_EX
def is_host_alive(host: str) -> bool: def is_host_alive(host: str) -> bool:
if ping(host, timeout=1, count=1).success(): if ping(host, timeout=1, count=1).success():
return True return True
else: else:
print(f"{GRAY}{host:15} is not alive {RESET}") print(f"{GRAY}{host:15} is not alive {RESET}")
return False return False
def is_port_open(host: str, port: int) -> bool: def is_port_open(host: str, port: int) -> bool:
try: try:
s = socket.socket() s = socket.socket()
@@ -31,14 +34,16 @@ def is_port_open(host: str, port: int) -> bool:
finally: finally:
s.close() s.close()
def is_subnet(ip: str) -> bool: def is_subnet(ip: str) -> bool:
if '/' in ip: if "/" in ip:
return True return True
else: else:
return False return False
def hosts_in_subnet(network: str) -> List: def hosts_in_subnet(network: str) -> List:
if not is_subnet: if not is_subnet:
print(f"{network} is not a network") print(f"{network} is not a network")
return list(ipaddress.ip_network(network).hosts()) return list(ipaddress.ip_network(network).hosts())

View File

@@ -3,7 +3,7 @@
# SPDX-License-Identifier: MIT # SPDX-License-Identifier: MIT
import sys import sys
if __name__ == '__main__': if __name__ == "__main__":
from .cli import pscanner from .cli import pscanner
sys.exit(pscanner()) sys.exit(pscanner())

View File

@@ -6,16 +6,19 @@ from pscanner import is_port_open, is_subnet, hosts_in_subnet, is_host_alive
from ..__about__ import __version__ from ..__about__ import __version__
@click.group(context_settings={'help_option_names': ['-h', '--help']}, invoke_without_command=True) @click.group(
@click.version_option(version=__version__, prog_name='pscanner') context_settings={"help_option_names": ["-h", "--help"]},
@click.argument('host') invoke_without_command=True,
@click.argument('port') )
@click.version_option(version=__version__, prog_name="pscanner")
@click.argument("host")
@click.argument("port")
@click.pass_context @click.pass_context
def pscanner(ctx: click.Context, host, port): def pscanner(ctx: click.Context, host, port):
if is_subnet(host): if is_subnet(host):
for ip in hosts_in_subnet(host): for ip in hosts_in_subnet(host):
if is_host_alive(str(ip)): if is_host_alive(str(ip)):
is_port_open(str(ip), port) is_port_open(str(ip), port)
else: else:
if is_host_alive(host): if is_host_alive(host):
is_port_open(host, port) is_port_open(host, port)

View File

@@ -25,6 +25,7 @@ classifiers = [
] ]
dependencies = [ dependencies = [
"click", "click",
"pythonping"
] ]
dynamic = ["version"] dynamic = ["version"]

View File

@@ -1,12 +1,15 @@
import pscanner import pscanner
import pytest import pytest
def test_is_port_open(): def test_is_port_open():
pass pass
def test_is_subnet(): def test_is_subnet():
assert pscanner.is_subnet('192.168.1.0') == False assert pscanner.is_subnet("192.168.1.0") == False
assert pscanner.is_subnet('192.168.0/24') == True assert pscanner.is_subnet("192.168.0/24") == True
def test_hosts_in_subnet(): def test_hosts_in_subnet():
assert len(pscanner.hosts_in_subnet('192.168.0.0/29')) == 6 assert len(pscanner.hosts_in_subnet("192.168.0.0/29")) == 6