摘要
上一章节已经完成了命令筛选和设备筛选部分的代码,接下来就是将其整合到执行器中,今天的章节中,我会在视频里带领大家完善SSHExecutor,大家也可以体会一下写代码的过程,以及其中体现的编程思想,对后续的学习和发展会有很大帮助。
SSHExecutor
这里再次巩固一下整体巡检的设计图,如下所示:
初始化连接
之前章节已经实现的SSHExecutor初始化代码如下:
class SSHExecutor:
def __init__(
self,
host: str = "",
username: str = "",
password: str = None,
secret: str = "",
port: str = None,
device_type: str = "",
conn_timeout: int = 10,
auth_timeout: int = None,
banner_timeout: int = 15,
log_file: str = "ssh_executor.log",
log_level: str = None,
log_format: str = None,
) -> None:
self.host = host
self.port = port
self.username = username
self.password = password
self.secret = secret
self.device_type = device_type
self.conn_timeout = conn_timeout
self.auth_timeout = auth_timeout
self.banner_timeout = banner_timeout
import logging
if log_level is None:
log_level = logging.ERROR
if log_format is None:
log_format = "%(asctime)s %(levelname)s %(name)s %(message)s"
logging.basicConfig(filename=log_file, level=log_level, format=log_format)
self.logger = logging.getLogger(__name__)
self.conn = self.connect()
def connect(self):
try:
conn = ConnUnify(**self.__dict__)
msg = f"Netmiko connection successful to {self.host}:{self.port}"
self.logger.info(msg)
return conn
except Exception as e:
self.logger.error(str(e))
def execute(self):
pass
def fetch_object(self):
pass
def fetch_action(self):
pass