Python script to check the Cisco IOS version and perform upgrades or downgrades as needed

This script will utilize the Netmiko library for SSH connections and the NAPALM library for device configuration management.

Script Requirements

  1. Python 3.x
  2. Netmiko library (install using pip install netmiko)
  3. NAPALM library (install using pip install napalm)
  4. Cisco IOS device(s) with SSH enabled
  5. A configuration file (e.g., config.yaml) defining the recommended IOS versions for each device model
  6. A directory containing the IOS image files (e.g., images/) matching the device models and recommended versions

Script Structure

The script will consist of the following components:

  1. Device Connection: Establish an SSH connection to the Cisco device using Netmiko.
  2. IOS Version Check: Retrieve the current IOS version from the device using NAPALM.
  3. Configuration Check: Compare the current IOS version with the recommended version defined in the configuration file.
  4. Upgrade/Downgrade: Perform the necessary upgrade or downgrade using NAPALM, if the current version does not match the recommended version.
  5. Image Transfer: Transfer the required IOS image file from the images/ directory to the device using SCP (or other transfer method).

Script Code

Here’s a sample script to get you started:

import os
import yaml
from netmiko import SSHNetmiko
from napalm.base import get_network_driver

# Configuration file path
config_file = 'config.yaml'

# Device model to IOS image mapping
device_models = {
    'WS-C3560X-48P': 'c3560e-universalk9-mz.122-55.SE8.bin'
}

# Recommended IOS versions for each device model
recommended_versions = {
    'WS-C3560X-48P': '15.2(4)E8'
}

def get_current_ios_version(device):
    # Establish SSH connection using Netmiko
    ssh = SSHNetmiko(device['ip'], username='your_username', password='your_password')
    # Retrieve current IOS version using NAPALM
    ios_version = ssh.get_facts()['ios_version']
    return ios_version

def check_ios_version(current_version, recommended_version):
    if current_version != recommended_version:
        return True  # Version mismatch, upgrade/downgrade required
    return False  # Version matches, no action needed

def upgrade_or_downgrade(device, current_version, recommended_version):
    # Determine upgrade/downgrade direction
    if current_version < recommended_version:
        direction = 'upgrade'
    elif current_version > recommended_version:
        direction = 'downgrade'
    else:
        return  # No action needed

    # Transfer required IOS image file using SCP
    image_file = os.path.join('images/', device_models[device['model']])
    ssh.scp.put(image_file, '/tmp/')

    # Perform upgrade/downgrade using NAPALM
    if direction == 'upgrade':
        ssh.load_replace_candidate(filename='/tmp/' + image_file)
        ssh.commit_config()
    elif direction == 'downgrade':
        ssh.load_replace_candidate(filename='/tmp/' + image_file, replace='exact')
        ssh.commit_config()

    # Reload the device to apply changes
    ssh.send_command('reload')

def main():
    with open(config_file, 'r') as f:
        config_data = yaml.safe_load(f)

    for device in config_data['devices']:
        current_version = get_current_ios_version(device)
        recommended_version = recommended_versions[device['model']]
        if check_ios_version(current_version, recommended_version):
            upgrade_or_downgrade(device, current_version, recommended_version)

if __name__ == '__main__':
    main()

Note

  1. Replace your_username and your_password with your actual SSH credentials.
  2. Update the device_models dictionary to match your specific device models and corresponding IOS image files.
  3. Modify the recommended_versions dictionary to reflect the desired IOS versions for each device model.
  4. Ensure the images/ directory contains the required IOS image files.
  5. This script is a starting point and may require additional error handling, logging, and testing to ensure its reliability.

Remember to test the script in a lab environment before deploying it to production πŸ™‚

renjithbs Avatar

Posted by

Leave a comment