Python – How to Connect to a Cisco WLC (Aireos)

This is a very simple article just explaining how you can connect to a Cisco WLC (aireos) using a python script.

You could use this as a first step in developing more advanced Python scripts to monitor or configure your Cisco Wireless Lan Controllers.

I am still learning Python so this might not be the only and easiest way to do it!
Note: starting with version 8.8, you could interact with your Cisco WLC in a easier way using YANG models. See this presentation from Cisco Live: ​https://www.ciscolive.com/c/dam/r/ciscolive/us/docs/2018/pdf/BRKEWN-2050.pdf

PREREQUISITES

We will assume that you have already Python installed on your computer. I would also advise you to use virtual environment when developing Python scripts on your computer.

Here is the tutorial I have followed in order to setup my laptop (macOS) for Python programming: ​https://developer.cisco.com/learning/lab/dev-mac/step/1

Here are the program and libraries we will be using here:

  • Python 3.7
  • netmiko
Here is how I setup a virtual environment for Python 3.7 on my  laptop:
Here is how I installed the netmiko library within this new virtual environment:

CODE - HOW TO

Here is the Python code you will need to in order to connect via SSH to a Cisco WLC. Obviously you need to gather this information first:

  • IP address of the WLC
  • Username allowed to SSH into the WLC
  • Password associated with that user
#!/usr/bin/env python
 
from netmiko import ConnectHandler
 
with ConnectHandler(ip = '192.168.20.2',
                        port = 22,
                        username = 'admin',
                        password = 'Cisco123!',
                        device_type = 'cisco_wlc_ssh') as ch:
    print(ch.send_command("show ap summary"))

In this example, we are using the object called ConnectHandler from the netmiko library to establish an SSH connection to the WLC (192.168.20.2).

​Once connected, we are sending the command “show ap summary” and we are displaying the output of this command.

GO A STEP FURTHER

onnecting to the Cisco WLC could be the first step in a more complex script. Therefore, I have done the following in order to ease the process:

  1. Created a configuration file where I keep my WLC details (IP address, username and passwords)
  2. Created a script that I called ssh_wlc.py that defines function that will establish the connection based on the WLC configuration file

I then re-use my script as a “library” whenever I work on a script and I need to connect to a Cisco WLC.

Note: Python 3.5+ is required here since we are using PEP 484 type hints.

Here is what my configuration file looks like (I used xml so it is easier to parse):
<!--?xml version ="1.0" encoding="UTF-8"?-->
 
    SemFio-WLC
    SemFio Networks
    192.168.20.2
    admin
    Cisco123!
    22
Here is what my ssh_wlc.py script looks like:
#!/usr/bin/env python
#
# This script SSH into a network device defined in a configuration filename
# Configuration file: wlc.configuration
#
# This script can also be used as a library
#
# Written by @VergesFrancois
 
import xmltodict
from netmiko import ConnectHandler
 
 
''' Retreive the network device details from an XML config filename
    - Config file:  'wlc.config'
    - Returns:      A dict containing the device details
'''
def retrv_device_info(config_file: str) -&gt; dict:
    with open(config_file, 'r') as file_object:
        xmlString = file_object.read()
        dom = xmltodict.parse(xmlString)
 
        device = {'name':dom['wlc']['name'],
            'address':dom['wlc']['ipAddress'],
            'username':dom['wlc']['username'],
            'password':dom['wlc']['password'],
            'ssh_port':dom['wlc']['ssh_port'],
            'device_type':'cisco_wlc_ssh'}
 
    return device
 
 
''' SSH into a WLC and send the 'show ap summary' command
    * Paramters
        - Device: Dict containing the device details
    * Returns
        - A str containing the output of the command
'''
def retrv_ap_summary(device: dict):
    print('** Connecting to {} ({})...'.format(device['name'],device['address']))
 
    with ConnectHandler(ip = device['address'],
                        port = device['ssh_port'],
                        username = device['username'],
                        password = device['password'],
                        device_type = device['device_type']) as ch:
        print('** The connection is open')
        command = "show ap summary"
        output =  ch.send_command(command)
 
    return output
 
 
def main():
    device = retrv_device_info('wlc.config')
 
if __name__ == "__main__":
    main()

If you know other alternatives to do the same thing, please feel free to add a comment.

As I continue to learn more about Python, I will continue to share it with you guys on the blog.

Thank you!

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments