10

Parsing Cisco AireOS Configuration with Python

 3 years ago
source link: https://rowelldionicio.com/python-parsing-cisco-aireos-configuration/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Python – Parsing Cisco AireOS Configuration

February 21, 2020 By Rowell Leave a Comment

I’m beginning to get the hang of Python and it’s capabilities with network operations/monitoring.

My good friend and co-host on the CTS Podcast, François Vergès, has been creating his own scripts to help automate some of the operational tasks he does with Cisco AireOS WLAN controllers.

There was a scenario where I wanted to gather the operating channel and transmit power of a specific number of access points joined to a Cisco 8540 controller.

We can simply look at this via the management GUI or create some sort of Cisco Prime report but I just wanted a simple CLI output.

Cisco AireOS has a CLI command to get this output for the 2.4 GHz and 5 GHz radio:

show advanced 802.11a|b summary

The result of the command is every single access point on the controller but I just wanted a subset of those access points. That’s where I need to match a specific pattern based on the access point name using REGEX.

With this script, I am using Python 3.7 with the Regular expression operations library and netmiko library.

François was able to point me in the right direction with using the ConnectHandler to create a my connection and send the command to the WLAN controller:

import re
from netmiko import ConnectHandler

with ConnectHandler(
    ip = 'big-wlan-controller',
    port = 22,
    username = 'admin',
    password = 'test123',
    device_type = 'cisco_wlc_ssh'
) as ch:

    output = ch.send_command("show advanced 802.11a summary")

The full output of the command is placed into a variable called output.

Now, I wanted to use REGEX to match on specific access points. I create a pattern and use a for loop on each line in the variable of output.

pattern = re.compile("b2", re.IGNORECASE)
for ap in output.splitlines():
    if pattern.search(ap) != None:
        print(ap)

In the pattern variable I am using REGEX to simply search for anything containing “b2” which stands for Building 2 in my scenario.

In the for loop, I am taking the output variable and splitting the string by line and applying the pattern search per line. Then I print the output to the screen.

The full script:

import re
from netmiko import ConnectHandler

with ConnectHandler(
    ip = 'big-wlan-controller',
    port = 22,
    username = 'admin',
    password = 'test123',
    device_type = 'cisco_wlc_ssh'
) as ch:

    output = ch.send_command("show advanced 802.11a summary")

pattern = re.compile("b2", re.IGNORECASE)
for ap in output.splitlines():
    if pattern.search(ap) != None:
        print(ap)

The output (sanitized):

$ python regex-test.py
b2-ap-1                     xx:xx:xx:xx:xx:xx  1   ENABLED  UP          (108,112)           3/6 (11 dBm)
b2-ap-2                     xx:xx:xx:xx:xx:xx  1   ENABLED  UP          (100,104,108,112)  *1/7 (19 dBm)
b2-ap-3                     xx:xx:xx:xx:xx:xx  1   ENABLED  UP          52*                *1/6 (17 dBm)
b2-ap-4                     xx:xx:xx:xx:xx:xx  1   ENABLED  UP          36*                *2/5 (12 dBm)
b2-ap-5                     xx:xx:xx:xx:xx:xx  1   ENABLED  UP          136*               *1/6 (17 dBm)

From the output I can see the AP name, MAC address of the AP, up/down status, operating channel, and transmit power. There’s a bit more work to put into the script but it gave me exactly what I needed at the time.

Filed Under: Coding Tagged With: python


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK