分享

如何写批量备份交换机配置的Python脚本?5个厂商安排上!

 copy_left 2023-04-10 发布于四川

你好,这里是网络技术联盟站。

在昨天的文章中,我们介绍了20个华为路由器常用的Python脚本:

  • 20个华为路由器常用的Python脚本,网工写自动化脚本时候可以参考!

文章末尾的评论区中,有小伙伴提出想要一下批量备份交换机配置的脚本:

文章图片1

既然没有提出要哪个厂商,今天瑞哥就安排多厂商的脚本:

文章图片2

下面让我们直接开始!

一、华为

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('system-view')    time.sleep(1)    ssh.exec_command('save backup.cfg')    # 等待备份完成    time.sleep(5)    # 关闭SSH连接    ssh.close()    # 保存备份文件    filename = f'{switch_ip}.cfg'    os.system(f'tftp -g -r backup.cfg 10.0.0.1')    os.rename('backup.cfg', filename)    print(f'交换机 {switch_ip} 的配置文件备份完成,保存为 {filename}。')

三、思科

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}。')

四、锐捷

import telnetlibimport timeimport os# 配置Telnet连接信息username = b'username\n'  # Telnet用户名,注意要使用字节串password = b'password\n'  # Telnet密码,注意要使用字节串port = 23  # Telnet端口号# 读取交换机列表with open('switch_list.txt') as f:    switch_list = f.read().splitlines()# 遍历交换机列表,备份配置文件for switch_ip in switch_list:    print(f'正在备份交换机 {switch_ip} 的配置文件...')    # 建立Telnet连接    tn = telnetlib.Telnet(switch_ip, port=port, timeout=10)    # 登录    tn.read_until(b'>>User name:', timeout=5)    tn.write(username)    tn.read_until(b'>>User password:', timeout=5)    tn.write(password)    # 发送命令    tn.write(b'enable\n')    tn.write(password)    tn.write(b'display current-configuration\n')    time.sleep(5)    # 获取输出结果    output = tn.read_very_eager().decode('gbk')    # 关闭Telnet连接    tn.write(b'quit\n')    tn.close()    # 保存备份文件    filename = f'{switch_ip}.cfg'    with open(filename, 'w') as f:        f.write(output)    print(f'交换机 {switch_ip} 的配置文件备份完成,保存为 {filename}。')

五、Juniper

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('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脚本,欢迎在下方评论区留言!

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多