tcl scripting for snmp

Unanswered Question

I am trying to make a script that will give me the uptime of the router and send it to a database. The problem I have, the part of the script that doesn't work, work if I execute it from the tclshell on the router.

set value [snmp_getone public]
regexp {oid='(.*)'.*val='(.*)'} $value ignore oid _snmp_result
set _snmp_result [expr $_snmp_result]

Has you can see, it work well and give me the information I want.

router(tcl)#set value [snmp_getone public]
{<obj oid='sysUpTime.0' val='1810190'/>}
stnley600(tcl)#regexp {oid='(.*)'.*val='(.*)'} $value ignore oid _snmp_result
router(tcl)#set _snmp_result [expr $_snmp_result]

But if I execute from a tcl file I have an error that tell me it didn't recognize the command snmp_getone

router#event manager run routeruptime.tcl
invalid command name "snmp_getone"
    while executing
"snmp_getone public"
    invoked from within
"$slave eval $Contents"
    (procedure "eval_script" line 7)
    invoked from within
"eval_script slave $scriptname"
    invoked from within
"if {$security_level == 1} {       #untrusted script
     interp create -safe slave
     interp share {} stdin slave
     interp share {} stdout slave
    (file "tmpsys:/lib/tcl/base.tcl" line 50)
Tcl policy execute failed: invalid command name "snmp_getone"

I am wondering if I need to import something else. Right now, all I have is this


namespace import ::cisco::eem::*
namespace import ::cisco::lib::*

I am not a pro in tcl scripting so your help would be very appreciated.


  • 1
  • 2
  • 3
  • 4
  • 5
Overall Rating: 0 (0 ratings)
Joe Clarke Thu, 07/15/2010 - 18:14
User Badges:
  • Cisco Employee,
  • Hall of Fame,

    Founding Member

You are mixing tclsh code with EEM Tcl code.  The snmp_* commands are not available in EEM Tcl.  If you want to get SNMP data in an EEM Tcl policy, you need to use the sys_reqinfo_snmp command.  In your example, this code should work:

array set snmp_res [sys_reqinfo_snmp oid get_type exact]

set snmp_val $snmp_res(value)

action_syslog msg "Uptime is $snmp_val timeticks"


This Discussion

Related Content