0% found this document useful (0 votes)
27 views

Xrdocs Io Programmability Tutorials How To Get A Serial Number Pyats

The document discusses different ways to programmatically collect a device's serial number using pyATS. It covers using the pyATS framework to connect to a device, issuing a CLI command to get inventory information, and parsing the output into a Python dictionary to extract the serial number value.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Xrdocs Io Programmability Tutorials How To Get A Serial Number Pyats

The document discusses different ways to programmatically collect a device's serial number using pyATS. It covers using the pyATS framework to connect to a device, issuing a CLI command to get inventory information, and parsing the output into a Python dictionary to extract the serial number value.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

@XRDOCS PROGRAMMABILITY BLOGS TUTORIALS SEARCH  TAGS

Antoine Orsoni
Programmability enthusiast and Systems Engineer, Cisco Follow

Save to PDF

How to get a serial number - pyATS


 5 minutes read

 TA B L E O F C O NT E NT S

OTHER “HOW TO GET A SERIAL NUMBER” EPISODES

U S I N G T H E D E V N E T S A N D B OX

COLLECTING THE SERIAL NUMBER USING CLI

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 is made of three main building blocks:

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/

The code for this series of posts will be published here.

Other “How to get a serial number” episodes

You’ve missed an episode? You would like to read more? Below the list of published episodes:

Episode URL What’s covered

1 - pyATS Link Using pyATS to get a serial number on a given IOS XR device

Using the Devnet sandbox

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

IOS XRv 9000 host sandbox-iosxr-1.cisco.com

SSH Port 22

NETCONF port 830

Username admin

Password C1sco12345

Collecting the serial number using CLI

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 .

1 RP/0/RP0/CPU0:R1#show inventory all


2 Wed Nov 17 15:19:44.062 UTC
3 NAME: "0/0", DESCR: "Cisco IOS-XRv 9000 Centralized Line Card"
4 PID: R-IOSXRV9000-LC-C , VID: V01, SN: 6475A28D725
5
6 NAME: "0/RP0", DESCR: "Cisco IOS-XRv 9000 Centralized Route Processor"
7 PID: R-IOSXRV9000-RP-C , VID: V01, SN: 986AF9109D3

8
9 NAME: "Rack 0", DESCR: "Cisco IOS-XRv 9000 Centralized Virtual Router"
10 PID: R-IOSXRV9000-CC , VID: V01, SN: 8F21767F3A3

show_inventory.log hosted with ❤ by GitHub view raw

Getting your hands dirty – Collecting the serial number using pyATS
Enough talking, let’s code!

How to enable pyATS on your IOS XR device?

pyATS leverages the Unicon library to connect to the device. It supports various protocols to connect to your
device, such as telnet or ssh.

SSH is the recommended administration protocol for modern operations.

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.

You can nd the complete documentation on how to build a testbed here.

In a nutshell, we need to specify how to connect to our device:

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.

Our testbed look like the below example:

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

testbed.yaml hosted with ❤ by GitHub view raw

Testbed de nition has been covered in more details in this post.

Leveraging pyATS parsers to get a Python dictionary

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": {

4 "descr": "Cisco IOS-XRv 9000 Centralized Line Card",


5 "pid": "R-IOSXRV9000-LC-C",

6 "vid": "V01",

7 "sn": "6475A28D725",

8 },

9 "0/0/0": {"descr": "N/A", "pid": "PORT-1G-NIC", "vid": "N/A", "sn": "N/A"},

10 "0/0/1": {"descr": "N/A", "pid": "PORT-1G-NIC", "vid": "N/A", "sn": "N/A"},


11 "0/0/2": {"descr": "N/A", "pid": "PORT-1G-NIC", "vid": "N/A", "sn": "N/A"},

12 "0/0/3": {"descr": "N/A", "pid": "PORT-1G-NIC", "vid": "N/A", "sn": "N/A"},

13 "0/0/4": {"descr": "N/A", "pid": "PORT-1G-NIC", "vid": "N/A", "sn": "N/A"},


14 "0/0/5": {"descr": "N/A", "pid": "PORT-1G-NIC", "vid": "N/A", "sn": "N/A"},

15 "0/0/6": {"descr": "N/A", "pid": "PORT-1G-NIC", "vid": "N/A", "sn": "N/A"},


16 "0/RP0": {
17 "descr": "Cisco IOS-XRv 9000 Centralized Route Processor",
18 "pid": "R-IOSXRV9000-RP-C",

19 "vid": "V01",

20 "sn": "986AF9109D3",
21 },
22 "Rack 0": {

23 "descr": "Cisco IOS-XRv 9000 Centralized Virtual Router",

24 "pid": "R-IOSXRV9000-CC",
25 "vid": "V01",
26 "sn": "8F21767F3A3",

27 },
28 }
29 }

show_inventory_pyats.log hosted with ❤ by GitHub view raw

To better understand the di erence between a raw output and a parserd output, you can refer to this article.

Using Python to get the value of a speci c key

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.

Dictionary keys are case sensitive!

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.

Bringing it all together

This is what the full script looks like.

1. We load the testbed to extact device information,

2. We connect to the device,


3. We collect the CLI output and we parse it using pyATS librairies,
4. We extract the device number from the nested dictionaries,
5. We disconnect from the device.

You can nd all supported pyATS parsers in the documentation.

1 from genie.testbed import load


2
3 testbed = load('./testbed.yaml')

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

13 show_inventory = iosxr.parse('show inventory')

14
15 print(f'{show_inventory["module_name"]["Rack 0"]["sn"] = }')

16
17 # Disconnect from the device
18 iosxr.disconnect()

get_serial_pyats.py hosted with ❤ by GitHub view raw

pyATS pros and cons to retrieve a serial number

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

Below a few useful pyATS resources.

List of supported pyATS parsers,


The o cial pyATS documentation,
List of Unicon supported platforms,
Devnet code exchange,

Join the Webex space with the pyATS community.


 Tags: Automation Programmability pyATS Python

 Updated: July 29, 2022

SHARE ON

   

Leave a Comment

What do you think?


1 Response

Upvote Funny Love Surprised

0 Comments 
1 Login

G Start the discussion…

LOG IN WITH OR SIGN UP WITH DISQUS ?

Name

 Share Best Newest Oldest

Be the rst to comment.

Subscribe Privacy Do Not Sell My Data

FOLLOW:  TWITTER  GITHUB  FEED

This site is maintained by Cisco Systems, Inc. employees. Powered by Jekyll & Minimal Mistakes.

You might also like