Skip to content

dot11.Network

Wireless & Network Stuff

Menu
  • Home
  • Contact
  • About
Menu
Python

Automate backup on multiple AireOS WLCs with Python

Posted on March 29, 2022March 29, 2022 by Kevin

As any Network/Wireless Engineer, we tend to automate more and more actions in order to save time (and money?), so I decided to dig deeper and deeper into Python, but …

What to do with Python?

And that was my biggest concern.. . Not that I didn’t want to learn Python from basics and do that funny boring stuff :

a = "Hello "
b = "World!"
print(a+b)

Instead, I really wanted to work on an exciting and useful project to keep me excited with Python.

For a year now, I have a second occupation as Adjunct Lecturer in French University and work with student on Cisco Lab to perform Implementation & Troubleshoot of Wireless config. As each student has its own Pod with a WLC & AP, I wanted to have a single script to perform config backup at the end of each working session.

What this code is actually doing?

  • Script should be ran with at least one argument :
    • If only one argument is set, we only perform the backup on the specific Pod
    • If two argument are set, we perform backup on the range of Pods
    • If “all” argument is set, we perform backup on all Pods
  • Backup is basically taken via the execution of command “show run-config commands”
    • I’m taking advantage of netmiko library for SSH connection

What could be optimized?

Not being super experience with Python, this script is working like a charm but the next step would be :

  • Handle multi-threading to open simulatenous SSH session to multiple devices
    • SSH connection to AireOS WLC is always taking a long time …
  • Improve management of argument with Try / Except blocks
  • Create added value with this script to check some configuration key-points and return a “compliance” level

Script in its earlier version

from netmiko import ConnectHandler
import sys
from datetime import datetime

def backupConfig (min_pod,max_pod):
    
    if min_pod == "all":
        min_pod=1
        max_pod=10
        
    if max_pod == 0:
        max_pod = min_pod
      
    for x in range(int(min_pod), int(max_pod)+1):
        print("Connecting to Pod"+str(x)+"...")
        with ConnectHandler(ip = "10.20."+str(x)+".10",
                        port = 22,
                        username = 'admin',
                        password = 'C1sco123',
                        device_type = 'cisco_wlc_ssh') as ch:
            print("Backup starting ...")
            ch.send_command("config paging disable")
            output = ch.send_command("show run-config commands")
            
            today = datetime.now()
            d1 = today.strftime("%d-%m-%Y_%H-%M")
            f = open("Backup-Session1-Pod"+str(x)+"-"+d1+".txt", "x")
            f.write(output)
            f.close
            print("Backup file created for Pod"+str(x))

n = len(sys.argv)
if n-1 <= 0:
    print("Missing argument")

else:  
    if str(sys.argv[1]) == "all":
        print("## Starting backup on all Pod")
        backupConfig("all",0)
        
    elif n-1 == 1:
        print("## Starting backup on Pod"+sys.argv[1])
        backupConfig(sys.argv[1],0)
        
    elif sys.argv[1] > sys.argv[2]:
        print("Argument are wrong, please review")
        
    else:
        print("## Starting backup from Pod"+sys.argv[1]+" to Pod"+sys.argv[2])
        backupConfig(sys.argv[1],sys.argv[2])

Thanks Francois for your great article that helped me a lot : https://semfionetworks.com/blog/python-how-to-connect-to-a-cisco-wlc-aireos/

FacebookTweetLinkedIn

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Categories

  • CCIE Notes (3)
  • Cisco (5)
  • Config (6)
  • Python (1)
  • SDA (3)
  • Security (1)
  • Uncategorized (2)

Archives

  • June 2022 (2)
  • March 2022 (2)
  • October 2019 (1)
  • January 2019 (2)
  • October 2018 (1)
© 2025 dot11.Network | Powered by Minimalist Blog WordPress Theme