
This script will utilize the Netmiko library for SSH connections and the NAPALM library for device configuration management.
Script Requirements
- Python 3.x
- Netmiko library (install using
pip install netmiko) - NAPALM library (install using
pip install napalm) - Cisco IOS device(s) with SSH enabled
- A configuration file (e.g.,
config.yaml) defining the recommended IOS versions for each device model - 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:
- Device Connection: Establish an SSH connection to the Cisco device using Netmiko.
- IOS Version Check: Retrieve the current IOS version from the device using NAPALM.
- Configuration Check: Compare the current IOS version with the recommended version defined in the configuration file.
- Upgrade/Downgrade: Perform the necessary upgrade or downgrade using NAPALM, if the current version does not match the recommended version.
- 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
- Replace
your_usernameandyour_passwordwith your actual SSH credentials. - Update the
device_modelsdictionary to match your specific device models and corresponding IOS image files. - Modify the
recommended_versionsdictionary to reflect the desired IOS versions for each device model. - Ensure the
images/directory contains the required IOS image files. - 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 π
Leave a comment