hasspy/hasspy/utils.py

28 lines
758 B
Python

import logging
import socket
from subprocess import run
from typing import Tuple
log = logging.getLogger(__name__)
def run_command(cmd: list[str]) -> Tuple[int, str]:
log.debug(f"Running command {' '.join(cmd)}")
proc = run(cmd, capture_output=True)
if proc.returncode != 0:
return proc.returncode, ""
return proc.returncode, proc.stdout.decode("utf-8")
def test_connection(host: str, port: int) -> bool:
log.debug(f"Testing connection to {host}:{port}")
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(1)
try:
sock.connect((host, port))
sock.close()
return True
except socket.error:
log.warning(f"Could not reach {host}:{port}")
return False