tcl scripting for snmp
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-15-2010 12:50 PM
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
- Labels:
-
Network Management

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-15-2010 06:14 PM
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"
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-16-2010 06:49 AM
Thanks!!!
It's working like a charm now.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-21-2022 09:52 AM
Hi Joe,
Any chance I could get help with pulling configurations or even pushing configurations? I used to have the scripts but are lost to me...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-21-2022 12:59 PM
Hello,
below is a script you could use for sending configs to a TFTP server:
vent manager applet BACKUP_CONF_TFTP
event syslog pattern "%SYS-5-CONFIG_I: Configured from"
action 0.01 info type routername
action 1.01 cli command "enable"
action 1.02 cli command "show clock"
action 1.03 regexp "(2[0-3]|[01][0-9]):([0-6][0-9]):([0-6][0-9])" "$_cli_result" time hour minute second
action 1.04 puts "$time"
action 1.05 puts "$hour"
action 1.06 puts "$minute"
action 1.07 puts "$second"
action 1.11 cli command "show clock"
action 1.12 regexp "(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) ([1-9]|0[1-9]|[1-2][0-9]|3[0-1]) (20[1-9][0-9])" "$_cli_result" time2 month day year
action 1.13 puts "$time2"
action 1.14 puts "$month"
action 1.15 puts "$day"
action 1.16 puts "$year"
action 2.01 cli command "configure terminal"
action 2.02 cli command "file prompt quiet"
action 2.03 cli command "do copy run tftp://10.0.0.10/$_info_routername-$year$month$day-$hour$minute$second.txt"
