Skip to content

Commit 2fa3d08

Browse files
committed
Add a RESTCONF script to get device serial numbers.
1 parent 167c6a8 commit 2fa3d08

File tree

2 files changed

+100
-0
lines changed

2 files changed

+100
-0
lines changed

RC-get-serial-numbers/RC-get-sns.py

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
#!/usr/bin/env python
2+
#
3+
# Copyright (c) 2019 Joe Clarke <jclarke@cisco.com>
4+
# All rights reserved.
5+
#
6+
# Redistribution and use in source and binary forms, with or without
7+
# modification, are permitted provided that the following conditions
8+
# are met:
9+
# 1. Redistributions of source code must retain the above copyright
10+
# notice, this list of conditions and the following disclaimer.
11+
# 2. Redistributions in binary form must reproduce the above copyright
12+
# notice, this list of conditions and the following disclaimer in the
13+
# documentation and/or other materials provided with the distribution.
14+
#
15+
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16+
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17+
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18+
# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19+
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20+
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21+
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22+
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23+
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24+
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25+
# SUCH DAMAGE.
26+
#
27+
# This script retrieves inventory from devices with RESTCONF and prints all serial
28+
# numbers per device.
29+
#
30+
31+
import requests
32+
from argparse import ArgumentParser
33+
34+
35+
def main():
36+
37+
parser = ArgumentParser(description='Select options.')
38+
39+
# Input parameters
40+
parser.add_argument('-hosts', '--hosts', type=str, required=True,
41+
help="Comma-separated list of devices")
42+
parser.add_argument('-user', '--username', type=str, default='cisco',
43+
help="User credentials for the request")
44+
parser.add_argument('-passwd', '--password', type=str, default='cisco',
45+
help="It's the password")
46+
47+
args = parser.parse_args()
48+
url = 'https://{}/restconf/data/Cisco-IOS-XE-device-hardware-oper:device-hardware-data/device-hardware'
49+
inv_cache = {}
50+
51+
hosts = args.hosts.split(',')
52+
53+
for host in hosts:
54+
55+
u = url.format(host)
56+
57+
headers = {
58+
'Accept': "application/yang-data+json",
59+
}
60+
61+
response = None
62+
63+
try:
64+
response = requests.request('GET', u, auth=(
65+
args.username, args.password), headers=headers, verify=False)
66+
response.raise_for_status()
67+
except Exception as e:
68+
print('Failed to get inventory from device: {}'.format(e))
69+
continue
70+
71+
inv = response.json()
72+
73+
for asset in inv['Cisco-IOS-XE-device-hardware-oper:device-hardware']['device-inventory']:
74+
if host not in inv_cache:
75+
inv_cache[host] = []
76+
77+
if asset['serial-number'] == '':
78+
continue
79+
80+
inv_cache[host].append(
81+
{'sn': asset['serial-number'], 'pn': asset['part-number']})
82+
83+
for host, comps in inv_cache.items():
84+
print('Host {} serial numbers:'.format(host))
85+
for comp in comps:
86+
print('\t{}'.format(comp['sn']))
87+
88+
89+
if __name__ == '__main__':
90+
main()

RC-get-serial-numbers/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# RESTCONF get-serial-numbers
2+
3+
This is an example Python script that retrieves the serial numbers of IOS-XE devices and prints
4+
each device with its collection of serial numbers.
5+
6+
# requirements
7+
-- IOS-XE running >/= 16.3.1 also enabled for RESTCONF
8+
9+
# running
10+
-- Can run on-box or off-box.

0 commit comments

Comments
 (0)