add debug connection

This commit is contained in:
s.mostryukov 2025-06-20 16:50:46 +03:00
parent 1c3ee16853
commit 744ce9f6d9
2 changed files with 9 additions and 1 deletions

View File

@ -37,12 +37,16 @@ def read_device_list() -> dict[str, dict[str, str]]:
def connect_to_device(vendor: str, host: str, name: str="") -> ConnectHandler:
log_dir: str = os.path.join(os.path.abspath(os.path.dirname(__file__)), "logs")
conn_conf: dict = {
"host": host,
"username": cfg.net_dev.user,
"password": cfg.net_dev.pwd,
"port": cfg.net_dev.ssh_port,
}
if cfg.net_dev.debug:
conn_conf["session_log"] = os.path.join(log_dir, 'session_log.log')
if vendor.lower() == "mikrotik":
conn_conf["device_type"] = "mikrotik_routeros"
if cfg.net_dev.domain is not None:
@ -72,8 +76,10 @@ def connect_to_device(vendor: str, host: str, name: str="") -> ConnectHandler:
def send_command(connection: ConnectHandler, command: str) -> str:
log.info("send command")
try:
result: str = connection.send_command(command, read_timeout=cfg.net_dev.read_timeout)
log.info('command send success')
except exceptions.NetmikoTimeoutException:
result = "NetmikoTimeoutException"
except exceptions.ReadTimeout:
@ -92,13 +98,14 @@ def save_mikrotik_bcp(host: str, name: str) -> None:
return
result = send_command(connection, "export terse show-sensitive")
connection.disconnect()
log.info("disconnected from %r", name)
if result == "NetmikoTimeoutException":
log.warning("Timeout error %r", name)
return
elif result == "ReadTimeout":
log.warning("Timeout read config from %r", name)
return
log.info("disconnected from %r", name)
result = "\n".join(result.split("\n")[1:])
with open(os.path.join(cfg.bcp.dir, name), "w") as f:
f.write(result)

View File

@ -68,6 +68,7 @@ class ConfigNetDev:
pwd: str = os.getenv("NET_DEV_PWD")
ssh_port: int = config["net_dev"].getint("ssh_port")
read_timeout: int = config["net_dev"].getint("read_timeout")
debug: bool = config["net_dev"].getboolean("debug")
@dataclass