Compare commits

...

9 Commits

Author SHA1 Message Date
sergey caa50eff25 Merge pull request 'dev-1.1.4' (#4) from dev into main
Reviewed-on: #4
2025-06-22 11:46:43 +00:00
s.mostryukov 2280196956 fix delay factor 2025-06-22 14:26:32 +03:00
s.mostryukov df0e4da626 add delay factor 2025-06-22 14:23:00 +03:00
s.mostryukov 86744e5d81 add debug connection in template 2025-06-22 14:11:55 +03:00
s.mostryukov fced69656e add debug connection in template 2025-06-22 13:25:56 +03:00
s.mostryukov 744ce9f6d9 add debug connection 2025-06-20 16:50:46 +03:00
s.mostryukov 1c3ee16853 fix exception 2025-06-19 14:49:29 +03:00
s.mostryukov 49e62892c8 read timeout in config 2025-06-19 13:56:32 +03:00
s.mostryukov e80821e6da docker-compose + exception fix 2025-06-18 16:38:54 +03:00
4 changed files with 38 additions and 12 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:
@ -61,7 +65,7 @@ def connect_to_device(vendor: str, host: str, name: str="") -> ConnectHandler:
conn_conf["username"] = cfg.net_dev.domain + "\\" + cfg.net_dev.user
try:
connection: ConnectHandler = ConnectHandler(**conn_conf)
log.info("connect to %s %s", name, host)
log.info("connect to %s %s %s", vendor, name, host)
return connection
except exceptions.NetmikoAuthenticationException:
log.warning("Authentication error %s %s ", name, host)
@ -72,10 +76,14 @@ 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=30)
result: str = connection.send_command(command, read_timeout=cfg.net_dev.read_timeout, delay_factor=cfg.net_dev.delay_factor)
log.info('command send success')
except exceptions.NetmikoTimeoutException:
result = "NetmikoTimeoutException"
except exceptions.ReadTimeout:
result = "ReadTimeout"
return result
@ -90,10 +98,13 @@ 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)
@ -113,7 +124,10 @@ def save_snr_bcp(host: str, name: str) -> None:
connection.disconnect()
log.info("disconnected from %r", name)
if result == "NetmikoTimeoutException":
log.warning("Timeout read config from %s", name)
log.warning("Timeout error %r", name)
return
elif result == "ReadTimeout":
log.warning("Timeout read config from %r", name)
return
with open(os.path.join(cfg.bcp.dir, name), "w") as f:
f.write(result)
@ -133,7 +147,10 @@ def save_cisco_sb_bcp(host: str, name: str) -> None:
connection.disconnect()
log.info("disconnected from %s", name)
if result == "NetmikoTimeoutException":
log.warning("Timeout read config from %s", name)
log.warning("Timeout error %r", name)
return
elif result == "ReadTimeout":
log.warning("Timeout read config from %r", name)
return
with open(os.path.join(cfg.bcp.dir, name), "w") as f:
f.write(result)
@ -152,6 +169,9 @@ def save_cisco_bcp(host: str, name: str) -> None:
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
with open(os.path.join(cfg.bcp.dir, name), "w") as f:

View File

@ -67,6 +67,9 @@ class ConfigNetDev:
domain: str = os.getenv("NET_DEV_DOMAIN")
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")
delay_factor: int = config["net_dev"].getint("delay_factor")
debug: bool = config["net_dev"].getboolean("debug")
@dataclass

View File

@ -37,8 +37,11 @@ pattern:(?P<name>\S+) (?P<ip>\d+\.\d+\.\d+\.\d+) (?P<vendor>\S+) (?P<model>\S+)
start_at: 18:59
[net_dev]
debug: False
# Порт на который подключаться по ssh
ssh_port: 22
read_timeout: 30
delay_factor: 2
[log]
# Уровень логов в консоль

View File

@ -1,10 +1,8 @@
version: '3.3'
services:
net-backup:
image: git.sm8255082.ru/osnova/net-backup:1.0.0
restart: always
image: git.sm8255082.ru/osnova/net-backup:1.1.0
restart: "no"
volumes:
- ./logs:/app/logs
- ./config:/app/config
@ -12,7 +10,9 @@ services:
environment:
- TZ=Europe/Moscow
- NET_DEV_USER=admin
- NET_DEV_DOMAIN=local
#- NET_DEV_DOMAIN=local
- NET_DEV_PWD=password
- GIT_USERNAME=git_user
- GIT_TOKEN=bla-bla-lba
#- GIT_USERNAME=git_user
#- GIT_TOKEN=bla-bla-lba
#- MAIL_USER=mail_user
#- MAIL_PWD=bla-bla-lba