Extract and Unify Cisco Device-Types with SNMP and Perl

Posted by: gdelmatto  :  Category: Cisco, Networking, Perl, Programming

Here’s a short script which I use to extract Cisco device-types from SNMP.
Bad enough, most of these devices return their device type ID differently, e.g. sometimes prefixed with an uppercase ‘C’, sometimes without any prefix, then again sometimes we find a suffix.

So here’s a snippet, that makes them look neat, so I can work with simple and unified looking device IDs.

Just make sure to fill in your hostnames and the SNMP community.

#!/usr/local/bin/perl -w

foreach $_SITE ('hostname1', 'hostname2', 'hostname3', 'hostname4') {

        my $_cpe_snmp_router_type = `/usr/local/bin/snmpget -v2c -c SNMP_COMMUNITY_NAME $_SITE .1.3.6.1.4.1.9.3.6.11.1.3.1`;

        print "debug: $_snmp_router_type \n";

        if ( $_snmp_router_type =~ /.*=\sSTRING:\s\"[cC]?(\d+[a-zA-Z0-9]+?)(?:\s.*)?\"/ ) {
                print "router type: $1 \n";
        } else {
                print "error: failed on extracting device ID!\n";
        }
}

And here’s what we get as output from this script:

debug: SNMPv2-SMI::enterprises.9.3.6.11.1.3.1 = STRING: "886"
 
router type: 886 
debug: SNMPv2-SMI::enterprises.9.3.6.11.1.3.1 = STRING: "887VDSL2"
 
router type: 887VDSL2 
debug: SNMPv2-SMI::enterprises.9.3.6.11.1.3.1 = STRING: "c2851 Motherboard with 2GE and integrated VPN"
 
router type: 2851 
debug: SNMPv2-SMI::enterprises.9.3.6.11.1.3.1 = STRING: "C836"
 
router type: 836 
debug: SNMPv2-SMI::enterprises.9.3.6.11.1.3.1 = STRING: "887VDSL2"
 
router type: 887VDSL2 
debug: SNMPv2-SMI::enterprises.9.3.6.11.1.3.1 = STRING: "C836"
 
router type: 836 
debug: SNMPv2-SMI::enterprises.9.3.6.11.1.3.1 = STRING: "877"
 
router type: 877 
debug: SNMPv2-SMI::enterprises.9.3.6.11.1.3.1 = STRING: "871"
 
router type: 871 
debug: SNMPv2-SMI::enterprises.9.3.6.11.1.3.1 = STRING: "1803"
 
router type: 1803 
debug: SNMPv2-SMI::enterprises.9.3.6.11.1.3.1 = STRING: "876"
 
router type: 876 

Comments are closed.