refactor
This commit is contained in:
parent
c0f6a46f2a
commit
6e258b03ba
|
@ -14,3 +14,4 @@ Python 3.13
|
||||||
Переменные окружения (см. app/config/.env-template)
|
Переменные окружения (см. app/config/.env-template)
|
||||||
app/config/config.ini
|
app/config/config.ini
|
||||||
Пример конфиг файла см. app/config/config-template.ini
|
Пример конфиг файла см. app/config/config-template.ini
|
||||||
|
Положить файл со списком устройств в папку config
|
|
@ -36,7 +36,7 @@ def read_device_list() -> dict[str, dict[str, str]]:
|
||||||
return all_devices
|
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 = {
|
conn_conf: dict = {
|
||||||
"host": host,
|
"host": host,
|
||||||
"username": cfg.net_dev.user,
|
"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
|
conn_conf["username"] = cfg.net_dev.domain + "\\" + cfg.net_dev.user
|
||||||
try:
|
try:
|
||||||
connection: ConnectHandler = ConnectHandler(**conn_conf)
|
connection: ConnectHandler = ConnectHandler(**conn_conf)
|
||||||
log.info("connect to %r", host)
|
log.info("connect to %s %s", name, host)
|
||||||
return connection
|
return connection
|
||||||
except exceptions.NetmikoAuthenticationException:
|
except exceptions.NetmikoAuthenticationException:
|
||||||
log.warning("Authentication error %r ", host)
|
log.warning("Authentication error %s %s ", name, host)
|
||||||
except exceptions.NetmikoTimeoutException:
|
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:
|
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:
|
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(
|
connection = connect_to_device(
|
||||||
vendor="mikrotik",
|
vendor="mikrotik",
|
||||||
host=host,
|
host=host,
|
||||||
|
name=name,
|
||||||
)
|
)
|
||||||
if connection is None:
|
if connection is None:
|
||||||
return
|
return
|
||||||
|
@ -112,7 +113,7 @@ def save_snr_bcp(host: str, name: str) -> None:
|
||||||
connection.disconnect()
|
connection.disconnect()
|
||||||
log.info("disconnected from %r", name)
|
log.info("disconnected from %r", name)
|
||||||
if result == "NetmikoTimeoutException":
|
if result == "NetmikoTimeoutException":
|
||||||
log.warning("Timeout read config from %r", name)
|
log.warning("Timeout read config from %s", name)
|
||||||
return
|
return
|
||||||
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)
|
||||||
|
@ -130,9 +131,9 @@ def save_cisco_sb_bcp(host: str, name: str) -> None:
|
||||||
return
|
return
|
||||||
result = send_command(connection, "show running-config")
|
result = send_command(connection, "show running-config")
|
||||||
connection.disconnect()
|
connection.disconnect()
|
||||||
log.info("disconnected from %r", name)
|
log.info("disconnected from %s", name)
|
||||||
if result == "NetmikoTimeoutException":
|
if result == "NetmikoTimeoutException":
|
||||||
log.warning("Timeout read config from %r", name)
|
log.warning("Timeout read config from %s", name)
|
||||||
return
|
return
|
||||||
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)
|
||||||
|
|
14
app/main.py
14
app/main.py
|
@ -16,6 +16,13 @@ import os
|
||||||
def main():
|
def main():
|
||||||
check_bcp_repo()
|
check_bcp_repo()
|
||||||
device_dict = read_device_list()
|
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:
|
for device in device_dict:
|
||||||
current_device = device_dict[device]
|
current_device = device_dict[device]
|
||||||
if current_device["vendor"].lower() == "mikrotik":
|
if current_device["vendor"].lower() == "mikrotik":
|
||||||
|
@ -42,14 +49,13 @@ def main():
|
||||||
if cfg.git.push:
|
if cfg.git.push:
|
||||||
push_to_remote()
|
push_to_remote()
|
||||||
if cfg.mail.send:
|
if cfg.mail.send:
|
||||||
log_dir: str = os.path.join(os.path.abspath(os.path.dirname(__file__)), "logs")
|
if os.path.isfile(log_file):
|
||||||
file_path: str = os.path.join(log_dir, "logfile.log")
|
with open(log_file, 'r', encoding='utf-8') as file:
|
||||||
if os.path.isfile(file_path):
|
|
||||||
with open(file_path, 'r', encoding='utf-8') as file:
|
|
||||||
text = file.read()
|
text = file.read()
|
||||||
send_mail(text)
|
send_mail(text)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
log.info("Starting app")
|
log.info("Starting app")
|
||||||
schedule.every().day.at(cfg.bcp.start_at).do(main)
|
schedule.every().day.at(cfg.bcp.start_at).do(main)
|
||||||
|
|
Loading…
Reference in New Issue