08-19-2018 08:50 PM
What I'm trying to do is see if a file exists with EEM. There may be a better way to do this, but the way I have works, except when I try to use a variable for the filename.
Below is the config without variables. It also shows the correct results for when the file does and doesn't exist.
event manager applet eem_test authorization bypass event timer cron cron-entry "*/1 * * * *" action 01 syslog msg "Starting EEM Test" action 02 cli command "enable" action 03 cli command "dir flash:/" action 04 syslog msg "$_cli_result" action 05 regexp "test.txt" "$_cli_result"" action 06 syslog msg "$_regexp_result" action 07 if $_regexp_result eq "0" action 08 syslog msg "File doesn't exist" action 09 else action 10 syslog msg "File exists" action 11 end action 12 syslog msg "Finished with EEM Test" ----------File doesn't exist---------- Aug 19 23:31:00.006: %HA_EM-6-LOG: eem_test: Starting EEM Test Aug 19 23:31:00.041: %HA_EM-6-LOG: eem_test: Directory of flash:/ 2 -rwx 676 Jan 26 2018 12:37:42 -05:00 vlan.dat 3 -rwx 14738 Aug 19 2018 13:52:33 -04:00 config.text 579 -rwx 4120 Aug 19 2018 13:52:34 -04:00 multiple-fs 5 drwx 512 Aug 13 2017 19:02:15 -04:00 c3560cx-universalk9-mz.152-4.E3 576 drwx 512 Aug 13 2017 19:02:16 -04:00 dc_profile_dir 582 -rwx 1922 Aug 19 2018 13:52:34 -04:00 private-config.text 122185728 bytes total (95105536 bytes free) sw1# Aug 19 23:31:00.041: %HA_EM-6-LOG: eem_test: 0 Aug 19 23:31:00.041: %HA_EM-6-LOG: eem_test: File doesn't exist Aug 19 23:31:00.041: %HA_EM-6-LOG: eem_test: Finished with EEM Test ----------END---------- ----------File does exist---------- Aug 19 23:33:00.010: %HA_EM-6-LOG: eem_test: Starting EEM Test Aug 19 23:33:00.045: %HA_EM-6-LOG: eem_test: Directory of flash:/ 2 -rwx 676 Jan 26 2018 12:37:42 -05:00 vlan.dat 3 -rwx 14738 Aug 19 2018 13:52:33 -04:00 config.text 4 -rwx 582 Aug 19 2018 23:32:00 -04:00 test.txt 579 -rwx 4120 Aug 19 2018 13:52:34 -04:00 multiple-fs 5 drwx 512 Aug 13 2017 19:02:15 -04:00 c3560cx-universalk9-mz.152-4.E3 576 drwx 512 Aug 13 2017 19:02:16 -04:00 dc_profile_dir 582 -rwx 1922 Aug 19 2018 13:52:34 -04:00 private-config.text 122185728 bytes total (95104000 bytes free) sw1# Aug 19 23:33:00.045: %HA_EM-6-LOG: eem_test: 1 Aug 19 23:33:00.045: %HA_EM-6-LOG: eem_test: File exists Aug 19 23:33:00.045: %HA_EM-6-LOG: eem_test: Finished with EEM Test ----------END----------
The problem is when I try to use a variable for the filename, the $_regexp_result is always 0 whether the file exists or not. I've also tried using string match, but I get the same results.
event manager applet eem_test authorization bypass event timer cron cron-entry "*/1 * * * *" action 01 syslog msg "Starting EEM Test" action 02 set test_file "test.txt" action 03 cli command "enable" action 04 cli command "dir flash:/" action 05 syslog msg "$_cli_result" action 06 regexp "$test_file" "$_cli_result" action 07 syslog msg "$_regexp_result" action 08 if $_regexp_result eq "0" action 09 syslog msg "File doesn't exist" action 10 else action 11 syslog msg "File exists" action 12 end action 13 syslog msg "Finished with EEM Test" ----------File doesn't exist---------- Aug 19 23:41:00.008: %HA_EM-6-LOG: eem_test: Starting EEM Test Aug 19 23:41:00.043: %HA_EM-6-LOG: eem_test: Directory of flash:/ 2 -rwx 676 Jan 26 2018 12:37:42 -05:00 vlan.dat 3 -rwx 14738 Aug 19 2018 13:52:33 -04:00 config.text 579 -rwx 4120 Aug 19 2018 13:52:34 -04:00 multiple-fs 5 drwx 512 Aug 13 2017 19:02:15 -04:00 c3560cx-universalk9-mz.152-4.E3 576 drwx 512 Aug 13 2017 19:02:16 -04:00 dc_profile_dir 582 -rwx 1922 Aug 19 2018 13:52:34 -04:00 private-config.text 122185728 bytes total (95105536 bytes free) sw1# Aug 19 23:41:00.043: %HA_EM-6-LOG: eem_test: 0 Aug 19 23:41:00.043: %HA_EM-6-LOG: eem_test: File doesn't exist Aug 19 23:41:00.043: %HA_EM-6-LOG: eem_test: Finished with EEM Test ----------END---------- ----------File exists---------- Aug 19 23:42:00.006: %HA_EM-6-LOG: eem_test: Starting EEM Test Aug 19 23:42:00.041: %HA_EM-6-LOG: eem_test: Directory of flash:/ 2 -rwx 676 Jan 26 2018 12:37:42 -05:00 vlan.dat 3 -rwx 14738 Aug 19 2018 13:52:33 -04:00 config.text 4 -rwx 582 Aug 19 2018 23:41:18 -04:00 test.txt 579 -rwx 4120 Aug 19 2018 13:52:34 -04:00 multiple-fs 5 drwx 512 Aug 13 2017 19:02:15 -04:00 c3560cx-universalk9-mz.152-4.E3 576 drwx 512 Aug 13 2017 19:02:16 -04:00 dc_profile_dir 582 -rwx 1922 Aug 19 2018 13:52:34 -04:00 private-config.text 122185728 bytes total (95104000 bytes free) sw1# Aug 19 23:42:00.041: %HA_EM-6-LOG: eem_test: 0 Aug 19 23:42:00.041: %HA_EM-6-LOG: eem_test: File doesn't exist Aug 19 23:42:00.041: %HA_EM-6-LOG: eem_test: Finished with EEM Test
As you can see, it never detects that the file exists when I use a variable for the filename. Any help is appreciated. Thanks.
Solved! Go to Solution.
08-21-2018 03:00 PM
The regexp action in applets does not support variables in patterns. So what you're seeing is expected.
You can either convert this to EEM Tcl at https://www.marcuscom.com/convert_applet or do something like:
action 03 cli command "dir flash:/ | inc $test_file"
Then your regexp would look for something like "-rw-". If that exists, then there was a $test_file in the output.
08-21-2018 03:00 PM
The regexp action in applets does not support variables in patterns. So what you're seeing is expected.
You can either convert this to EEM Tcl at https://www.marcuscom.com/convert_applet or do something like:
action 03 cli command "dir flash:/ | inc $test_file"
Then your regexp would look for something like "-rw-". If that exists, then there was a $test_file in the output.
08-26-2018 03:07 PM
Thanks for the workaround option. I could see it working, but it's probably better to use TCL instead. It seems more powerful, but I've never used it and wasn't haven't much luck getting started with it. I'll give TCL a try.
Find answers to your questions by entering keywords or phrases in the Search bar above. New here? Use these resources to familiarize yourself with the community: