背景:核心汇聚设备工作时间变更配置后不建议立即保存配置,保存配置占用设备性能可能出现设备异常,为此通过python脚本每天凌晨4点对设备批量保存,账号密码储存在excel表格中(也可以存储在数据库,表格相对简单对,数据库需要另外的服务器)。
python环境:python 3.9
系统环境:win10
import paramiko
import time
from queue import Queue
import threading
import openpyxl
threads = []
def save_config(ip, dev_type, username, password, output_q):
ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
if dev_type == 'h3c':
try:
ssh_client.connect(hostname=ip, username=username, password=password, look_for_keys=False)
ssh_shell = ssh_client.invoke_shell()
print('......H3C Dev login successful..........')
ssh_shell.send("save" + '\n')
time.sleep(1)
ssh_shell.send("y" + '\n')
time.sleep(1)
ssh_shell.send('\n')
time.sleep(1)
ssh_shell.send("y" + '\n')
time.sleep(5)
output = ssh_shell.recv(