Xrdocs Io Programmability Tutorials How To Get A Serial Number Pyats
Xrdocs Io Programmability Tutorials How To Get A Serial Number Pyats
Antoine Orsoni
Programmability enthusiast and Systems Engineer, Cisco Follow
Save to PDF
TA B L E O F C O NT E NT S
U S I N G T H E D E V N E T S A N D B OX
G E T T I N G YO U R H A N D S D I R T Y – C O L L E C T I N G T H E S E R I A L N U M B E R U S I N G P YAT S
P YAT S P R O S A N D C O N S T O R E T R I E V E A S E R I A L N U M B E R
CONCLUSION
RESOURCES
Recently, I got a query from a Customer: how could I easily collect my device(s) serial number?
At rst, the question sounded silly: you could just do show inventory all on any IOS XR platform to get the
platform serial number. What if you need to retrieve an information 100 times per day? What if you need get this
information on 100 devices at once? The goal of this new series of article is to explain di erent ways to collect a
serial number on a device. If you can do it with a serial number, you can do it with anything else!
In thirs rst episode, we will use pyATS (Python Automated Test Systems, to be pronounced “py A. T. S.”) was
rst created as an internal project, to ease the validation of two OS versions. It has been made public in 2017
through Cisco Devnet.
pyATS, the core block of this ecosystem. It’s a Python framework which leverages multiple Python libraries
such as Unicon, providing a simpli ed connection experience to network devices. It supports CLI, NETCONF,
RESTCONF and gRPC. It enables network engineers and developers to start with small and simple test cases.
pyATS libraries (also known as Genie) which provides everything you need for network testing such as parsers,
triggers and APIs.
XPRESSO, the pyATS Web UI Dashboard.
If you are not already familiar with pyATS and you want to know how to install it and how to use it, have a look at my pyATS series below.
https://xrdocs.io/programmability/tutorials/pyats-series-install-and-use-pyats/
You’ve missed an episode? You would like to read more? Below the list of published episodes:
1 - pyATS Link Using pyATS to get a serial number on a given IOS XR device
In order for everyone to be able to run the code, we will use the IOS XR always-on sandbox on Cisco Devnet.
Below the sandbox information.
Key Value
SSH Port 22
Username admin
Password C1sco12345
To make sure we are all on the same page, below is the command to collect the serial number with CLI on an
IOS XR device and a sample output. In this case, the answer we want to get is SN: 8F21767F3A3 .
8
9 NAME: "Rack 0", DESCR: "Cisco IOS-XRv 9000 Centralized Virtual Router"
10 PID: R-IOSXRV9000-CC , VID: V01, SN: 8F21767F3A3
Getting your hands dirty – Collecting the serial number using pyATS
Enough talking, let’s code!
pyATS leverages the Unicon library to connect to the device. It supports various protocols to connect to your
device, such as telnet or ssh.
In other words, you just need to make sure you have an account with read rights, which can connects using ssh.
You can enable SSH on IOS XR with the command ssh server v2 .
Testbed de nition
The simplest way to connect to a device is through a pyATS testbed le, written in YAML. This information will be
used by Unicon to connect to the device and send the requested commands.
IP address or URL ,
Credentials ,
Type , the Network Operating System of our device, in our case IOS XR,
Protocol , how to connect to our device, in our case SSH on port 22.
1 testbed:
2 name: XR_Testbed
3 credentials:
4 default:
5 username: admin
6 password: C1sco12345
7
8 devices:
9 R1:
10 type: iosxr
11 os: iosxr
12 connections:
13 vty:
14 protocol: ssh
15 ip: sandbox-iosxr-1.cisco.com
16 port: 22
The power of the pyATS libraries: to be able to convert a raw output (what you would get in a CLI output,
printed earlier in this post) into a parsed output (dictionary) where you can easily get a value by accessing a
speci c key . Once parsed by pyATS, the output would look to something like below:
1 {
2 "module_name": {
3 "0/0": {
6 "vid": "V01",
7 "sn": "6475A28D725",
8 },
19 "vid": "V01",
20 "sn": "986AF9109D3",
21 },
22 "Rack 0": {
24 "pid": "R-IOSXRV9000-CC",
25 "vid": "V01",
26 "sn": "8F21767F3A3",
27 },
28 }
29 }
To better understand the di erence between a raw output and a parserd output, you can refer to this article.
In Python, you can see a Dicitonary as a set of key: value pairs. For example: { "name": "IOS-XR1", "version":
"7.4.2"} .
In my_dict , in order to retrieve my_value associated with a speci c my_key , you should use my_value =
my_dict['my_key'] .
A value can be a dictionary. In this case, we call it a nested dictionary . In our example, the key "module_name" is
associated with a dictionary. In our pyATS output, we have multiple nested dictionaries.
In our case, the code to get the Serial Number out of the parsed output should look something like: serial_number
= my_output["module_name"]["Rack 0"]["sn"] .
You can read more about Python dictionaries in the o cial documentation.
4 iosxr = testbed.devices["R1"]
5
6 # Connect to the device
7 iosxr.connect(init_exec_commands=[],
8 init_config_commands=[],
9 log_stdout=False,
10 learn_hostname=True)
11
12 # Collecting the structured output
14
15 print(f'{show_inventory["module_name"]["Rack 0"]["sn"] = }')
16
17 # Disconnect from the device
18 iosxr.disconnect()
This last section re ects my own experience. Based on your own use of pyATS, it might vary. Feel free to comment if you disagree or if you think
about something else.
Pros
pyATS uses SSH as transport, which is most of the time open on the device.
You can extract the serial number with very basic Python knowledge and in less than 10 lines of code.
pyATS has great documentation and a very active community.
Cons
pyATS is great to retrieve this information once. You might have better tools if you need to retrieve an
information periodically (ex: interface CRC errors, once per day).
If you use another Network Operating System, the parser might not exist yet. It might take many more lines of
code to extact what you need using text parsing tools like Text FSM.
What if the CLI changes and the parser is not valid anymore?
Conclusion
pyATS is a great tool to retrieve a serial number: we were able to achieve our goal in less than 10 lines of Python.
In the next episode, we will see how to get a serial number using NETCONF.
Resources
SHARE ON
Leave a Comment
0 Comments
1 Login
Name
This site is maintained by Cisco Systems, Inc. employees. Powered by Jekyll & Minimal Mistakes.