Compare commits
9 Commits
9f8a695ba5
...
caa50eff25
Author | SHA1 | Date |
---|---|---|
|
caa50eff25 | |
|
2280196956 | |
|
df0e4da626 | |
|
86744e5d81 | |
|
fced69656e | |
|
744ce9f6d9 | |
|
1c3ee16853 | |
|
49e62892c8 | |
|
e80821e6da |
|
@ -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:
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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]
|
||||
# Уровень логов в консоль
|
||||
|
|
|
@ -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
|
Loading…
Reference in New Issue