Commit f97f05ba authored by Alberts S's avatar Alberts S
Browse files

Improve ssh logging

parent 7f843a1f
......@@ -5,8 +5,6 @@ import sys
import asyncssh
import yaml
logging.basicConfig(stream=sys.stdout, format="%(levelname)-8s:%(name)-24s.%(funcName)-40s:%(message)-s")
logging.getLogger("CapybaraNetty").setLevel(logging.INFO)
# https://stackoverflow.com/a/50731615
class MetaCapybaraNetty(type):
......@@ -25,21 +23,22 @@ class CapybaraNetty(metaclass=MetaCapybaraNetty):
def __init__(self):
self.config = {"external_targets_for_optimizations": [], "pinger_interval": 10}
self.setup_config()
logging.basicConfig(stream=sys.stdout, format="%(levelname)-8s:%(name)-24s.%(funcName)-40s:%(message)-s")
logging.getLogger(CapybaraNetty.__name__).setLevel(self.config["log_level"])
def setup_config(self):
with open("config.yaml", "r") as f:
self.config = self.config | yaml.safe_load(f)
return self.config
@staticmethod
async def ssh_exec(ip, username, command):
async def ssh_exec(self, ip, username, command):
self.__logger.debug(f"{ip:<16s} CMD: {command}")
async with asyncssh.connect(host=ip, username=username, known_hosts=None, connect_timeout=10) as conn:
result = await conn.run(command, check=True)
return result.stdout.strip("\n").split("\n"), result.stderr.strip("\n").split("\n")
@staticmethod
async def ssh_is_alive(ip, username):
async def ssh_is_alive(self, ip, username):
try:
async with asyncssh.connect(host=ip, username=username, known_hosts=None, connect_timeout=3) as conn:
await conn.run("true", check=True)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment