Generate List of Subnets on Switch Using SNMP

ciscosnmpsubnetswitch

Is anyone familiar with an approach to generate a list of subnets on a particular switch (let's pick a more popular one – Cisco) using SNMP? How about a list of VLANs? If so, can you point me to the MIBs / OIDs involved? (network newbie here)

Best Answer

Interfaces/IP Addressing:

If you're looking to poll a device's IP address, subnet mask and corresponding interface you can use the following OIDs from the IP-MIB and IF-MIB MIBs:

  • .1.3.6.1.2.1.4.20.1.1 - The IP address can be found at this OID

    ~]$ snmptranslate .1.3.6.1.2.1.4.20.1.1
    IP-MIB::ipAdEntAddr
    
    ~]$ snmpwalk -v2c -c cisco 10.30.46.1 .1.3.6.1.2.1.4.20.1.1
    IP-MIB::ipAdEntAddr.10.30.46.1 = IpAddress: 10.30.46.1
    IP-MIB::ipAdEntAddr.25.255.25.254 = IpAddress: 25.255.25.254
    IP-MIB::ipAdEntAddr.55.44.33.22 = IpAddress: 55.44.33.22
    IP-MIB::ipAdEntAddr.172.31.10.10 = IpAddress: 172.31.10.10
    


  • .1.3.6.1.2.1.4.20.1.3 - The subnet mask cane be found at this OID

    ~]$ snmptranslate .1.3.6.1.2.1.4.20.1.3
    IP-MIB::ipAdEntNetMask
    
    ~]$ snmpwalk -v2c -c cisco 10.30.46.1 .1.3.6.1.2.1.4.20.1.3
    IP-MIB::ipAdEntNetMask.10.30.46.1 = IpAddress: 255.255.255.0
    IP-MIB::ipAdEntNetMask.25.255.25.254 = IpAddress: 255.255.255.0
    IP-MIB::ipAdEntNetMask.55.44.33.22 = IpAddress: 255.255.255.0
    IP-MIB::ipAdEntNetMask.172.31.10.10 = IpAddress: 255.255.255.0
    


  • .1.3.6.1.2.1.4.20.1.2 - The interface index (ifTable Indexes) are unique integers for each interface.

    ~]$ snmptranslate .1.3.6.1.2.1.4.20.1.2
    IP-MIB::ipAdEntIfIndex
    
    ~]$ snmpwalk -v2c -c cisco 10.30.46.1 .1.3.6.1.2.1.4.20.1.2
    IP-MIB::ipAdEntIfIndex.10.30.46.1 = INTEGER: 1
    IP-MIB::ipAdEntIfIndex.25.255.25.254 = INTEGER: 5
    IP-MIB::ipAdEntIfIndex.55.44.33.22 = INTEGER: 6
    IP-MIB::ipAdEntIfIndex.172.31.10.10 = INTEGER: 7
    


  • .1.3.6.1.2.1.2.2.1.2 - The interface friendly name can be found at this OID and the ifTable index is appended (eg. ...2.1.2.[INDEX]) for each interface.

    ~]$ snmptranslate .1.3.6.1.2.1.2.2.1.2
    IF-MIB::ifDescr
    
    ~]$ snmpwalk -v2c -c cisco 10.30.46.1 .1.3.6.1.2.1.2.2.1.2
    IF-MIB::ifDescr.1 = STRING: FastEthernet0/0
    IF-MIB::ifDescr.2 = STRING: FastEthernet0/1
    IF-MIB::ifDescr.4 = STRING: Null0
    IF-MIB::ifDescr.5 = STRING: Loopback0
    IF-MIB::ifDescr.6 = STRING: Tunnel10
    IF-MIB::ifDescr.7 = STRING: Dialer1
    IF-MIB::ifDescr.8 = STRING: Virtual-Access1
    

You can walk these OIDs manually, script something out in the language of your choice, or use much smarter programs/scripts similar to what's mentioned in Tim Peck's answer.

Here's a quick (and dirty) shell example:

#!/bin/bash
# duct taped by one.time
# Basic interface information collector

##
# Set usage var and getoptions
usage="Usage: interface-info.sh -H <IP Address> -C <snmp community string>

OPTIONS:
  -H Hostname          set IP address or hostname
  -h Help              prints usage options

SNMPv2 OPTIONS:
  -C Community         set SNMPv2 community string
"

while getopts H:C:h option;
do
        case $option in
                H) ipaddress=$OPTARG;;
                C) community=$OPTARG;;
                h) echo "$usage"
                exit $invalid_result;;
        esac
done

##
# Prevent blank argvars
if [[ -z $ipaddress || -z $community ]]; then
  echo "$usage"
  exit 0
fi

## 
# Set field separator to new line
IFS=$'\n'

##
# Store our IP-MIB info in arrays
ipAdEntAddr=( $(snmpbulkwalk -v2c -c $community  $ipaddress .1.3.6.1.2.1.4.20.1.1 | awk -F ": " '{print $2}') )
ipAdEntNetMask=( $(snmpbulkwalk -v2c -c $community $ipaddress .1.3.6.1.2.1.4.20.1.3 | awk -F ": " '{print $2}') )
ipAdEntIfIndex=( $(snmpbulkwalk -v2c -c $community $ipaddress .1.3.6.1.2.1.4.20.1.2 | awk -F ": " '{print $2}') )

for ((i=0; i<${#ipAdEntAddr[@]}; i++)); do
  ifDescr[$i]=$(snmpwalk -v2c -c $community $ipaddress .1.3.6.1.2.1.2.2.1.2.${ipAdEntIfIndex[$i]} | awk -F ": " '{print $2}')
  echo "${ifDescr[$i]}: ${ipAdEntAddr[$i]} ${ipAdEntNetMask[$i]}"
done

Example:

~]$ ./interface-info.sh -H 10.30.46.1 -C cisco
FastEthernet0/0: 10.30.46.1 255.255.255.0
Loopback0: 25.255.25.254 255.255.255.0
Tunnel10: 55.44.33.22 255.255.255.0
Dialer1: 172.31.10.10 255.255.255.0


VLANs:

If you're looking for the VLAN IDs and VLAN names you can use the following OID:

  • .1.3.6.1.4.1.9.9.46.1.3.1.1.4.1 The vtpVlanName can be found (on Cisco devices) at this OID and the VLAN-ID can be found appended, e.g.: ...1.4.1.[VLAN-ID] (similar to the ifIndex and ifDescr example above).

    ~]$ snmptranslate  .1.3.6.1.4.1.9.9.46.1.3.1.1.4.1
    SNMPv2-SMI::enterprises.9.9.46.1.3.1.1.4.1
    
    ~]$ snmpwalk -v2c -c cisco 192.168.0.8 SNMPv2-SMI::enterprises.9.9.46.1.3.1.1.4.1
    SNMPv2-SMI::enterprises.9.9.46.1.3.1.1.4.1.1 = STRING: "default"
    SNMPv2-SMI::enterprises.9.9.46.1.3.1.1.4.1.10 = STRING: "VLAN0010"
    SNMPv2-SMI::enterprises.9.9.46.1.3.1.1.4.1.21 = STRING: "VLAN0021"
    SNMPv2-SMI::enterprises.9.9.46.1.3.1.1.4.1.100 = STRING: "VLAN0100"
    SNMPv2-SMI::enterprises.9.9.46.1.3.1.1.4.1.344 = STRING: "VLAN0344"
    SNMPv2-SMI::enterprises.9.9.46.1.3.1.1.4.1.456 = STRING: "iSCSI-TRAFFIC"
    SNMPv2-SMI::enterprises.9.9.46.1.3.1.1.4.1.1002 = STRING: "fddi-default"
    SNMPv2-SMI::enterprises.9.9.46.1.3.1.1.4.1.1003 = STRING: "token-ring-default"
    SNMPv2-SMI::enterprises.9.9.46.1.3.1.1.4.1.1004 = STRING: "fddinet-default"
    SNMPv2-SMI::enterprises.9.9.46.1.3.1.1.4.1.1005 = STRING: "trnet-default"
    


Manual example of scraping the VLAN IDs:

    ~]$ snmpbulkwalk -v2c -c cisco 192.168.0.8 1.3.6.1.4.1.9.9.46.1.3.1.1.4 | sed -e 's/.*4.1.\(.*\) =.*/\1/'
    1
    10
    21
    100
    344
    456
    1002
    1003
    1004
    1005