这些针对不同厂商交换机的批量配置备份 Python 脚本,核心目标是通过自动化方式简化网络设备配置的备份流程
厂商差异:命令与连接方式
不同厂商的交换机 CLI(命令行界面)语法不同,导致脚本中核心备份命令存在差异,具体如下:
厂商 | 连接方式 | 核心备份逻辑 |
---|---|---|
华为 | SSH | 进入系统视图( |
H3C | SSH | 进入系统视图( |
思科 | SSH | 进入特权模式( |
锐捷 | Telnet | 进入特权模式( |
Juniper | SSH | 进入独占配置模式( |
网工必修课,快报名加入
01
华为 HUAWEI
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}。')
相关学习推荐
02
华三 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}。')
相关学习推荐
03
思科 CISCO
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}。')
相学习推荐
04
锐捷 Ruijie
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}。')
相关学习推荐
05
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端口号
# 读取交换机列表
withopen(
'switch_list.txt'
) as f:
switch_list = f.read().splitlines()
# 遍历交换机列表,备份配置文件
for
switch_ip
in
switch_list:
(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)
(f
'交换机 {switch_ip} 的配置文件备份完成,保存为 {filename}。'
)
相关
脚本共同特点
-
都使用了相同的基本结构:
-
读取交换机IP列表文件
-
遍历列表连接每台设备
-
执行备份命令
-
保存配置文件
-
-
都需要预先准备:
-
安装Paramiko库(SSH脚本)
-
创建switch_list.txt文件
-
设置正确的用户名和密码
-
使用注意事项
-
确保网络连通性
-
检查防火墙设置允许SSH/Telnet连接
-
不同设备型号可能有不同的命令语法
-
生产环境使用前应在测试环境验证
-
考虑使用更安全的认证方式(如密钥认证)替代明文密码
✅这些脚本为网络管理员提供了自动化备份交换机配置的便捷方法,可以大大提高工作效率。