你好,这里是网络技术联盟站。 在昨天的文章中,我们介绍了20个华为路由器常用的Python脚本:
文章末尾的评论区中,有小伙伴提出想要一下批量备份交换机配置的脚本: ![]() 既然没有提出要哪个厂商,今天瑞哥就安排多厂商的脚本: ![]() 下面让我们直接开始! 一、华为import paramikoimport timeimport os# 创建SSH客户端对象ssh = paramiko.SSHClient()# 自动添加主机密钥ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())# 配置SSH连接信息username = 'username' # SSH用户名password = 'password' # SSH密码port = 22 # SSH端口号# 读取交换机列表with open('switch_list.txt') as f: switch_list = f.read().splitlines()# 遍历交换机列表,备份配置文件for switch_ip in switch_list: print(f'正在备份交换机 {switch_ip} 的配置文件...') # 建立SSH连接 ssh.connect(switch_ip, port=port, username=username, password=password, timeout=10) # 发送命令 ssh.exec_command('system-view') time.sleep(1) ssh.exec_command('backup configuration to tftp 10.0.0.1 config.cfg') # 等待备份完成 time.sleep(5) # 关闭SSH连接 ssh.close() # 保存备份文件 filename = f'{switch_ip}.cfg' os.system(f'tftp -g -r config.cfg 10.0.0.1') os.rename('config.cfg', filename) print(f'交换机 {switch_ip} 的配置文件备份完成,保存为 {filename}。') 二、H3C
三、思科import paramikoimport timeimport os# 创建SSH客户端对象ssh = paramiko.SSHClient()# 自动添加主机密钥ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())# 配置SSH连接信息username = 'username' # SSH用户名password = 'password' # SSH密码port = 22 # SSH端口号# 读取交换机列表with open('switch_list.txt') as f: switch_list = f.read().splitlines()# 遍历交换机列表,备份配置文件for switch_ip in switch_list: print(f'正在备份交换机 {switch_ip} 的配置文件...') # 建立SSH连接 ssh.connect(switch_ip, port=port, username=username, password=password, timeout=10) # 发送命令 ssh.exec_command('enable') time.sleep(1) ssh.exec_command('terminal length 0') time.sleep(1) ssh.exec_command('show running-config') time.sleep(5) # 获取输出结果 output = ssh.recv(65535).decode('ascii') # 关闭SSH连接 ssh.close() # 保存备份文件 filename = f'{switch_ip}.cfg' with open(filename, 'w') as f: f.write(output) print(f'交换机 {switch_ip} 的配置文件备份完成,保存为 {filename}。') 四、锐捷
五、Juniperimport paramikoimport timeimport os# 创建SSH客户端对象ssh = paramiko.SSHClient()# 自动添加主机密钥ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())# 配置SSH连接信息username = 'username' # SSH用户名password = 'password' # SSH密码port = 22 # SSH端口号# 读取交换机列表with open('switch_list.txt') as f: switch_list = f.read().splitlines()# 遍历交换机列表,备份配置文件for switch_ip in switch_list: print(f'正在备份交换机 {switch_ip} 的配置文件...') # 建立SSH连接 ssh.connect(switch_ip, port=port, username=username, password=password, timeout=10) # 发送命令 ssh.exec_command('configure exclusive') time.sleep(1) ssh.exec_command('show') time.sleep(5) # 获取输出结果 output = ssh.recv(65535).decode('ascii') # 关闭SSH连接 ssh.close() # 保存备份文件 filename = f'{switch_ip}.cfg' with open(filename, 'w') as f: f.write(output) print(f'交换机 {switch_ip} 的配置文件备份完成,保存为 {filename}。') 六、脚本说明以上脚本使用了Paramiko库来实现SSH连接和命令执行,使用了os库来进行文件操作。在使用脚本前,请确保已经安装好Paramiko库并且已经将需要备份的交换机的IP地址列表保存在名为switch_list.txt的文件中,每行一个IP地址。另外,需要将脚本中的username和password替换成实际的值。注意,Juniper交换机的备份命令与其他厂商的命令略有不同。 另外,由于华为和H3C设备的配置命令几乎一致,思科和锐捷设备的配置命令也几乎一致,所以写出来的脚本也差不多一样,大家在练习或者使用的时候注意一下就好。 总结本文给大家介绍了五个厂商(华为、H3C、思科、锐捷、Juniper)批量备份交换机配置的Python脚本,希望对您有所帮助!如果您还想了解或者学习更多针对网络设备配置的Python脚本,欢迎在下方评论区留言! |
|