02-02-2018 03:26 PM - edited 03-01-2019 06:23 PM
Hi I am very new EEM and Cisco network. I have all ready done a few Eem scripts.
But can't figure out the syntax, when I have to run multi routines.
The system is running EEM 4.0
What I would like to do is first to check for the version of Eem by using If and else commands, and for me it has been quite hard to know where to have the "end" and how manny.
The next step is to have a loop running to check for a ip by use cli command "sh arp" and keep doing this until it show up, if the ip show run a new set command changes the config after this eem should removing from running config, before saving it.
The separate pices of the code runs fine, but when I put them together, it jumps to the wrong place, don't read the full eem, says it missing end, or it is out side the eem, times out , or "else" variable unable to see "if".
What is the syntax for "if else " ?
What command should I use for running loop condition ?
Sometimes I get "confirm" "y" response how do I send this to the cli ?
thanks in advance, hope someone can help
Solved! Go to Solution.
02-27-2018 02:42 PM
I just re-read this. There absolutely is an elseif action. What I was trying to convey is there is no boolean logic one can perform with an if. So doing things like "if a && b" or "if a || b" is not doable directly. For those, you will need to either use nesting or use some fairly ugly logic (maybe involving goto).
02-03-2018 11:31 AM
There is no "else if" syntax in EEM. You have to nest if's instead:
if $var eq 1
...
else
if $var eq 2
...
end
end
I don't understand the second question. If you're asking for what is the syntax for a loop, you can use the while or foreach actions.
You can use the pattern keyword for the cli action:
cli command "write erase" pattern "confirm"
cli command "\n"
02-03-2018 02:03 PM
Thanks you for the replay.
this is the EEM code i am trying to put together there could be alot of mistakes in it, EEM is very new to me
!Check version!
event timer countdown time 10
action 0001 comment -----------------------------------
action 0010 set iface "x"
action 0020 set old_gw "x.x.x.x"
action 0030 set new_ip_mask "x.x.x.x x.x.x"
action 0040 set new_gw "x.x.x.x"
action 0099 comment -----------------------------------
action 0100 cli command "enable"
action 0101 cli command "terminal monitor"
action 0105 syslog priority alerts msg "Running IP-change script"
action 0120 cli command "sh event manager version"
action 0125 regexp "Embedded Event Manager Version 4.00" "$_cli_result" _match
action 0150 if $_regexp_result eq "1"
action 0151 syslog priority alerts msg "Event manager V 4.0 - script will proceeding"
action 0200 syslog priority critical msg "Preparing for IP address change"
action 0954 syslog priority alerts msg "Event manager wrong version - script stopped"
action 0957 end
! Check if the ip show up in arp !
action 0300 while $_regexp_result ne $new_gw
action 0310 cli command "sh arp | include interface $iface"
action 0330 cli command "clear arp interface $iface"
action 0340 wait 120
action 0400 if $_regexp_result ne $new_gw _macth 1
action 0410 cli command "conf t""
action 0420 cli command "no event manager applet IP-CHANGE"
action 0430 cli command "end"
action 0440 cli command "cli command "config confirm"
02-04-2018 02:57 AM
EEM labels (they're not line numbers) are treated as ASCII strings, and thus evaluated in lexicographical order. So the policy that you've written below scatters logic at wrong location, and ends up just being gibberish.
I'm not sure why EEM 4.0 matters. Nothing in your policy requires any EEM 4.0 features. I'm really not sure what you're trying to accomplish. Is it that you want to configure a new GW and then check ARP to make sure that GW shows up? I'm also not sure why you need "config confirm" since you're not doing a config replace or config revert.
02-27-2018 06:58 AM
Hi I have a script
but I have two issues with it,
It don´t work with two variabler, issue is in "action 0760 regexp "192.168.253.1" "$_cli_result" _match" but I would like to use the variable New_gw instead of the IP.
The other issue is that when I run the script in vty, the syslog messeges don´t show up in the command line
event manager applet IP-CHANGE
event timer countdown time 10 maxrun 6000
action 0001 comment -----------------------------------
action 0010 set iface "GigabitEthernet0/0"
action 0020 set old_gw "192.168.251.1"
action 0030 set new_ip_mask "192.168.253.32 255.255.255.0"
action 0040 set new_gw "192.168.253.1"
action 0050 set arp_target "192.168.253.1"
action 0060 set max_tries "20"
action 0099 comment -----------------------------------
action 0100 syslog priority alerts msg "IP change 30 second countdown initiated, change $iface to $new_ip_mask"
action 0400 cli command "enable"
action 0410 cli command " terminal monitor "
action 0410 cli command "conf t"
action 0420 cli command "interface $iface"
action 0430 cli command "ip addr $new_ip_mask secondary"
action 0450 cli command "ip route 0.0.0.0 0.0.0.0 $new_gw 200"
action 0460 cli command "end"
action 0500 syslog priority notifications msg "check for new ip seting $iface is $new_ip_mask, gateway is $new_gw."
action 0600 set num_try "1"
action 0700 while $num_try le $max_tries
action 0730 syslog priority notifications msg "Initiating connectivity check to $arp_target"
action 0740 wait 60
action 0760 regexp "192.168.253.1" "$_cli_result" _match
action 0764 cli command "ping $new_gw"
action 0765 cli command "sh arp | include $iface"
action 0770 if $_regexp_result eq "1"
action 0771 syslog priority alerts msg "Ip present in arp! Confirm change and exit script."
action 0772 cli command "conf t"
action 0773 cli command "interface $iface"
action 0774 cli command "no ip addr $new_ip_mask secondary"
action 0775 cli command "ip addr $new_ip_mask"
action 0776 cli command "no ip route 0.0.0.0 0.0.0.0 $old_gw"
action 0777 cli command "ip route 0.0.0.0 0.0.0.0 $new_gw"
action 0778 cli command "end"
action 0779 cli command "no event manager applet IP-CHANGE"
action 0780 cli command "wr"
action 0790 exit
action 0800 else
action 0810 syslog priority alerts msg "Ip arp test #$num_try min failed."
action 0820 increment num_try 1
action 0825 syslog priority alerts msg "Retest in 1 min"
action 0830 end
action 0900 end
action 0910 syslog priority alerts msg "All arp test for the new ipsettting, reverting change"
action 0940 syslog priority critical msg "rollback to old config"
action 0960 cli command "configure replace nvram:startup-config" pattern "proceed."
action 0980 cli pattern [proceed.] command y
action 0990 syslog priority notifications msg "Reversion complete
action 0998 exit
Hop you can help ?
Br Søren
02-27-2018 03:02 PM
The syslog will not come out on the current VTY unless you have terminal monitor enabled. Note: EEM uses buginf to send syslog messages, so you need to have logging level set to debug where you want EEM syslog messages to be sent/captured. Also note: The terminal monitor you have in your applet will do nothing as this will be run in another VTY session asynchronously from your current terminal. In fact, this whole policy is asynchronous.
The regexp action does not support variables in the pattern space. You need to use Tcl if this is a hard requirement. You might be able to get around this using your "include" argument a bit differently:
"show arp | inc $new_gw"
Then match on the literal words that should appear if the include was able to match.
Finally, this is not a good design. Your countdown is set to run every 10 seconds with a maxrun of 6000. This means the applet will over-run itself (up to the maximum number of threads). By default, my math says you can easily have five of these running at once. I would pick another event that will only run once, then keep your high maxrun and handle all your retry logic in the same applet.
Other issues: action 0779 is a no-op as you've exited config mode here. Action 0980 is odd since [proceed.] likely won't match what you want since '[' and ']' are regex meta-characters.
02-27-2018 02:42 PM
I just re-read this. There absolutely is an elseif action. What I was trying to convey is there is no boolean logic one can perform with an if. So doing things like "if a && b" or "if a || b" is not doable directly. For those, you will need to either use nesting or use some fairly ugly logic (maybe involving goto).
03-01-2018 02:14 AM
Thank you
I think i will try with tcl
br Søren
Discover and save your favorite ideas. Come back to expert answers, step-by-step guides, recent topics, and more.
New here? Get started with these tips. How to use Community New member guide