Do you remember back in CCNA school when we learned all sorts of great things that we very rarely followed. One of the favourites was that we are supposed to put meaningful descriptions on all of our interfaces so we know what the other side is connected to.
How many people actually follow that advice?
Yeah, I never do it either. There’s always just too many things on the list that need to get done and it seems like that extra 5 seconds it would take me to update the description to the interface just doesn’t seem like it’s worth the effort. Of course, then I later check the port and end up knocking out my XYZ services and cause myself an outage.
This is where a little python and a decent NMS can help to solve a problem.
Before we get into the code. We need to understand a little about ifIndex values and how they relate to the physical interfaces of the devices. If you’re REALLY interested, you can do some reading in RFC 2863. But in a nutshell, each interface on a device, whether physical or logical has a specific numeric value assigned to it which is the last digit in the interface statistics that can be seen through the SNMP interfaces. This is commonly known as interfaces group stats.
There are a bunch of different tables in the interface group stats that are used to store specific kinds of information or statistics for the interfaces such as
- ifNumber (.22.214.171.124.126.96.36.199.0) – Which is the total number of Interfaces
- ifIndex ( .188.8.131.52.184.108.40.206.1.1. * ) – Which acts as a primary key for all the other interface group stats.
- ifDescr (.220.127.116.11.18.104.22.168.1.2.* ) – Which is the name of the interface
- ifType (.22.214.171.124.126.96.36.199.1.3.* ) – Which describes the type of the interface
- ifMTU ( .188.8.131.52.184.108.40.206.1.4.*) – Which shows the current MTU size configured on the interface
- ifSpeed (.220.127.116.11.18.104.22.168.1.5.*) – Which shows the current speed of the interface
- ifPhysicalAddress (.22.214.171.124.126.96.36.199.1.6.*) – Which shows the mac address of the interface
- ifAdminStatus (.188.8.131.52.184.108.40.206.1.7.*) – Which shows the current admin status of the port
- ifOperStatus (.220.127.116.11.18.104.22.168.1.8.*) – Which shows the current operational status
There’s a bunch more which you can see here if you’re interested, but one that I found particularly fun was the ifAlias ( .22.214.171.124.126.96.36.199.1.1.18.* ) which actually corresponds to the description command in your friendly neighbourhood network operating system
So when your interface is configured like this
The ifAlias value for the corresponding ifIndex looks like this
The Fun Begins
The interesting part about the ifAlias value is that it’s actually a SET-able value through the SNMP interface. That means that if you have a simple piece of python code like the following
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|def set_snmp_single(rwstring, ip_address, ifAlias, description):|
|from pysnmp.entity.rfc3413.oneliner import cmdgen|
|from pysnmp.proto import rfc1902|
|cmdGen = cmdgen.CommandGenerator()|
|def set_interface_description(ipaddress, ifIndex, descr):|
|Issues SNMP SET Command to change the description on a interface of device|
|:param ifIndex: type int|
|:param descr: type string|
|ip_address = ipaddress|
|community_string = 'private'|
|snmp_port = 161|
|ifIndex = str(ifIndex)|
|descr = descr|
|ifAlias = str("188.8.131.52.184.108.40.206.1.1.18." + ifIndex)|
|set_snmp_single(community_string, ip_address, ifAlias, descr)|
It will allow you to run a little command like this >>> set_interface_description(‘10.101.0.221’, ‘1’, ‘Changed This’ ) which will result in the following.
So this is cool, right? We’ve just programatically changed a single interface description on a interface. We could stop here and you would be left with a
“So what? Why would I go to the trouble of doing that. It’s harder than just typing in the description manually!”
But wait! There’s more…
The really cool part about automating something so simple is that now we have a building block that we can do something with. For those of you who might have seen the HPE Intelligent Management Centre, you may already be aware that the topology map does this really cool thing where it actually creates a table of all of the links within a given custom view and automatically creates link-names for them.
You’ll notice that the auto-generated Link-Name actually tells me who’s connected to both sides of that link. You’ll also notice that I have the left and right nodes ( with the IP address ), as well as the left and right interfaces. And “yes”, there is an API for this where it’s all represented in a nice little JSON string which can be easily parsed in your favourite IDE.
Time vs ROI
The reason we don’t label interfaces is that most people feel it’s just not worth the effort to keep them up to date. Things change too often and it’s just too easy to say “it’s not that important” and move on before changing that description. I’m with you. I’m as guilty as everyone else on this. But with the help of a little code, a good NMS, the entire process is now automated.
I can proudly say that, for now at least, my lab is 100% accurate as far as interface descriptions go. And because the whole thing is totally automated, I simply re-run the script overtime I make some changes.
So for those of you who weren’t aware. Yes, there is someone who actually uses SNMP SETs in the world. 🙂