[Python] Pulling data from Cisco Wireless Controller

Discussão em 'Python' iniciado por Stack, Fevereiro 21, 2025.

  1. Stack

    Stack Membro Participativo

    since there is no api support for cisco wireless lan controllers, I am trying to use python to connect via cli and pull the data.

    unfortunately cli output from cisco WLC is not well structured. I cannot use ansible at this point as I only have win10 LAB equipment.

    this is the data that I am pulling from controller.

    [​IMG]

    I am using netmiko library to pull the data and put into dictionary so I can manipulate and access however it is not working as intended.

    from netmiko import ConnectHandler
    from netmiko import ConnectHandler, exceptions
    import re

    device= {
    'device_type': 'cisco_wlc_ssh',
    'ip': 'redacted',
    'username': 'redacted',
    'password': 'redacted',
    'port' : 22,
    "session_log":r'D:\Python Projects\Netmiko\WifiCLI\Netmiko_session_GNR_log',
    "session_log_file_mode": "append"
    }

    try:
    connect = ConnectHandler(**device)
    show_netuser_sum = connect.send_command("show netuser summary", expect_string=f'y/n')
    show_netuser_sum += connect.send_command_timing("y")
    show_netuser_sum = show_netuser_sum.strip().split("\n")
    except (exceptions.ReadTimeout) as e:
    connect = ConnectHandler(**device)
    show_netuser_sum = connect.send_command("show netuser summary")
    show_netuser_sum = show_netuser_sum.strip().split("\n")

    header_line = show_netuser_sum[3]
    data_lines = show_netuser_sum[5:]

    headers = re.split(r'\s{2,}', header_line.strip())

    # Extract data
    data = []
    for line in data_lines:
    # Split the line based on multiple spaces
    split_line = re.split(r'\s{2,}', line.strip())

    if len(split_line) > len(headers):
    split_line[1] = ' '.join(split_line[1:3])
    split_line.pop(2)
    if len(split_line) > len(headers):
    split_line[3] = ' '.join(split_line[3:])
    split_line = split_line[:len(headers)]

    if len(split_line) < len(headers):
    split_line.insert(3, '')

    row_data = dict(zip(headers, split_line))
    data.append(row_data)

    # Print the structured data
    for row in data:
    print(row)


    unfortunately the output is still messy [​IMG]

    as you see, lifetime and description is mixing up. I have 13 rows of users and some of them is coming up like that (like user bcook and its lifetime/description)

    Continue reading...

Compartilhe esta Página