This post will describe the exercises and solutions for week four of Kirk Byers Python for Network Engineers.

Our next task is to parse data from show version from a device.

[code lang=text]
II. Parse the below 'show version' data and obtain the following items (vendor, model, os_version, uptime, and serial_number). Try to make your string parsing generic i.e. it would work for other Cisco IOS devices.

The following are reasonable strings to look for:
'Cisco IOS Software' for vendor and os_version
'bytes of memory' for model
'Processor board ID' for serial_number
' uptime is ' for uptime

Store these variables (vendor, model, os_version, uptime, and serial_number) in a dictionary. Print the dictionary to standard output when done.
Note, "Cisco IOS Software…Version 15.0(1)M4…(fc1)" is one line.

>>>>> show version data <<<<<
Cisco IOS Software, C880 Software (C880DATA-UNIVERSALK9-M), Version 15.0(1)M4, RELEASE SOFTWARE (fc1)
Technical Support:
Copyright (c) 1986-2010 by Cisco Systems, Inc.
Compiled Fri 29-Oct-10 00:02 by prod_rel_team
ROM: System Bootstrap, Version 12.4(22r)YB5, RELEASE SOFTWARE (fc1)

twb-sf-881 uptime is 7 weeks, 5 days, 19 hours, 23 minutes
System returned to ROM by reload at 15:33:36 PST Fri Feb 28 2014
System restarted at 15:34:09 PST Fri Feb 28 2014
System image file is "flash:c880data-universalk9-mz.150-1.M4.bin"
Last reload type: Normal Reload
Last reload reason: Reload Command

Cisco 881 (MPC8300) processor (revision 1.0) with 236544K/25600K bytes of memory.
Processor board ID FTX1000038X

5 FastEthernet interfaces
1 Virtual Private Network (VPN) Module
256K bytes of non-volatile configuration memory.
126000K bytes of ATA CompactFlash (Read/Write)

License Info:
License UDI:
————————————————-
Device# PID SN
————————————————-
*0 CISCO881-SEC-K9 FTX1000038X

License Information for 'c880-data'
License Level: advipservices Type: Permanent
Next reboot license Level: advipservices

Configuration register is 0x2102
>>>>> end <<<<<
[/code]

The first thing we do is to import pprint to do pretty printing.

[python]
import pprint
[/python]

Then we’ll add the large blob of text from show version into a string called show_version.

[python]
show_version = """
Cisco IOS Software, C880 Software (C880DATA-UNIVERSALK9-M), Version 15.0(1)M4, RELEASE SOFTWARE (fc1)

Technical Support: http://www.cisco.com/techsupport

Copyright (c) 1986-2010 by Cisco Systems, Inc.

Compiled Fri 29-Oct-10 00:02 by prod_rel_team

ROM: System Bootstrap, Version 12.4(22r)YB5, RELEASE SOFTWARE (fc1)

twb-sf-881 uptime is 7 weeks, 5 days, 19 hours, 23 minutes

System returned to ROM by reload at 15:33:36 PST Fri Feb 28 2014

System restarted at 15:34:09 PST Fri Feb 28 2014

System image file is "flash:c880data-universalk9-mz.150-1.M4.bin"

Last reload type: Normal Reload

Last reload reason: Reload Command

Cisco 881 (MPC8300) processor (revision 1.0) with 236544K/25600K bytes of memory.

Processor board ID FTX1000038X

5 FastEthernet interfaces

1 Virtual Private Network (VPN) Module

256K bytes of non-volatile configuration memory.

126000K bytes of ATA CompactFlash (Read/Write)

License Info:

License UDI:

————————————————-

Device# PID SN

————————————————-

*0 CISCO881-SEC-K9 FTX1000038X

License Information for ‘c880-data’

License Level: advipservices Type: Permanent

Next reboot license Level: advipservices

Configuration register is 0x2102
"""
[/python]

We want to split this large string into a list so that we can do useful things with the data. We will split based on \n and store it in a list called show_version_list.

[python]
# Split string based on "\n" and store it "show_version_list"
show_version_list = show_version.split("\n")
[/python]

To store data in later we create an empty dictionary. A dictionary can be used to look up data based on a key. It’s a key/value store. The curly brackets indicate that it’s a dictionary and not a list which uses normal brackets.

[python]
# Create dictionary to store cleaned up data in
sh_ver_dict = {}
[/python]

We then need to loop through the data that we stored in the list show_version_list.

[python]
# Loop through all items in the list "show_version_list"
for line in show_version_list:
[/python]

To show what is currently in the list, I will loop through the list using enumerate() to show where our strings are currently placed.

[python]
for index, line in enumerate(show_version_list):
print(index, line)
[/python]

This is what the list currently looks like.

[code lang=text]
daniel@daniel-iperf3:~/python/Week4$ python3 show_version_parser.py
0
1 Cisco IOS Software, C880 Software (C880DATA-UNIVERSALK9-M), Version 15.0(1)M4, RELEASE SOFTWARE (fc1)
2
3
4
5 Technical Support: http://www.cisco.com/techsupport
6
7 Copyright (c) 1986-2010 by Cisco Systems, Inc.
8
9 Compiled Fri 29-Oct-10 00:02 by prod_rel_team
10
11 ROM: System Bootstrap, Version 12.4(22r)YB5, RELEASE SOFTWARE (fc1)
12
13
14
15 twb-sf-881 uptime is 7 weeks, 5 days, 19 hours, 23 minutes
16
17 System returned to ROM by reload at 15:33:36 PST Fri Feb 28 2014
18
19 System restarted at 15:34:09 PST Fri Feb 28 2014
20
21
22
23 System image file is "flash:c880data-universalk9-mz.150-1.M4.bin"
24
25 Last reload type: Normal Reload
26
27 Last reload reason: Reload Command
28
29
30
31 Cisco 881 (MPC8300) processor (revision 1.0) with 236544K/25600K bytes of memory.
32
33
34
35 Processor board ID FTX1000038X
36
37
38
39 5 FastEthernet interfaces
40
41 1 Virtual Private Network (VPN) Module
42
43 256K bytes of non-volatile configuration memory.
44
45 126000K bytes of ATA CompactFlash (Read/Write)
46
47
48
49 License Info:
50
51 License UDI:
52
53 ————————————————-
54
55 Device# PID SN
56
57 ————————————————-
58
59 *0 CISCO881-SEC-K9 FTX1000038X
60
61
62
63 License Information for 'c880-data'
64
65 License Level: advipservices Type: Permanent
66
67 Next reboot license Level: advipservices
68
69
70
71 Configuration register is 0x2102
72
[/code]

There are a lot of \n entries in there and also a lot of white space.

We want to start extracting information from the list and adding it to the dictionary. The first step is to look for the vendor. This is done by using an if statement and looking for a string. If we find Cisco IOS Software in the line that we are iterating through in our list, we will store that the vendor is Cisco under the key vendor in the dictionary sh_ver_dict. The OS version can be found by using split() and splitting on “,“. The string at index 2 will then be the OS version. To get only the number of the version, once again split based on Version and the number is stored at index 1.

[python]
# Loop through all items in the list "show_version_list"
for line in show_version_list:
# Check for vendor and OS version
if "Cisco IOS Software" in line:
sh_ver_dict["vendor"] = "Cisco"
os_version = line.split(",")[2]
sh_ver_dict["os_version"] = os_version.split("Version ")[1]
[/python]

The next thing we are looking for is the model of the device. To find the model we will look for bytes of memory. If we find that string then the second word in that line should be the model.

[python]
# Check for the model of the device
if "bytes of memory" in line:
# Model is second word in this line
sh_ver_dict["model"] = line.split()[1]
[/python]

After the model has been found, let’s look for the serial number. To find the serial number we look for the string Processor board ID. We can then split that string and the second word should be the serial number.

[python]
# Check for serial number
if "Processor board ID" in line:
sh_ver_dict["serial_number"] = line.split("Processor board ID ")[1]
[/python]

The final thing we are looking for is the uptime. We will search for the string uptime is and if we find that string the second word should be the uptime. We will strip off some white space with the strip() function.

[python]
# Check for uptime
if " uptime is " in line:
uptime = line.split(" uptime is ")[1]
uptime = uptime.strip()
sh_ver_dict["uptime"] = uptime
[/python]

All that is left then is to print the dictionary.

[python]
# Print the dictionary "sh_ver_dict"
pprint.pprint(sh_ver_dict)
[/python]

If we run the script, this is what we get.

[code lang=text]
daniel@daniel-iperf3:~/python/Week4$ python3 show_version_parser.py
{'model': '881',
'os_version': '15.0(1)M4',
'serial_number': 'FTX1000038X',
'uptime': '7 weeks, 5 days, 19 hours, 23 minutes',
'vendor': 'Cisco'}
[/code]

That’s all for this time! Get the code at Github!.

Python – Kirk Byers Course Week 4 Part 2

Leave a Reply

Your email address will not be published. Required fields are marked *