This commit is contained in:
s.mostryukov 2025-06-18 16:06:28 +03:00
parent c0f6a46f2a
commit 6e258b03ba
3 changed files with 20 additions and 12 deletions

View File

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

View File

@ -36,7 +36,7 @@ def read_device_list() -> dict[str, dict[str, str]]:
return all_devices
def connect_to_device(vendor: str, host: str) -> ConnectHandler:
def connect_to_device(vendor: str, host: str, name: str="") -> ConnectHandler:
conn_conf: dict = {
"host": host,
"username": cfg.net_dev.user,
@ -61,14 +61,14 @@ def connect_to_device(vendor: str, host: str) -> ConnectHandler:
conn_conf["username"] = cfg.net_dev.domain + "\\" + cfg.net_dev.user
try:
connection: ConnectHandler = ConnectHandler(**conn_conf)
log.info("connect to %r", host)
log.info("connect to %s %s", name, host)
return connection
except exceptions.NetmikoAuthenticationException:
log.warning("Authentication error %r ", host)
log.warning("Authentication error %s %s ", name, host)
except exceptions.NetmikoTimeoutException:
log.warning("Connection time out error %r ", host)
log.warning("Connection time out error %s %s ", name, host)
except Exception as e:
log.warning("Connection error %r: %r", host, e)
log.warning("Connection error %s %s: %s", name, host, e)
def send_command(connection: ConnectHandler, command: str) -> str:
@ -84,6 +84,7 @@ def save_mikrotik_bcp(host: str, name: str) -> None:
connection = connect_to_device(
vendor="mikrotik",
host=host,
name=name,
)
if connection is None:
return
@ -112,7 +113,7 @@ 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 %r", name)
log.warning("Timeout read config from %s", name)
return
with open(os.path.join(cfg.bcp.dir, name), "w") as f:
f.write(result)
@ -130,9 +131,9 @@ def save_cisco_sb_bcp(host: str, name: str) -> None:
return
result = send_command(connection, "show running-config")
connection.disconnect()
log.info("disconnected from %r", name)
log.info("disconnected from %s", name)
if result == "NetmikoTimeoutException":
log.warning("Timeout read config from %r", name)
log.warning("Timeout read config from %s", name)
return
with open(os.path.join(cfg.bcp.dir, name), "w") as f:
f.write(result)

View File

@ -16,6 +16,13 @@ import os
def main():
check_bcp_repo()
device_dict = read_device_list()
log_dir: str = os.path.join(os.path.abspath(os.path.dirname(__file__)), "logs")
log_file: str = os.path.join(log_dir, "logfile.log")
if os.path.isfile(log_file):
with open(log_file, 'w') as file:
file.write('')
log.info("log file clear")
for device in device_dict:
current_device = device_dict[device]
if current_device["vendor"].lower() == "mikrotik":
@ -42,14 +49,13 @@ def main():
if cfg.git.push:
push_to_remote()
if cfg.mail.send:
log_dir: str = os.path.join(os.path.abspath(os.path.dirname(__file__)), "logs")
file_path: str = os.path.join(log_dir, "logfile.log")
if os.path.isfile(file_path):
with open(file_path, 'r', encoding='utf-8') as file:
if os.path.isfile(log_file):
with open(log_file, 'r', encoding='utf-8') as file:
text = file.read()
send_mail(text)
if __name__ == "__main__":
log.info("Starting app")
schedule.every().day.at(cfg.bcp.start_at).do(main)