Python automation for Cisco AireOS WLCs

TL:DR

  1. Make sure Python and Netmiko are installed
  2. Create a ‘.py’ file
  3. Gotta have from netmiko import ConnectHandler before everything else
  4. Build your device dictionary (device = {'device_type': 'cisco_wlc', ...})
  5. Establish a connection to the device (net_connect = ConnectHandler(**device))
  6. Send command(s) to the device
  7. Disconnect from the device (net_connect.disconnect())

Basics

OK since I know not everyone is going to know how to get everything set up to successfully run scripts, here’s the quick rundown:

  1. Install Python on your device.
    • You can verify the installation by opening a new Terminal/Command Prompt and type python3 --version
  2. Install Netmiko via pip by entering the command in your Terminal/Command Prompt:
    • pip install netmiko
  3. Install some kind of IDE to make your life WAY easier…unless you like making your own life hard, I don’t judge. Personally, I use Visual Studio Code
  4. Create some sort of new file, named whatever makes sense to you, ending in the ‘.py’ extension, and fire it up in your IDE (or type it all into your terminal like a psychopath)

Building the script

First thing we need to do is tell Python to load the factory function ‘ConnectHandler’ that is part of the Netmiko library by putting from netmiko import ConnectHandler on the first line. Python runs from the top of the file down, so if you don’t load this right from the get-go, the script may not work correctly.

from netmiko import ConnectHandler

Next, we’re going to create a dictionary named ‘device’ that will store all the info Netmiko needs to connect to your WLC:

from netmiko import ConnectHandler

device = {
    'device_type': 'cisco_wlc',
    'ip': '192.168.11.12',
    'username': 'admin',
    'password': 'totallynotadmin'
}

Now Netmiko knows how to connect to your device, so now let’s add a Python command to tell Netmiko to establish an SSH connection to the WLC by adding the line net_connect = ConnectHandler(**device). Let’s break this down so we know what’s going on:

Now, we’re going to use ‘net_connect’ (which we assigned to be equal to ConnectHandler(**device)) to send a command to the WLC with net_connect.send_command(command_string='{command}'). This will instruct Netmiko to send whatever command you put in place of {command}, but if we want to actually see the output, let’s assign the output of whatever command was sent to a variable:

    output = net_connect.send_command(command_string='{command}')

Now, the output of your command will be saved to the variable ‘output‘ for us to do whatever we want with later in the script. In this example, I’m going to send the ‘show wlan summary‘ command to the WLC, then print the output to my terminal:

    output = net_connect.send_command(command_string='show wlan summary')
    print(output)

from netmiko import ConnectHandler

device = {
    'device_type': 'cisco_wlc',
    'ip': '192.168.11.12',
    'username': 'admin',
    'password': 'totallynotadmin'
}
output = net_connect.send_command(command_string='show wlan summary')
print(output)

Nice! Now all that’s left to do it tell Netmiko to disconnect from the device:

    net_connect.disconnect()

Let’s put this all together, save the ‘.py’ file, and run it!

from netmiko import ConnectHandler

device = {
    'device_type': 'cisco_wlc',
    'ip': '192.168.11.12',
    'username': 'admin',
    'password': 'totallynotadmin'
}

output = net_connect.send_command(command_string='show wlan summary')
print(output)
net_connect.disconnect()

If you’re using VSCode, you can click the little “Play” button in the top right to run the script, or you can navigate to the folder you saved the ‘.py’ file in and enter ‘python3 {filename.py}’:

taylor.thurston@UA-MAC-C02DQ8V5MD6T Python Automation for AireOS % python3 aireos_basics.py

Number of WLANs.................................. 5

WLAN ID  WLAN Profile Name / SSID                                       Status    Interface Name        PMIPv6 Mobility
-------  -------------------------------------------------------------  --------  --------------------  ---------------
1        testwlan / testwlan                                            Disabled  null_vlan_66          none        

Congrats! You’ve just taken your first step to Python automation of Cisco AireOS WLCs! In the next part, we’ll work on the next steps and how we can do things like hide passwords. See you there!

Here’s the same script with some comments annotating what’s going on:

from netmiko import ConnectHandler  # Import the ConnectHandler factory function that will handle the SSH connection to the device

# Anything between three single quotes is a multi-line comment. It's useful for writing longer comments.
'''
Set your device connection details. This is a dictionary that contains the device's IP address, username, and password.
Each key in the dictionary is on a new line and indented with 4 spaces (1 tab) for readability. You can put them all
on one line if you want, but it's harder to read. Make sure each entry in the dictionary has a comma at the end to separate
each key-value pair. The keys are strings, so they're in single quotes. The values are also strings, so they're in single
quotes too. The dictionary is assigned to the variable 'device'. The dictionary is a data structure that stores key-value
pairs. In this case, the keys are 'device_type', 'ip', 'username', and 'password', and the values are 'cisco_wlc', 
'192.168.11.12', 'admin', and 'totallynotadmin'.
'''
device = {
    'device_type': 'cisco_wlc',  # Set device type as 'cisco_wlc', which for Netmiko is AireOS-based WLCs (not IOS-XE)
    'ip': '192.168.11.12',  # Your device's IP address
    'username': 'admin',  # Your device's SSH username
    'password': 'totallynotadmin'  # Your device's SSH password
}
# Establish an SSH connection to the device using the device dictionary:
net_connect = ConnectHandler(**device)

# Send some commands to the device and print the output:
output = net_connect.send_command(command_string='show wlan summary')  
print(output)

# Disconnect from the device:
net_connect.disconnect()

Subscribe

Enter your email below to receive updates.

One response to “Python automation for Cisco AireOS WLCs”

  1. […] Part 1, we created a Python script that gathered the current WLANs configured on a device, but the […]

    Like

Leave a comment