cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
5232
Views
0
Helpful
32
Replies

Script to inspect files in flash and create two sequential entries

Anthony LaRosa
Level 1
Level 1

What im trying to do:

1. Have the switch do a showtech every 2 hours

2. Send the showtech to flash

3. The first showtech will be techinfo1.txt

4. The second will be techinfo2.txt

5. The next showtech will overwrite techinfo1.txt

6. The next showtech will overwrite techinfo2.txt and so forth.

Is this possible? In the direction of regexpressions possibly?

Write now I have it appending to flash in one file and than once it hits a certain file size, clearing the file and restarting it. I don't like doing it like that.

::cisco::eem::event_register_timer cron name tech1 cron_entry 0 */2 ***

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


array set arr_einfo [event_reqinfo]

set x = file size flash:techinfo.txt

if {$x > 5242880}

file delete flash:techinfo.txt

else{

if [catch {cli_open} result] {
    error $result $errorInfo
} else {
    array set cli1 $result
}

if [catch {cli_exec $cli1(fd) "enable"} _cli_result] {
    error $_cli_result $errorInfo
}

if [catch {cli_exec $cli1(fd) "show tech-support | append flash:techinfo.txt"} _cli_result] {
    error $_cli_result $errorInfo
}

}

catch {cli_close $cli1(fd) $cli1(tty_id)} result

Credit to Joseph Clark for the applet converter

32 Replies 32

You still have that array set line in there.  Do this.  Replace the line starting with "array set" with these lines:

if { [file exists $filename] } {

    file stat $filename sarr

    if { [expr $now - $sarr(mtime)] < 14400 } {

        set filename "newtech.txt"

    }

}

Thanks, everything was correct.

I need to re-evaluate this because how I am using mtime the oldtech.txt will never be overwritten or updated.

Is there any kind of FIFO that can be implemented with TCL with a queue size of two?

I just need two showtechs to go into the flash and the oldest one always overwritten. I have a seperate applet triggering the EEM policy to run every two hours.

You first have to make sure you set filename appropriately before you send the output of show tech to it.  I'm not sure you're doing that in your current script.  Then for rotation, this should work:

set filename "flash:tech1.txt"

set nfilename "flash:text2.txt"

if { [file exists $filename] } {

    cli_exec $cli(fd) "copy $filename $nfilename"

}

cli_exec $cli(fd) "show tech | redirect $filename"

If the policy is already running every two hours, flash:tech1.txt will contain the latest stuff, and flash:tech2.txt will contain the previous run.

Yes great thanks, I was reading about the copy command. And incase the text2.txt file keeps appending I believe force copy can be used to overwrite it.

Initially I thought to send the showtech output to flash first which is why I redirected it there. I see that the variables should be the ones piping it to flash.

After some small adjustments it's timing out due to the $result variable. I thought this might be because result isnt being called back after the showtech runs.

I tried appending it to the showtech redirect line in the format "_cli_result" but receive the same error.

Policy

array set arr_einfo [event_reqinfo]

#Pass CLI Commands

if [catch {cli_open} result] {

    error $result $errorInfo

} else {

    array set cli1 $result

}

if [catch {cli_exec $cli1(fd) "enable"} _cli_result] {

    error $_cli_result $errorInfo

}

set filename "flash:oldtech.txt"

set nfilename "flash:newtech.txt"

if { [file exists $filename] } {

    cli_exec $cli1(fd) "copy $filename $nfilename"

}

cli_exec $cli1(fd) "show tech | redirect $filename" _cli_result

}

catch {cli_close $cli1(fd) $cli1(tty_id)} result

Error

Process Forced Exit- MAXRUN timer expired.

    while executing

"return -code error "error reading the channel: $result""

    (procedure "cli_exec" line 5)

    invoked from within

"cli_exec $cli1(fd) "copy $filename $nfilename""

    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: Process Forced Exit- MAXRUN timer expired.


This line:

cli_exec $cli1(fd) "show tech | redirect $filename" _cli_result

Should be:

cli_exec $cli1(fd) "show tech | redirect $filename"

Or:

if { [catch {cli_exec $cli1(fd) "show tech | redirect $filename"} _cli_result] } {

    error $_cli_result $errorInfo

}

You'll also need to set file prompt quiet to remove the prompting:

if { [file exists $filename] } {

    cli_exec $cli1(fd) "config t"

    cli_exec $cli1(fd) "file prompt quiet"

    cli_exec $cli1(fd) "do copy $filename $nfilename"

    cli_exec $cli1(fd) "no file prompt quiet"

    cli_exec $cli1(fd) "end"

}

Good tip with file prompt quiet so it doesn't ask for confirmation.

In my previous post i had already tried it the correct ways you posted so I am still at the same error as last post.

The file prompt quiet suppresses the prompt, and thus the timeout problem should go away (unless, of course, it takes more than 20 seconds to generate the show tech).  If you still need more time, you'll need to add a maxrun argument to the end of your event registration line (e.g., maxrun 60).

I had a stray bracket and the maxrun timer was too short as you suggested.

Do you see any issue with the current script below?

Its succesfully creating one showtech instance (oldtech.txt) and any time the script runs again it does not overwrite oldtech.txt and it does not create newtech.txt.

Essentially the "if" statement is being ignored.

I turned on all debugging and there are no errors when i run it.

::cisco::eem::event_register_none maxrun 900

#Namespace Imports

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


array set arr_einfo [event_reqinfo]


#Pass CLI Commands

if [catch {cli_open} result] {
    error $result $errorInfo
} else {
    array set cli1 $result
}

if [catch {cli_exec $cli1(fd) "enable"} _cli_result] {
    error $_cli_result $errorInfo
}

set filename "flash:oldtech.txt"

set nfilename "flash:newtech.txt"

if { [file exists $filename] } {
    cli_exec $cli1(fd) "config t"
    cli_exec $cli1(fd) "file prompt quiet"
    cli_exec $cli1(fd) "do copy $filename $nfilename"
    cli_exec $cli1(fd) "no file prompt quiet"
    cli_exec $cli1(fd) "end"
}

cli_exec $cli1(fd) "show tech | redirect $filename"


catch {cli_close $cli1(fd) $cli1(tty_id)} result

Post the output of "debug event manager tcl cli" when this policy runs. 

There was a heavy delay, my bad.

*Jan  6 12:47:13.990: %HA_EM-6-LOG: showtech.tcl : DEBUG(cli_lib) : OUT : Switch#

*Jan  6 12:47:13.991: %HA_EM-6-LOG: showtech.tcl : DEBUG(cli_lib) : CTL : cli_close called.

*Jan  6 12:47:14.273: %HA_EM-6-LOG: showtech.tcl : DEBUG(cli_lib) : CTL : cli_open called.

*Jan  6 12:47:14.406: %HA_EM-6-LOG: showtech.tcl : DEBUG(cli_lib) : OUT : Switch>

*Jan  6 12:47:14.407: %HA_EM-6-LOG: showtech.tcl : DEBUG(cli_lib) : IN  : Switch>enable

*Ja

--More-- n  6 12:47:14.616: %HA_EM-6-LOG: showtech.tcl : DEBUG(cli_lib) : OUT : Switch#

*Jan  6 12:47:14.619: %HA_EM-6-LOG: showtech.tcl : DEBUG(cli_lib) : IN  : Switch#config t

*Jan  6 12:47:14.855: %HA_EM-6-LOG: showtech.tcl : DEBUG(cli_lib) : OUT : Enter configuration commands, one per line.  End with CNTL/Z.

*Jan  6 12:47:14.855: %HA_EM-6-LOG: showtech.tcl : DEBUG(cli_lib) : OUT : Switch(config)#

*Jan  6 12:47:14.855: %HA_EM-6-LOG: showtech.tcl : DEBUG(cli_lib) : IN  : Switch(config)#file prompt quiet

*Ja

--More-- n  6 12:47:15.063: %HA_EM-6-LOG: showtech.tcl : DEBUG(cli_lib) : OUT : Switch(config)#

*Jan  6 12:47:15.063: %HA_EM-6-LOG: showtech.tcl : DEBUG(cli_lib) : IN  : Switch(config)#do copy flash:oldtech.txt flash:newtech.txt

*Jan  6 12:47:15.375: %HA_EM-6-LOG: showtech.tcl : DEBUG(cli_lib) : OUT : copy not allowed in config mode or submode

*Jan  6 12:47:15.375: %HA_EM-6-LOG: showtech.tcl : DEBUG(cli_lib) : OUT :

*Jan  6 12:47:15.375: %HA_EM-6-LOG: showtech.tcl : DEBUG(cli_lib) : OUT : Switch(config)#

*J

--More-- an  6 12:47:15.375: %HA_EM-6-LOG: showtech.tcl : DEBUG(cli_lib) : IN  : Switch(config)#no file prompt quiet

*Jan  6 12:47:15.587: %HA_EM-6-LOG: showtech.tcl : DEBUG(cli_lib) : OUT : Switch(config)#

*Jan  6 12:47:15.587: %HA_EM-6-LOG: showtech.tcl : DEBUG(cli_lib) : IN  : Switch(config)#end

*Jan  6 12:47:15.595: %SYS-5-CONFIG_I: Configured from console by  on vty0 (EEM:showtech.tcl)

*Jan  6 12:47:15.694: %HA_EM-6-LOG: showtech.tcl : DEBUG(cli_lib) : OUT : Switch#

*Jan  6 12:47:15.694: %HA_EM-6-LOG: sho

--More-- wtech.tcl : DEBUG(cli_lib) : IN  : Switch#show tech | redirect flash:oldtech.txt

Weird.  The "do" copy worked in my test.  Okay, change your if block to this:

if { [file exists $filename] } {

    cli_exec $cli1(fd) "config t"

    cli_exec $cli1(fd) "file prompt quiet"

    cli_exec $cli1(fd) "end"

    cli_exec $cli1(fd) "copy $filename $nfilename"

    cli_exec $cli1(fd) "config t"

    cli_exec $cli1(fd) "no file prompt quiet"

    cli_exec $cli1(fd) "end"

}

Thanks, it's very close now. I'm still having an issue that I've been trying to debug the last few days.

The oldtech and newtech are initially being created but the next time and every other time after that the oldtech does not get updated, just the newtech.

For Example:

The current script:

::cisco::eem::event_register_none maxrun 2200

#Namespace Imports

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


array set arr_einfo [event_reqinfo]


#Pass CLI Commands

if [catch {cli_open} result] {
    error $result $errorInfo
} else {
    array set cli1 $result
}

if [catch {cli_exec $cli1(fd) "enable"} _cli_result] {
    error $_cli_result $errorInfo
}

set filename "flash:oldtech.txt"

set nfilename "flash:newtech.txt"


if { [file exists $filename] } {

    cli_exec $cli1(fd) "config t"

    cli_exec $cli1(fd) "file prompt quiet"

    cli_exec $cli1(fd) "end"

    cli_exec $cli1(fd) "copy $filename $nfilename"

    cli_exec $cli1(fd) "config t"

    cli_exec $cli1(fd) "no file prompt quiet"

    cli_exec $cli1(fd) "end"

}


cli_exec $cli1(fd) "show tech | redirect $filename"


catch {cli_close $cli1(fd) $cli1(tty_id)} result

The Flash:

11     809388 Jan 08 2014 13:34:28 +00:00 oldtech.txt

12     809388 Jan 10 2014 07:01:04 +00:00 newtech.txt

The debugging

*Jan 10 06:58:18.409: %HA_EM-6-LOG: showtech.tcl: Process Forced Exit- MAXRUN timer expired.

*Jan 10 06:58:18.409: %HA_EM-6-LOG: showtech.tcl:     while executing

*Jan 10 06:58:18.409: %HA_EM-6-LOG: showtech.tcl: "if [catch {cli_read $fd} result] {

*Jan 10 06:58:18.409: %HA_EM-6-LOG: showtech.tcl:         return -code error "error reading the channel: $result"

*Jan 10 06:58:18.409: %HA_EM-6-LOG: showtech.tcl:     } else {

*Jan 10 06:58:18.409: %HA_EM-6-LOG: showtech.tcl:         return $result

*Jan 10 06:58:18.409: %HA_EM-6-LOG: showtech.tcl:     }"

*Jan 10 06:58:18.409: %HA_EM-6-LOG: showtech.tcl:     (procedure "cli_exec" line 4)

*Jan 10 06:58:18.409: %HA_EM-6-LOG: showtech.tcl:     invoked from within

*Jan 10 06:58:18.409: %HA_EM-6-LOG: showtech.tcl: "cli_exec $cli1(fd) "show tech | redirect $filename" "

*Jan 10 06:58:18.409: %HA_EM-6-LOG: showtech.tcl:     invoked from within

*Jan 10 06:58:18.409: %HA_EM-6-LOG: showtech.tcl: "$slave eval $Contents"

*Jan 10 06:58:18.409: %HA_EM-6-LOG: showtech.tcl:     (procedure "eval_script" line 7)

*Jan 10 06:58:18.409: %HA_EM-6-LOG: showtech.tcl:     invoked from within

*Jan 10 06:58:18.409: %HA_EM-6-LOG: showtech.tcl: "eval_script slave $scriptname"

*Jan 10 06:58:18.409: %HA_EM-6-LOG: showtech.tcl:     invoked from within

*Jan 10 06:58:18.409: %HA_EM-6-LOG: showtech.tcl: "if {$security_level == 1} {       #untrusted script

*Jan 10 06:58:18.409: %HA_EM-6-LOG: showtech.tcl:      interp create -safe slave

*Jan 10 06:58:18.409: %HA_EM-6-LOG: showtech.tcl:      interp share {} stdin slave

*Jan 10 06:58:18.409: %HA_EM-6-LOG: showtech.tcl:      interp share {} stdout slave

*Jan 10 06:58:18.409: %HA_EM-6-LOG: showtech.tcl: ..."

*Jan 10 06:58:18.409: %HA_EM-6-LOG: showtech.tcl:     (file "tmpsys:/lib/tcl/base.tcl" line 50)

*Jan 10 06:58:18.409: %HA_EM-6-LOG: showtech.tcl: Tcl policy execute failed:

*Jan 10 06:58:18.409: %HA_EM-6-LOG: showtech.tcl: Process Forced Exit- MAXRUN timer expired.

Switch#show event manager policy registered
No.  Class     Type    Event Type          Trap  Time Registered           Name
1    applet    user    timer countdown     Off   Fri Jan 10 06:04:58 2014  techtimer
time 1000.000
maxrun 20.000
action 1.0 policy showtech.tcl

2    script    user    none                Off   Fri Jan 10 06:18:30 2014  showtech.tcl
policyname {showtech.tcl} sync {yes}
nice 0 queue-priority normal maxrun 2200.000 scheduler rp_primary Secu none

I know from the debugging that the maxrun is still timing out but I don't beleive it is because of the length of the showtech. I timed the showtech at 16 minutes and set the maxrun timer to double that.

There must be another prompt that is thrown even though file prompt quiet is configured.  Try adding a "delete /force" command before you do the copy:

cli_exec $cli1(fd) "delete /force $nfilename"

   Same thing Joe, the newtech updates but the oldtech does not.

Can you post the new script as well as the "debug event manager tcl cli" output again?

Review Cisco Networking for a $25 gift card