dev-1.1.4 #4
|
@ -37,12 +37,16 @@ def read_device_list() -> dict[str, dict[str, str]]:
|
||||||
|
|
||||||
|
|
||||||
def connect_to_device(vendor: str, host: str, name: str="") -> ConnectHandler:
|
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 = {
|
conn_conf: dict = {
|
||||||
"host": host,
|
"host": host,
|
||||||
"username": cfg.net_dev.user,
|
"username": cfg.net_dev.user,
|
||||||
"password": cfg.net_dev.pwd,
|
"password": cfg.net_dev.pwd,
|
||||||
"port": cfg.net_dev.ssh_port,
|
"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":
|
if vendor.lower() == "mikrotik":
|
||||||
conn_conf["device_type"] = "mikrotik_routeros"
|
conn_conf["device_type"] = "mikrotik_routeros"
|
||||||
if cfg.net_dev.domain is not None:
|
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
|
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 %s %s", name, host)
|
log.info("connect to %s %s %s", vendor, name, host)
|
||||||
return connection
|
return connection
|
||||||
except exceptions.NetmikoAuthenticationException:
|
except exceptions.NetmikoAuthenticationException:
|
||||||
log.warning("Authentication error %s %s ", name, host)
|
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:
|
def send_command(connection: ConnectHandler, command: str) -> str:
|
||||||
|
log.info("send command")
|
||||||
try:
|
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:
|
except exceptions.NetmikoTimeoutException:
|
||||||
result = "NetmikoTimeoutException"
|
result = "NetmikoTimeoutException"
|
||||||
|
except exceptions.ReadTimeout:
|
||||||
|
result = "ReadTimeout"
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
@ -90,10 +98,13 @@ def save_mikrotik_bcp(host: str, name: str) -> None:
|
||||||
return
|
return
|
||||||
result = send_command(connection, "export terse show-sensitive")
|
result = send_command(connection, "export terse show-sensitive")
|
||||||
connection.disconnect()
|
connection.disconnect()
|
||||||
|
log.info("disconnected from %r", name)
|
||||||
if result == "NetmikoTimeoutException":
|
if result == "NetmikoTimeoutException":
|
||||||
|
log.warning("Timeout error %r", name)
|
||||||
|
return
|
||||||
|
elif result == "ReadTimeout":
|
||||||
log.warning("Timeout read config from %r", name)
|
log.warning("Timeout read config from %r", name)
|
||||||
return
|
return
|
||||||
log.info("disconnected from %r", name)
|
|
||||||
result = "\n".join(result.split("\n")[1:])
|
result = "\n".join(result.split("\n")[1:])
|
||||||
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)
|
||||||
|
@ -113,7 +124,10 @@ 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 %s", name)
|
log.warning("Timeout error %r", name)
|
||||||
|
return
|
||||||
|
elif result == "ReadTimeout":
|
||||||
|
log.warning("Timeout read config from %r", 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)
|
||||||
|
@ -133,7 +147,10 @@ def save_cisco_sb_bcp(host: str, name: str) -> None:
|
||||||
connection.disconnect()
|
connection.disconnect()
|
||||||
log.info("disconnected from %s", name)
|
log.info("disconnected from %s", name)
|
||||||
if result == "NetmikoTimeoutException":
|
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
|
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)
|
||||||
|
@ -152,6 +169,9 @@ def save_cisco_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 error %r", name)
|
||||||
|
return
|
||||||
|
elif result == "ReadTimeout":
|
||||||
log.warning("Timeout read config from %r", name)
|
log.warning("Timeout read config from %r", 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:
|
||||||
|
|
|
@ -67,6 +67,9 @@ class ConfigNetDev:
|
||||||
domain: str = os.getenv("NET_DEV_DOMAIN")
|
domain: str = os.getenv("NET_DEV_DOMAIN")
|
||||||
pwd: str = os.getenv("NET_DEV_PWD")
|
pwd: str = os.getenv("NET_DEV_PWD")
|
||||||
ssh_port: int = config["net_dev"].getint("ssh_port")
|
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
|
@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
|
start_at: 18:59
|
||||||
|
|
||||||
[net_dev]
|
[net_dev]
|
||||||
|
debug: False
|
||||||
# Порт на который подключаться по ssh
|
# Порт на который подключаться по ssh
|
||||||
ssh_port: 22
|
ssh_port: 22
|
||||||
|
read_timeout: 30
|
||||||
|
delay_factor: 2
|
||||||
|
|
||||||
[log]
|
[log]
|
||||||
# Уровень логов в консоль
|
# Уровень логов в консоль
|
||||||
|
|
|
@ -1,10 +1,8 @@
|
||||||
version: '3.3'
|
version: '3.3'
|
||||||
|
|
||||||
services:
|
services:
|
||||||
|
|
||||||
net-backup:
|
net-backup:
|
||||||
image: git.sm8255082.ru/osnova/net-backup:1.0.0
|
image: git.sm8255082.ru/osnova/net-backup:1.1.0
|
||||||
restart: always
|
restart: "no"
|
||||||
volumes:
|
volumes:
|
||||||
- ./logs:/app/logs
|
- ./logs:/app/logs
|
||||||
- ./config:/app/config
|
- ./config:/app/config
|
||||||
|
@ -12,7 +10,9 @@ services:
|
||||||
environment:
|
environment:
|
||||||
- TZ=Europe/Moscow
|
- TZ=Europe/Moscow
|
||||||
- NET_DEV_USER=admin
|
- NET_DEV_USER=admin
|
||||||
- NET_DEV_DOMAIN=local
|
#- NET_DEV_DOMAIN=local
|
||||||
- NET_DEV_PWD=password
|
- NET_DEV_PWD=password
|
||||||
- GIT_USERNAME=git_user
|
#- GIT_USERNAME=git_user
|
||||||
- GIT_TOKEN=bla-bla-lba
|
#- GIT_TOKEN=bla-bla-lba
|
||||||
|
#- MAIL_USER=mail_user
|
||||||
|
#- MAIL_PWD=bla-bla-lba
|
Loading…
Reference in New Issue