当前位置:首页>python>python备份h3c交换机配置文件(tftp方式)

python备份h3c交换机配置文件(tftp方式)

  • 2026-10-10 19:01:50
python备份h3c交换机配置文件(tftp方式)
  各位兄弟,昨天分享了通过共享文件夹方式备份配置文件,今天我们通过tftp方式来备份配置文件,首先我们需要在一台电脑上安装tftp软件,我下载了一个:
打开tftpd32应用程序:
设置Current Directory,这个是保存配置文件的目录,我设置好了。另外操作系统必须要放行22端口,我是直接关闭了防火墙。
这样子电脑端就设置好了,接下来我们就来看下python脚本:
from netmiko import ConnectHandler
from concurrent.futures import ThreadPoolExecutor
import datetime
import time

# ===================== 【必须修改的配置】 =====================
# 交换机列表
SWITCH_LIST = [
    {
"device_type": "hp_comware",  # H3C 专用
"host": "10.0.0.21",
"username": "admin",
"password": "***********",
"timeout": 15,
    },
{
'device_type': 'hp_comware',
'host': '10.0.0.9',
'port': 22,
'username': 'admin',
'password': '************',
'timeout': 15,
    },

    {
'device_type': 'hp_comware',
'host': '10.0.0.27',
'port': 22,
'username': 'admin',
'password': '*************',
'timeout': 15,
    },

    {
'device_type': 'hp_comware',
'host': '10.0.0.25',
'port': 22,
'username': 'admin',
'password': '*************',
'timeout': 15,
    },
# 可以继续添加更多交换机...
]

# TFTP 服务器地址(你的电脑IP,必须开启TFTP服务)
TFTP_SERVER = "192.168.1.189"
# 并发线程数
MAX_WORKERS = 10
# ==============================================================
def backup_h3c_config(switch):
    ip = switch["host"]
    current_time = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
    backup_filename = f"{ip}_{current_time}_config.cfg"
print(f"\n===== 开始备份:{ip} =====")

try:
# 连接交换机
with ConnectHandler(**switch) as conn:
# 保存配置
conn.send_command("save force", expect_string=r">|]")
            time.sleep(1)

# 通过 TFTP 上传配置文件
cmd = f"tftp {TFTP_SERVER} put flash:/startup.cfg {backup_filename}"
output = conn.send_command(cmd)

# 判断是否备份成功
if "Success" in output or "success" in output or "完成" in output or "%" in output:
print(f"✅ {ip} 备份成功 → {backup_filename}")
return f"✅ {ip} 备份成功"
else:
print(f"❌ {ip} 备份失败")
print(f"调试输出:{output}")
return f"❌ {ip} 备份失败"
except Exception as e:
print(f"❌ {ip} 连接/备份失败:{str(e)}")
return f"❌ {ip} 异常:{str(e)}"
if __name__ == "__main__":
print("=" * 60)
print("      H3C 交换机批量备份程序(纯 TFTP 版)")
print("=" * 60)

    start_time = datetime.datetime.now()

# 多线程并发备份
with ThreadPoolExecutor(max_workers=MAX_WORKERS) as executor:
        results = executor.map(backup_h3c_config, SWITCH_LIST)

# 输出结果
print("\n" + "=" * 60)
print("备份结果汇总:")
for res in results:
print(res)

    end_time = datetime.datetime.now()
print(f"\n总耗时:{(end_time - start_time).total_seconds():.2f} 秒")
print("✅ 所有任务完成!")
其实这个程序也有问题,就是交换机上tftp命令,可能有些老版本的交换机不一定支持,但是新版本的交换机基本没问题,大家也可以测试下,最后我们来看下执行结果:

D:\python\pythonProject3\.venv\Scripts\python.exe D:\python\pythonProject3\备份交换机配置文件2(tftp).py 

============================================================

      H3C 交换机批量备份程序(纯 TFTP 版)

============================================================

===== 开始备份:10.0.0.21 =====

===== 开始备份:10.0.0.9 =====

===== 开始备份:10.0.0.27 =====

===== 开始备份:10.0.0.25 =====

✅ 10.0.0.21 备份成功 → 10.0.0.21_20260330_075212_config.cfg

✅ 10.0.0.25 备份成功 → 10.0.0.25_20260330_075212_config.cfg

✅ 10.0.0.27 备份成功 → 10.0.0.27_20260330_075212_config.cfg

✅ 10.0.0.9 备份成功 → 10.0.0.9_20260330_075212_config.cfg

============================================================

备份结果汇总:

✅ 10.0.0.21 备份成功

✅ 10.0.0.9 备份成功

✅ 10.0.0.27 备份成功

✅ 10.0.0.25 备份成功

总耗时:5.63 秒

✅ 所有任务完成!

进程已结束,退出代码为 0

再看下tftp服务器:

已经保存成功:

ok,今天就分享到这里。

最新文章

随机文章