Add in pythonping

This commit is contained in:
=
2022-11-01 11:13:15 -05:00
parent a90d83fd2e
commit c6cd5b270f
2 changed files with 16 additions and 8 deletions

View File

@@ -2,6 +2,7 @@
#
# SPDX-License-Identifier: MIT
import ipaddress
from pythonping import ping
import socket
from typing import List
from colorama import init, Fore
@@ -11,16 +12,22 @@ 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
def is_port_open(host: str, port: int) -> bool:
try:
s = socket.socket()
s.connect((host, int(port)))
except ConnectionRefusedError:
print(f"{GRAY}{host:15}:{port:15} is closed {RESET}", end='/r')
print(f"{GRAY}{host:15}:{port:5} is closed {RESET}")
except TimeoutError:
print(f"{GRAY}{host:15}:{port:15} is not alive {RESET}", end='/r')
print(f"{GRAY}{host:15}:{port:5} is not alive {RESET}")
else:
print(f"{GREEN}{host:15}:{port:15} is open {RESET}", end='/r')
print(f"{GREEN}{host:15}:{port:5} is open {RESET}")
finally:
s.close()

View File

@@ -2,7 +2,7 @@
#
# SPDX-License-Identifier: MIT
import click
from pscanner import is_port_open, is_subnet, hosts_in_subnet
from pscanner import is_port_open, is_subnet, hosts_in_subnet, is_host_alive
from ..__about__ import __version__
@@ -12,9 +12,10 @@ from ..__about__ import __version__
@click.argument('port')
@click.pass_context
def pscanner(ctx: click.Context, host, port):
click.echo(f'{host}, {port}')
if is_subnet(host):
for ip in hosts_in_subnet(host):
is_port_open(str(ip), port)
if is_host_alive(str(ip)):
is_port_open(str(ip), port)
else:
is_port_open(host, port)
if is_host_alive(host):
is_port_open(host, port)