Cisco Support Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Announcements

Welcome to Cisco Support Community. We would love to have your feedback.

For an introduction to the new site, click here. And see here for current known issues.

New Member

tcl scripting for snmp

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.

tcl
set value [snmp_getone public 1.3.6.1.2.1.1.3.0]
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#tclsh
router(tcl)#set value [snmp_getone public 1.3.6.1.2.1.1.3.0]
{<obj oid='sysUpTime.0' val='1810190'/>}
stnley600(tcl)#regexp {oid='(.*)'.*val='(.*)'} $value ignore oid _snmp_result
1
router(tcl)#set _snmp_result [expr $_snmp_result]
1810190

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 1.3.6.1.2.1.1.3.0"
    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

::cisco::eem::event_register_none

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


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

Thanks

Everyone's tags (3)
2 REPLIES
Cisco Employee

Re: tcl scripting for snmp

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 1.3.6.1.2.1.1.3.0 get_type exact]

set snmp_val $snmp_res(value)

action_syslog msg "Uptime is $snmp_val timeticks"

New Member

Re: tcl scripting for snmp

Thanks!!!

It's working like a charm now.

3391
Views
0
Helpful
2
Replies