Display interface SNMP IfIndex

From CT3

Jump to: navigation, search

If you want to write your own SNMP agents or use interface-related SNMP variables in EEM applets or Tcl scripts, you need to know the interface indexes (ifIndex in SNMP terminology) to select the desired value from the interface-related MIBs. The show snmp mib ifmib ifindex command returns interface-to-ifIndex mappings in random order; this script sorts them by interface name or ifIndex.

Contents

Installation

  • download the source code into flash:ifindex.tcl
  • configure alias exec ifindex tclsh flash:ifindex.tcl

Usage guidelines

Usage: ifindex [ name | index ]

Command line parameters:

  • name: display the printout sorted by interface name (default)
  • index: display the printout sorted by ifIndex

Author

Ivan Pepelnjak, © 2008 NIL Data Communications

Source code

#
# Copyright (c) 2008 NIL Data Communications
# All rights reserved.
#
# by:       Ivan Pepelnjak, NIL Data Communications
# title:    Displays interface SNMP index sorted by interface name or ifIndex
# name:     ifindex.tcl
# desc:     This script displays SNMP interface indices
#
# ios config:
#
#           * download the file into flash:ifindex.tcl
#           * configure alias exec ifindex tclsh flash:ifindex.tcl
#
#           invoke with ifindex [name|index]
#

if { ! [string equal $tcl_platform(os) "Cisco IOS"] } {
  proc exec {fileName} {
    regsub -all {[|:\\]} $fileName {} fileName
    regsub -all {  } $fileName { } fileName
    set srcFile [open "$fileName.txt" "r"]
    set data [read $srcFile]
    close $srcFile
    return $data
  }
}

proc printSorted {dataname sort} {
  upvar $dataname data
  global lineFormat

  if {! [array exists data]} { return }
  foreach idx [lsort $sort [array names data]] {
    puts [format $lineFormat $idx $data($idx)]
  }
}

proc printByName {} {
  global lineFormat
  global nameindex

  set lineFormat "%-25s %5s"
  puts [format $lineFormat {Interface} {ifIndex}]
  puts "=============================================="
  printSorted nameindex {-ascii}
}

proc printByIndex {} {
  global lineFormat
  global indexname

  set lineFormat "%5s   %-25s"
  puts [format "%7s %-25s" {ifIndex} {Interface}]
  puts "=============================================="
  printSorted indexname {-integer}
}

set sort 0
set persist 1

switch [lindex $argv 0] {
  name    { set sort 0 }
  index   { set sort 1 }
  {}      { }
  default { puts {Syntax: tclsh ifindex.tcl [ name|index ]}; return; }
}

set cmdtext [exec {show snmp mib ifmib ifindex detail}]
foreach line [split $cmdtext "\n"] {
  puts $line
  set row [regexp -all -inline {\S+} $line]
  if { [string equal [lindex $row 2] "yes"] } {
    set ifname [lindex $row 0]
    set ifidx  [lindex $row 1]
    if { ! [string equal [lindex $row 3] "enabled"] } { set persist 0 }
    set nameindex($ifname) $ifidx
    set indexname($ifidx) $ifname
    puts [lindex $row 0]
  }
}

if {[array exists nameindex]} { 
  if { $sort == 0 } { printByName } else { printByIndex }
}

if { $persist == 0 } { 
 puts "\nWARNING: ifIndex is not persistent, enable with snmp-server ifindex persist" }
Personal tools

CT3

Main menu