Python代码优化 - 自动化设备配置和监控脚本
utf- python3
导入所需模块
from paramiko import SSHClient, AutoAddPolicy # SSH连接库 from time import sleep, localtime, strftime, time # 时间相关库 from ncclient import manager # NETCONF连接库
设备信息
DEV_IP = '10.1.0.6' # 设备IP地址
SSH_USR = 'sshuser' # SSH用户名
SSH_PWD = 'Huawei@123' # SSH密码
NC_USR = 'netconf' # NETCONF用户名
NC_PWD = 'Huawei@123' # NETCONF密码
COMMAND_FILE = 'command.txt'
NAME_TIME = strftime('%Y_%m_%d', localtime(time())) + '_X_T1_AGG1_vrpcfg.zip'
NC_CONF_SYSLOG = '''
连接设备
def get_ssh_connection(hostname, username, password): ssh_con = SSHClient() # 创建SSH连接对象 ssh_con.set_missing_host_key_policy(AutoAddPolicy) # 未知主机密钥的策略 ssh_con.connect(hostname=hostname, username=username, password=password) # 连接设备 return ssh_con
获取SSH交互式终端对象
def get_cli(ssh_con): cli = ssh_con.invoke_shell() # SSH交互式终端对象 cli.send('n\n') #操作步骤 cli.send("screen-length 0 temporary\n") # 临时关闭分页显示 cli.recv(65535) return cli
运行命令
def run_command(cli, command): cli.send(command) # 发送命令 sleep(1) dis_this = cli.recv(65535).decode() # 接收命令输出 if command == 'display fan\n': # 如果是显示风扇命令 state = dis_this.find("Normal") # 判断是否存在"Normal" if state < 0: print("All fans are faulty") # 如果不存在,输出所有风扇异常 print(dis_this) # 输出命令输出内容
进入netconf模式
def enter_netconf_mode(cli): cli.send('sys\n') # 进入系统视图 cli.send('netconf\n') # 进入NETCONF模式 cli.send('y\n') # 确认进入NETCONF模式 cli.send('source ip 10.1.0.6 port 830\n') # 设置NETCONF连接参数 sleep(10) dis_this = cli.recv(65535).decode() # 接收命令输出 print(dis_this) # 输出命令输出内容
保存配置
def save_running_config(cli, name_time): cmd = 'save ' + name_time # 构造保存配置命令 cli.send("{} ".format(cmd)) # 发送保存配置命令 cli.send('y\n') # 确认保存 cli.send('y\n') sleep(1) dis_this = cli.recv(65535).decode() # 接收命令输出 print(dis_this) # 输出命令输出内容
下载配置文件
def download_config_file(sftp_con, name_time): l_path = name_time # 本地保存路径 r_path = '/' + l_path # 远程保存路径 sftp_con.get(localpath=l_path, remotepath=r_path) # 下载远程文件到本地 sleep(1)
编辑syslog服务器地址
def edit_loghost(ip, usr, pwd, conf): with manager.connect_ssh( host=ip, username=usr, password=pwd, hostkey_verify=False, device_params={"name": "huaweiyang"} ) as m: m.edit_config(config=conf, target='running') # 编辑配置 sleep(1)
数据循环处理
def datacom_loop(ip, usr, pwd): now_time = time() # 记录当前时间 ssh_con = get_ssh_connection(ip, usr, pwd) cli = get_cli(ssh_con) with ssh_con: with cli: f = open(COMMAND_FILE, 'r') # 打开命令列表文件 cmd_list = f.readlines() # 读取命令列表 for cmd in cmd_list: run_command(cli, cmd) # 运行命令 f.close() # 关闭文件 save_running_config(cli, NAME_TIME) # 保存配置 sftp_con = ssh_con.open_sftp() # 创建SFTP连接对象 download_config_file(sftp_con, NAME_TIME) # 下载配置 sftp_con.close() # 关闭SFTP连接 if last_time - now_time < 300: # 如果时间间隔小于5分钟 save_running_config(cli, NAME_TIME) # 保存配置 sftp_con = ssh_con.open_sftp() # 创建SFTP连接对象 download_config_file(sftp_con, NAME_TIME) # 下载配置 sftp_con.close() # 关闭SFTP连接 sleep(300) # 休眠300秒
进入netconf模式
def enter_netconf(ip, usr, pwd): ssh_con = get_ssh_connection(ip, usr, pwd) cli = get_cli(ssh_con) with ssh_con: with cli: enter_netconf_mode(cli) # 进入NETCONF模式
if name == "main": try: enter_netconf(DEV_IP, SSH_USR, SSH_PWD) # 进入NETCONF模式 edit_loghost(DEV_IP, NC_USR, NC_PWD, NC_CONF_SYSLOG) # 编辑syslog服务器地址 datacom_loop(DEV_IP, SSH_USR, SSH_PWD) # 数据循环处理 except KeyboardInterrupt: print("Monitoring has stopped!") # 监控停止提示
原文地址: https://www.cveoy.top/t/topic/oa9F 著作权归作者所有。请勿转载和采集!