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

The final assignment in week 2 is the following:

IV. You have the following string from "show version" on a Cisco router

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

Note, the string is a single line; there is no newline in the string.

How would you process this string to retrieve only the IOS version:

   ios_version = "15.0(1)M4"


Try to make it generic (i.e. assume that the IOS version can change). 

You can assume that the commas divide this string into four sections and that the string will always have 'Cisco IOS Software', 'Version', and 'RELEASE SOFTWARE' in it.

The first thing we want to do is to split the string into several parts and put them in a list. We were told that we could use the comma as a separator. We’ll print the list and show the type to show what is going on.

cisco_ios_list = cisco_ios.split(",")
print(cisco_ios_list)
print(type(cisco_ios_list))

This gives us the following output:

daniel@daniel-iperf3:~/python/Week2$ python3 ios_version.py 
['Cisco IOS Software', ' C880 Software (C880DATA-UNIVERSALK9-M)', '\n Version 15.0(1)M4', ' RELEASE SOFTWARE (fc1)']

There was a “\n” inserted before “Version 15.0(1)M4” because we were using a multi line string. But we can take care of that later. Let’s print the list again with an index by using a For loop.

for index, entry in enumerate(cisco_ios_list):
    print(index, entry)

This gives us the list with the index:

daniel@daniel-iperf3:~/python/Week2$ python3 ios_version.py 
0 Cisco IOS Software
1  C880 Software (C880DATA-UNIVERSALK9-M)
2 
 Version 15.0(1)M4
3  RELEASE SOFTWARE (fc1)

We know that the version is stored at index 2. Let’s create a variable that stores the version and strip the “\n” from it. We will then print the variable.

ios_version = cisco_ios_list[2]
ios_version = ios_version.strip()
print(ios_version)

This gives us the version without the “\n”.

daniel@daniel-iperf3:~/python/Week2$ python3 ios_version.py 
Version 15.0(1)M4

We now have the version but we want to extract the version number from this. We can use “split()” and tell it to split the string into two parts, one with “Version ” and the other with whatever follows that string. Note the space that was inserted after the string. We then print the IOS version.

ios_version = ios_version.split("Version ")[1]
print(ios_version)

This should give us the version number:

daniel@daniel-iperf3:~/python/Week2$ python3 ios_version.py 
15.0(1)M4

We got the IOS version by splitting the string stored in “ios_version” into two parts where the first part at index 0 is “Version ” and the second part at index 1 is the version number.

I hope this has been informative and check the code at Github if you want to. See you next time!

Python – Kirk Byers Course Week 2 Part 4

Leave a Reply

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