cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
573
Views
0
Helpful
3
Replies

Cleaning up EEM Script for NCS-520, filename to variables

Friis
Level 1
Level 1

Hi
I need some inputs how this case cn be solved best.
Then we reuse our equitment i have to build a script that can cleanup on the device, i think basicly so simple as some variables with file names to keep, then a loop that is handling files and folders, see if the current name is equal with name to keep, else, delete the item.

but how do i get a list of names, dir is giving a line with a lot of info, but it is only the last word in the line i have to use, but how? 

eg:
22 -rw- 1077 Nov 8 2024 11:00:57 +00:00 test.tcl

Here i want to get test.tcl in the variable.

3 Replies 3

Dan Frey
Cisco Employee
Cisco Employee

One option is to use regexp to parse out the filename on each line by using parenthesis to capture the syntax to be placed into variable(s).   The first set of () is subvariable1 and below that variable is called filename.    $line is the data to iterate over, "match" is the variable for the entire line, and subvar1 is "filename".   The files to capture can be limited to "include/exclude" when the dir command is executed.   

set lines [exec dir | inc test]
set lines [split $lines "\n"]
foreach line $lines {
    if  [regexp {([0-9A-Za-z._]+)\s+$} $line match filename] {
        file delete $filename
        puts "$filename deleted"
    }
}

 test:

clientPKI1(tcl)#set lines [exec dir | inc test]
43      -rw-               14   Nov 9 2024 21:49:42 +00:00  test3.txt
42      -rw-               14   Nov 9 2024 21:49:41 +00:00  test2.txt
41      -rw-               14   Nov 9 2024 21:49:41 +00:00  test1.txt

clientPKI1(tcl)#set lines [split $lines "\n"]
{43      -rw-               14   Nov 9 2024 21:49:42 +00:00  test3.txt
} {42      -rw-               14   Nov 9 2024 21:49:41 +00:00  test2.txt
} {41      -rw-               14   Nov 9 2024 21:49:41 +00:00  test1.txt
} {}
clientPKI1(tcl)#foreach line $lines {
+>    if  [regexp {([0-9A-Za-z._]+)\s+$} $line match filename] {
+>        file delete $filename
+>        puts "$filename deleted"
+>    }
+>}
test3.txt deleted
test2.txt deleted
test1.txt deleted

clientPKI1(tcl)#

 

Hi Thanks for your input.
i have now based your input made this eem:
event manager applet Cleanup_NCS authorization bypass
event none
action 4200 set do_not_delete "file-to-keep.txt"
action 4201 cli command "enable"
action 4210 cli command "dir | include ^[0-9]+.*[^)]$"
action 4215 foreach line "$_cli_result" "\n"
action 4225 regexp "(.*:00..)" "$line" match regud
action 4230 if $_regexp_result eq "1"
action 4235 string trimleft "$line" "$regud"
action 4240 set trimmed_filename "$_string_result"
action 4250 string match "*$do_not_delete*" "$trimmed_filename"
action 4255 if "$_string_result" eq "1"
action 4265 else
action 4270 cli command "delete /recursive /force $trimmed_filename"
action 4275 syslog msg "Deleted $trimmed_filename"
action 4280 end
action 4285 else
action 4290 end
action 4295 end

but can we do the regex more precise to eg match on the timestamp and timezone?

This regex \S+$ should match last word, but i cant get it to work, since it will be more bulletprof