如何备份交换机的配置(中国的备份厂商)

来源:网络技术联盟站 

链接:https://www.wljslmz.cn/20223.html

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

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

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

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

如何备份交换机的配置(中国的备份厂商)

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

如何备份交换机的配置(中国的备份厂商)

下面让我们直接开始!

一、华为

import paramiko
import time
import 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 paramiko
import time
import 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 paramiko
import time
import 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 telnetlib
import time
import 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 paramiko
import time
import 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脚本,欢迎在下方评论区留言!

如何备份交换机的配置(中国的备份厂商)

原创文章,作者:网络技术联盟站,如若转载,请注明出处:https://www.sudun.com/ask/34633.html

(0)
网络技术联盟站的头像网络技术联盟站
上一篇 2024年4月16日
下一篇 2024年4月16日

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注