add debug connection
This commit is contained in:
parent
1c3ee16853
commit
744ce9f6d9
|
@ -37,12 +37,16 @@ def read_device_list() -> dict[str, dict[str, str]]:
|
||||||
|
|
||||||
|
|
||||||
def connect_to_device(vendor: str, host: str, name: str="") -> ConnectHandler:
|
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 = {
|
conn_conf: dict = {
|
||||||
"host": host,
|
"host": host,
|
||||||
"username": cfg.net_dev.user,
|
"username": cfg.net_dev.user,
|
||||||
"password": cfg.net_dev.pwd,
|
"password": cfg.net_dev.pwd,
|
||||||
"port": cfg.net_dev.ssh_port,
|
"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":
|
if vendor.lower() == "mikrotik":
|
||||||
conn_conf["device_type"] = "mikrotik_routeros"
|
conn_conf["device_type"] = "mikrotik_routeros"
|
||||||
if cfg.net_dev.domain is not None:
|
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:
|
def send_command(connection: ConnectHandler, command: str) -> str:
|
||||||
|
log.info("send command")
|
||||||
try:
|
try:
|
||||||
result: str = connection.send_command(command, read_timeout=cfg.net_dev.read_timeout)
|
result: str = connection.send_command(command, read_timeout=cfg.net_dev.read_timeout)
|
||||||
|
log.info('command send success')
|
||||||
except exceptions.NetmikoTimeoutException:
|
except exceptions.NetmikoTimeoutException:
|
||||||
result = "NetmikoTimeoutException"
|
result = "NetmikoTimeoutException"
|
||||||
except exceptions.ReadTimeout:
|
except exceptions.ReadTimeout:
|
||||||
|
@ -92,13 +98,14 @@ def save_mikrotik_bcp(host: str, name: str) -> None:
|
||||||
return
|
return
|
||||||
result = send_command(connection, "export terse show-sensitive")
|
result = send_command(connection, "export terse show-sensitive")
|
||||||
connection.disconnect()
|
connection.disconnect()
|
||||||
|
log.info("disconnected from %r", name)
|
||||||
if result == "NetmikoTimeoutException":
|
if result == "NetmikoTimeoutException":
|
||||||
log.warning("Timeout error %r", name)
|
log.warning("Timeout error %r", name)
|
||||||
return
|
return
|
||||||
elif result == "ReadTimeout":
|
elif result == "ReadTimeout":
|
||||||
log.warning("Timeout read config from %r", name)
|
log.warning("Timeout read config from %r", name)
|
||||||
return
|
return
|
||||||
log.info("disconnected from %r", name)
|
|
||||||
result = "\n".join(result.split("\n")[1:])
|
result = "\n".join(result.split("\n")[1:])
|
||||||
with open(os.path.join(cfg.bcp.dir, name), "w") as f:
|
with open(os.path.join(cfg.bcp.dir, name), "w") as f:
|
||||||
f.write(result)
|
f.write(result)
|
||||||
|
|
|
@ -68,6 +68,7 @@ class ConfigNetDev:
|
||||||
pwd: str = os.getenv("NET_DEV_PWD")
|
pwd: str = os.getenv("NET_DEV_PWD")
|
||||||
ssh_port: int = config["net_dev"].getint("ssh_port")
|
ssh_port: int = config["net_dev"].getint("ssh_port")
|
||||||
read_timeout: int = config["net_dev"].getint("read_timeout")
|
read_timeout: int = config["net_dev"].getint("read_timeout")
|
||||||
|
debug: bool = config["net_dev"].getboolean("debug")
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
|
|
Loading…
Reference in New Issue