You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Cisco WLC 9800配置备份文件内容截断问题求助

问题:Cisco WLC 9800配置备份缺失内容,同脚本在4500交换机正常

使用Netmiko脚本登录Cisco WLC 9800执行show run并保存到文件,但文件丢失大量配置。调试确认命令已获取完整配置,却无法完整写入;但对Cisco 4500交换机执行相同操作可正常备份完整配置。

现有脚本

device = {
   'device_type': 'cisco_ios',
   'ip': 'IP',
   'username': 'username',
   'password': 'password',
    }
##opening IP file
ipfile = open("C:\XXXX\switch-ips.txt")
print("Script to take backup of devices, Please enter your credentials")
device['username'] = input("User name: ")
device['password'] = getpass.getpass()

##taking backup
for line in ipfile:
    try:
       device['ip'] = line.strip("\n")
       print("\n\nConnecting to Device ", line)
       net_connect = netmiko.ConnectHandler(**device)
       net_connect.enable()
       time.sleep(1)
       print("Reading the hostname")
       hostname = (net_connect.send_command('show run | i host').split()[1])
       print(hostname)
       print("Reading the running config ")
       output = net_connect.send_command('show run')
       time.sleep(2)
       filename = r'C:\\Users\\XXXX\\Script Results\\' + hostname + '-' + today + ".txt"
       saveconfig = open(filename, 'w+')
       print("Writing Configuration to file")
       saveconfig.write(output)
       saveconfig.close()
       time.sleep(2)
       net_connect.disconnect()
       print("Configuration saved to file", filename)
   except:
       print("Access to " + device['ip'] + " failed, backup did not taken")
       continue
ipfile.close()

问题分析

  1. 设备类型适配问题:WLC 9800虽支持类IOS命令,但Netmiko的cisco_ios设备类型未针对WLC的输出分页、字符处理做适配,可能导致send_command未完整接收配置内容。
  2. 文件写入不规范:默认w+模式用系统编码写入,WLC配置含特殊字符时可能引发写入截断。
  3. 固定延迟不可靠:time.sleep(2)无法适配不同大小的配置,可能在输出未完全接收时就执行写入。

修复方案

1. 替换设备类型

将device_type改为cisco_wlc,Netmiko针对WLC有专门的输出处理逻辑:

device = {
   'device_type': 'cisco_wlc',  # 替换为WLC专属类型
   'ip': 'IP',
   'username': 'username',
   'password': 'password',
}

2. 延长命令接收超时

通过read_timeout参数确保大配置能完整接收:

output = net_connect.send_command('show run', read_timeout=30)  # 可根据配置大小调整超时值

3. 规范文件写入(指定编码+自动关闭)

用UTF-8编码写入,同时用with语句自动管理文件句柄,避免手动关闭遗漏:

with open(filename, 'w', encoding='utf-8') as saveconfig:
    saveconfig.write(output)

4. 优化主机名获取逻辑

WLC的show run | i host格式可能差异,增加 fallback 逻辑:

hostname_output = net_connect.send_command('show run | include hostname')
hostname = hostname_output.split()[-1] if hostname_output else device['ip']

5. 精准错误捕获

替换宽泛的except,捕获具体异常便于排查:

except Exception as e:
    print(f"访问设备 {device['ip']} 失败,备份未完成,错误: {str(e)}")
    continue

修改后的完整脚本

import netmiko
import getpass
import time
from datetime import datetime

# 补充原脚本缺失的today变量定义
today = datetime.now().strftime('%Y%m%d')

device = {
   'device_type': 'cisco_wlc',
   'ip': 'IP',
   'username': 'username',
   'password': 'password',
}

# 转义路径反斜杠,避免语法错误
ipfile = open("C:\\XXXX\\switch-ips.txt")
print("脚本执行设备备份,请输入你的凭证")
device['username'] = input("用户名: ")
device['password'] = getpass.getpass()

for line in ipfile:
    try:
        device['ip'] = line.strip("\n")
        print(f"\n\n正在连接设备 {device['ip']}")
        net_connect = netmiko.ConnectHandler(**device)
        net_connect.enable()
        
        print("读取主机名")
        hostname_output = net_connect.send_command('show run | include hostname')
        hostname = hostname_output.split()[-1] if hostname_output else device['ip']
        print(f"主机名: {hostname}")
        
        print("读取运行配置")
        output = net_connect.send_command('show run', read_timeout=30)
        
        filename = fr'C:\Users\XXXX\Script Results\{hostname}-{today}.txt'
        print(f"正在写入配置到文件: {filename}")
        with open(filename, 'w', encoding='utf-8') as saveconfig:
            saveconfig.write(output)
        
        net_connect.disconnect()
        print(f"配置已保存到文件: {filename}")
        
    except Exception as e:
        print(f"访问设备 {device['ip']} 失败,备份未完成,错误: {str(e)}")
        continue

ipfile.close()

内容的提问来源于stack exchange,提问作者Choppers16

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.21 01:24:30