Compare commits

...

3 Commits

Author SHA1 Message Date
sergey cd572b3c1a update gitignore 2025-06-18 12:24:35 +03:00
s.mostryukov d387d5849f add cisco_sb + snr
fix logging
2025-06-18 12:15:02 +03:00
sergey fe8decc0eb Merge pull request '1.0.10' (#1) from dev into main
Reviewed-on: #1
2025-06-17 21:10:45 +00:00
5 changed files with 96 additions and 36 deletions

2
.gitignore vendored
View File

@ -10,4 +10,4 @@ app/config/
!app/config/config-template.ini
!app/config/.env-template
app/backups/
!app/backups/.keep
!/app/backups/.keep

View File

@ -14,5 +14,4 @@ Python 3.13
Для запуска.
Переменные окружения (см. app/config/.env-template)
app/config/config.ini
Пример конфиг файла см. app/config/config-template.ini
Пример конфиг файла см. app/config/config-template.ini

View File

@ -47,12 +47,18 @@ def connect_to_device(vendor: str, host: str) -> ConnectHandler:
conn_conf["device_type"] = "mikrotik_routeros"
if cfg.net_dev.domain is not None:
conn_conf["username"] = cfg.net_dev.user + "@" + cfg.net_dev.domain
elif vendor.lower() == "cisco":
conn_conf["device_type"] = "cisco_ios"
elif vendor.lower() == "cisco_sb":
conn_conf["device_type"] = "cisco_s300"
if cfg.net_dev.domain is not None:
conn_conf["username"] = cfg.net_dev.domain + "\\" + cfg.net_dev.user
elif vendor.lower() == "snr":
conn_conf["device_type"] = "cisco_ios"
if cfg.net_dev.domain is not None:
conn_conf["username"] = cfg.net_dev.domain + "\\" + cfg.net_dev.user
elif vendor.lower() == "cisco":
conn_conf["device_type"] = "cisco_ios"
if cfg.net_dev.domain is not None:
conn_conf["username"] = cfg.net_dev.domain + "\\" + cfg.net_dev.user
try:
connection: ConnectHandler = ConnectHandler(**conn_conf)
log.info("connect to %r", host)
@ -66,7 +72,10 @@ def connect_to_device(vendor: str, host: str) -> ConnectHandler:
def send_command(connection: ConnectHandler, command: str) -> str:
result: str = connection.send_command(command)
try:
result: str = connection.send_command(command, read_timeout=30)
except exceptions.NetmikoTimeoutException:
result = "NetmikoTimeoutException"
return result
@ -79,12 +88,16 @@ def save_mikrotik_bcp(host: str, name: str) -> None:
if connection is None:
return
result = send_command(connection, "export terse show-sensitive")
connection.disconnect()
if result == "NetmikoTimeoutException":
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)
log.info("Backup saved")
connection.disconnect()
log.info("disconnected from %r", name)
add_file_and_commit(file_name=name)
@ -96,9 +109,53 @@ def save_snr_bcp(host: str, name: str) -> None:
if connection is None:
return
result = send_command(connection, "show running-config")
connection.disconnect()
log.info("disconnected from %r", name)
if result == "NetmikoTimeoutException":
log.warning("Timeout read config from %r", name)
return
with open(os.path.join(cfg.bcp.dir, name), "w") as f:
f.write(result)
log.info("Backup saved")
add_file_and_commit(file_name=name)
def save_cisco_sb_bcp(host: str, name: str) -> None:
connection = connect_to_device(
vendor="cisco_sb",
host=host,
)
if connection is None:
return
result = send_command(connection, "show running-config")
connection.disconnect()
log.info("disconnected from %r", name)
if result == "NetmikoTimeoutException":
log.warning("Timeout read config from %r", name)
return
with open(os.path.join(cfg.bcp.dir, name), "w") as f:
f.write(result)
log.info("Backup saved")
add_file_and_commit(file_name=name)
def save_cisco_bcp(host: str, name: str) -> None:
connection = connect_to_device(
vendor="cisco",
host=host,
)
if connection is None:
return
result = send_command(connection, "show running-config")
connection.disconnect()
log.info("disconnected from %r", name)
if result == "NetmikoTimeoutException":
log.warning("Timeout read config from %r", name)
return
with open(os.path.join(cfg.bcp.dir, name), "w") as f:
f.write(result)
log.info("Backup saved")
add_file_and_commit(file_name=name)

View File

@ -5,40 +5,21 @@ import configparser
import os
def default_logging(main_logging: logging) -> None:
log_dir: str = os.path.join(os.path.abspath(os.path.dirname(__file__)), "logs")
main_logging.basicConfig(
level=20,
datefmt="%Y-%m-%d %H:%M:%S",
format="[%(asctime)s.%(msecs)03d] %(module)-25s:%(lineno)4d | %(funcName)-20s| %(levelname)-8s | %(message)s",
)
file_handler = main_logging.FileHandler(os.path.join(log_dir, "logfile.log"))
file_handler.setLevel(30)
file_handler.setFormatter(
logging.Formatter(
"[%(asctime)s.%(msecs)03d] %(module)s:%(lineno)4d | %(funcName)s| %(levelname)s | %(message)s"
)
)
main_logging.getLogger().addHandler(file_handler)
main_logging.info("success configure logging")
default_logging(logging)
def configure_loging(main_logging: logging) -> None:
log_dir: str = os.path.join(os.path.abspath(os.path.dirname(__file__)), "logs")
logging.info("configure logging")
for handler in main_logging.root.handlers[:]:
main_logging.root.removeHandler(handler)
main_logging.basicConfig(
level=cfg.log.console_lvl,
datefmt=cfg.log.dare_format,
format=cfg.log.format,
)
handler = main_logging.FileHandler(os.path.join(log_dir, "logfile.log"))
handler.setLevel(cfg.log.file_lvl)
handler.setFormatter(logging.Formatter(cfg.log.format))
logging.getLogger().addHandler(handler)
logging.info("success configure logging")
file_handler = main_logging.FileHandler(os.path.join(log_dir, "logfile.log"))
file_handler.setLevel(cfg.log.file_lvl)
file_handler.setFormatter(logging.Formatter(cfg.log.format))
main_logging.getLogger().addHandler(file_handler)
main_logging.info("success configure logging")
def load_config() -> configparser.ConfigParser:

View File

@ -1,6 +1,12 @@
from config import cfg
import logging as log
from backup import read_device_list, save_mikrotik_bcp
from backup import (
read_device_list,
save_mikrotik_bcp,
save_snr_bcp,
save_cisco_sb_bcp,
save_cisco_bcp,
)
from repo import check_bcp_repo, push_to_remote
import schedule
import time
@ -14,7 +20,24 @@ def main():
if current_device["vendor"].lower() == "mikrotik":
save_mikrotik_bcp(host=current_device["ip"], name=current_device["name"])
elif current_device["vendor"].lower() == "snr":
save_mikrotik_bcp(host=current_device["ip"], name=current_device["name"])
save_snr_bcp(host=current_device["ip"], name=current_device["name"])
elif current_device["vendor"].lower() == "cisco":
if current_device["model"].lower() in [
"sg350-28mp",
"sg350-10mp",
"cbs350-8fp-2g",
"sg300-28p",
"sf302-08mp",
"sg300-10mpp",
"cbs250-48p-4g",
"cbs250-24p-4g",
"sg350-28sfp",
]:
save_cisco_sb_bcp(
host=current_device["ip"], name=current_device["name"]
)
else:
save_cisco_bcp(host=current_device["ip"], name=current_device["name"])
if cfg.git.push:
push_to_remote()