Building network automation solutions

9 module online course

Start now!

SNMP with Tcl

Looking from the outside, it looks like Tcl SNMP routines in Cisco IOS were designed by a commitee or came straight from Dilbert. The snmp_getone function that reads a single SNMP value does not return an array or a list (as one would expect), but a string representation of something that looks like an XML object (but is not, since its attributes are not properly quoted). As Tcl on Cisco IOS has no built-in XML support, parsing the return values is a pure joy (and a nice exercise in writing regular expressions).

The following excerpt of a telnet session shows how to extract a single SNMP value in Tcl (I've used extra steps and an interactive tclsh session for illustration purposes). The SNMP community has to be configured in advance with the snmp-server community test ro configuration command.
rtr(tcl)#set value [snmp_getone test system.3.0]
{<obj oid='sysUpTime.0' val='14886'/>}
rtr(tcl)#regexp -inline {oid='(.*)'.*val='(.*)'} $value
{oid='sysUpTime.0' val='14886'} sysUpTime.0 14886
rtr(tcl)#regexp {oid='(.*)'.*val='(.*)'} $value ignore oid result
rtr(tcl)#puts $result
And now for a complete example: the following script prints the router uptime.
# Simple Tcl script to print system uptime
set value [snmp_getone test system.3.0]
regexp {oid='(.*)'.*val='(.*)'} $value ignore oid result
set result [expr $result / 100]
puts "Router uptime is $result seconds"

We migrated our blog a few days ago, and the commenting functionality is not there yet. In the meantime enjoy the older comments, or find our content on LinkedIn and comment there.


  1. Hi,

    it's very nice and look like pretty easy, but is any chance make it for current interface traffic load per 30 sec. For example i have router like 1800 and i would like check current interface load i'll use "show interfaces | inc rate" but i want to see periodicly?



  2. Hi Karl!

    Tcl isn't the best tool to use if you want to see a longer-term series of a value (for example, interface load). I would use a free network management graphing tool to do it (MRTG immediately springs to mind).

  3. Just found the perfect matching Dilbert strip ;-)