Introduction
Cisco’s PyATS is a powerful network test automation framework that can greatly simplify network management tasks, such as extracting device information. In this blog post, we’ll walk you through a Python script using PyATS to extract Cisco IOS version and device information from a Cisco Catalyst 3750 switch.
Script Overview
Our Python script will connect to the Catalyst 3750 switch, execute the show version command, and parse the output to extract the relevant information, such as the IOS version, device model, and system uptime. We’ll be using the Genie library, which is included with PyATS, to parse the command output.
Script Code
To get started, install PyATS and Genie:
pip install pyats[library]
Now, create a Python script called extract_ios_info.py with the following content:
from pyats.topology import loader
from genie.libs import ops
from genie.conf import Genie
# Define device information
device_info = {
'device_type': 'ios',
'ip': 'your_switch_ip',
'username': 'your_username',
'password': 'your_password',
}
# Create a testbed with the device information
testbed = loader.load(
{'devices': {'switch': {'connections': {'cli': {'protocol': 'ssh', 'ip': device_info['ip']}}, 'credentials': {'default': {'username': device_info['username'], 'password': device_info['password']}}, 'type': 'Device'}}}
)
# Connect to the device
device = testbed.devices['switch']
device.connect(via='cli')
# Execute 'show version' command and parse the output
version_output = ops.version.version.Version(device)
version_output.learn()
# Extract relevant information
ios_version = version_output.version['os']
device_model = version_output.version['platform']
system_uptime = version_output.version['uptime']
# Print the extracted information
print(f"IOS Version: {ios_version}")
print(f"Device Model: {device_model}")
print(f"System Uptime: {system_uptime}")
Replace your_switch_ip, your_username, and your_password with the appropriate values for your switch.
Run the script using the following command:
python extract_ios_info.py
The script should output the extracted information, similar to the following:
IOS Version: 12.2(55)SE9
Device Model: WS-C3750G-24TS-1U
System Uptime: 33 weeks, 4 days, 4 hours, 49 minutes
Conclusion
In this blog post, we demonstrated how to use PyATS to extract Cisco IOS version and device information from a Cisco Catalyst 3750 switch. This script can be easily adapted to extract other information from the show version command output or other Cisco IOS commands. PyATS is a powerful and flexible tool that can greatly simplify network management tasks and help you stay on top of your network’s health and performance. Stay tuned to Network ThinkTank for more tips, tricks, and insights into the world of networking and automation.