Add in pythonping
This commit is contained in:
@@ -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()
|
||||
|
||||
|
||||
@@ -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)
|
||||
Reference in New Issue
Block a user