批量备份交换机配置的脚本

这些针对不同厂商交换机的批量配置备份 Python 脚本,核心目标是通过自动化方式简化网络设备配置的备份流程

图片

厂商差异:命令与连接方式

不同厂商的交换机 CLI(命令行界面)语法不同,导致脚本中核心备份命令存在差异,具体如下:

厂商

连接方式

核心备份逻辑

华为

SSH

进入系统视图(system-view)→通过 TFTP 备份配置到服务器(backup configuration to tftp)→本地从 TFTP 下载文件

H3C

SSH

进入系统视图(system-view)→保存配置到设备本地(save backup.cfg)→通过 TFTP 下载本地文件

思科

SSH

进入特权模式(enable)→关闭分页(terminal length 0,避免输出截断)→导出运行配置(show running-config

锐捷

Telnet

进入特权模式(enable)→导出当前配置(display current-configuration)→直接获取命令输出保存

Juniper

SSH

进入独占配置模式(configure exclusive)→导出配置(show)→获取输出保存

网工必修课,快报名加入

01

华为 HUAWEI

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

相关学习推荐


02

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

相关学习推荐


03

思科 CISCO

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

相学习推荐


    04

    锐捷 Ruijie

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

    相关学习推荐


        05

        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端口号# 读取交换机列表withopen('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'    withopen(filename, 'w') as f:        f.write(output)    print(f'交换机 {switch_ip} 的配置文件备份完成,保存为 {filename}。')
        

        相关


            脚本共同特点

            1. 都使用了相同的基本结构:

              • 读取交换机IP列表文件

              • 遍历列表连接每台设备

              • 执行备份命令

              • 保存配置文件

            2. 都需要预先准备:

              • 安装Paramiko库(SSH脚本)

              • 创建switch_list.txt文件

              • 设置正确的用户名和密码

            使用注意事项

            1. 确保网络连通性

            2. 检查防火墙设置允许SSH/Telnet连接

            3. 不同设备型号可能有不同的命令语法

            4. 生产环境使用前应在测试环境验证

            5. 考虑使用更安全的认证方式(如密钥认证)替代明文密码

             ✅这些脚本为网络管理员提供了自动化备份交换机配置的便捷方法,可以大大提高工作效率。

            评论
            添加红包

            请填写红包祝福语或标题

            红包个数最小为10个

            红包金额最低5元

            当前余额3.43前往充值 >
            需支付:10.00
            成就一亿技术人!
            领取后你会自动成为博主和红包主的粉丝 规则
            hope_wisdom
            发出的红包
            实付
            使用余额支付
            点击重新获取
            扫码支付
            钱包余额 0

            抵扣说明:

            1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
            2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

            余额充值