深圳市鸿华锐信息技术有限公司欢迎您!
服务创新提升价值!
百度地图| SiteMap| 知识库| 联系我们
全国服务热线:0755-88855786   深圳:13058107600

知识库

全国服务热线:
0755-88855786
深圳:
13058107600

五家网络设备厂商批量备份交换机配置的Python脚本


1、华为:

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

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

3、思科

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

4、锐捷:

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

5、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设备的配置命令几乎一致,思科和锐捷设备的配置命令也几乎一致,所以写出来的脚本也差不多一样,大家在练习或者使用的时候注意一下就好。
————————————————
原文链接:https://blog.csdn.net/weixin_43025343/article/details/129915777




QQ客服在线咨询
业务咨询:
223883921
QQ客服在线咨询
技术支持:
56802890
联系方式
联系电话:
0755-88855786
微信客服
扫码二维码
返回顶部
✉ 

留言