cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
1094
Views
2
Helpful
2
Replies

EEM applet conditional logic based on show command output

Waterbird
Level 1
Level 1

In an  EEM applet I want to condition actions based on whether a show command outputs a result or does not output a result.

Here is what I have come up with so far:

conf t

event manager applet TEST 

event none

! creating a variable with a temporary value of none

action 0 set var1 "none"  

action 1 cli command "enable"

! assigning the command output to the variable

action 2 cli command "show run | include whatever " var1   

! checking if there is no actual output from the show command

action 3 if var1 eq " "

action 4 syslog msg "There was no show command ouput"

action 5 else

action 6 syslog msg "There was show command ouput"

action 7 end

exit

Questions:

On action 2,  I'm not clear about the syntax of assigning command output to a a variable, but this line doesn't seem to work.

On action 3, 1. I'm not clear on how to check if the show command output is completely empty.  I checked for a space " " but that's my best guess, I don't think it works.  I also tried "" but that gives an error.  When a show command has no result, it doesn't return anything in IOS and just returns to the prompt.  So not sure how to check this.

Any other assistance with getting this code working would be appreciated.

 

 

 

 

1 Accepted Solution

Accepted Solutions

M02@rt37
VIP
VIP

Hello @Waterbird,

I propose you this one:

event manager applet TEST
event none
action 1 cli command "enable"
action 2 cli command "show run | include whatever"
action 3 regexp "whatever" "$_cli_result" var1
action 4 if $var1 eq ""
action 5 syslog msg "There was no show command output"
action 6 else
action 7 syslog msg "There was show command output"
action 8 end

--In this version, action 2 runs the desired "show" command, and action 3 uses the "regex" keyword to extract the text "whatever" from the output and store it in the variable "var1". Note that the regular expression pattern needs to match the entire desired output, not just a portion of it. You can adjust the regular expression pattern as needed for your specific command and output.

For checking whether the output is completely empty, you can check whether the variable "var1" is equal to an empty string ("") as shown in action 4.

Best regards
.ı|ı.ı|ı. If This Helps, Please Rate .ı|ı.ı|ı.

View solution in original post

2 Replies 2

M02@rt37
VIP
VIP

Hello @Waterbird,

I propose you this one:

event manager applet TEST
event none
action 1 cli command "enable"
action 2 cli command "show run | include whatever"
action 3 regexp "whatever" "$_cli_result" var1
action 4 if $var1 eq ""
action 5 syslog msg "There was no show command output"
action 6 else
action 7 syslog msg "There was show command output"
action 8 end

--In this version, action 2 runs the desired "show" command, and action 3 uses the "regex" keyword to extract the text "whatever" from the output and store it in the variable "var1". Note that the regular expression pattern needs to match the entire desired output, not just a portion of it. You can adjust the regular expression pattern as needed for your specific command and output.

For checking whether the output is completely empty, you can check whether the variable "var1" is equal to an empty string ("") as shown in action 4.

Best regards
.ı|ı.ı|ı. If This Helps, Please Rate .ı|ı.ı|ı.

Thank you for this solution. 

I tested it and I think there may be some slight modification needed to this script. 

action 4 if $var1 eq "" doesn't seem to work as expected.  I get an syslog error that says %HA_EM-3-FMPD_UNKNOWN_ENV: fh_parse_var: could not find environment variable: var1

I tried changing this line to:

action 4 if $var1 ne "whatever"

But this gets the same error.

I'll keep troubleshooting but please let me know if you have any thoughts on how to overcome this error.