cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
6544
Views
5
Helpful
22
Replies

Calling a variable in regular expression

CORY HEBERT
Level 1
Level 1

Hi.  Is there any way to call a variable in a regular expression, instead of explicitly defining it?  For example,

 

THIS WORKS:

 action 10   cli command "show run | section router ospf"
 action 20   regexp "passive-interface Serial4/0" "$_cli_result"

 

BUT THIS DOES NOT:

event manager environment _circuit1 Serial4/0

...

 action 10   cli command "show run | section router ospf"
 action 20   regexp "passive-interface $_circuit1" "$_cli_result"

 

1 Accepted Solution

Accepted Solutions

Joe Clarke
Cisco Employee
Cisco Employee

Unfortunately, not all fields in an applet are set for variable expansion.  This is one of them.  You can convert this applet to Tcl using our tool at http://www.marcuscom.com/convert_applet/ .  Or you could do:

 

action 20 regexp "passive interface ([A-Za-z0-9/\.:]+)" $_cli_result match intf

action 30 if $intf eq $_circuit1

...

View solution in original post

22 Replies 22

Joe Clarke
Cisco Employee
Cisco Employee

Unfortunately, not all fields in an applet are set for variable expansion.  This is one of them.  You can convert this applet to Tcl using our tool at http://www.marcuscom.com/convert_applet/ .  Or you could do:

 

action 20 regexp "passive interface ([A-Za-z0-9/\.:]+)" $_cli_result match intf

action 30 if $intf eq $_circuit1

...

WOW, thanks Joseph!  That regexp worked like a champ.

Sooo glad you monitor and respond to these discussions.

Ok, I have a follow-up.  How can I modify this script to catch more than one passive-interface.  I have this:


 action 20   regexp "passive-interface ([A-Za-z0-9/\.:]+)" "$_cli_result" match _intf
 action 30   if $_intf eq $_circuit1
 action 40   elseif $_intf eq $_circuit2
 

...but it'll stop at action 30 if _circuit1 is found.  I have to run the script again to catch _circuit2.  How can I run the script once to catch both circuits?

event manager environment circuits Serial4/0 Serial3/0 Serial2/0

!

action 20 foreach circuit $circuits

action 30 if $intf eq $circuit

...

Ok, not sure what I'm missing, but the script will only catch the 1st interface listed as passive, but not the 2nd.

 

 

event manager environment _passive Serial0/0/0 Serial0/1/0

!

event manager applet UNPASSIVE
 event none
 action 10 cli command "show run | section router ospf"
 action 20 regexp "passive-interface ([A-Za-z0-9/\.:]+)" "$_cli_result" match _intf
 action 25 foreach circuit "$_passive"
 action 30  if $_intf eq $circuit
 action 40   puts "$_intf"
 action 50  end
 action 99 end
!

If the config contains more than one passive interface, you need to move the foreach loop higher:

 

action 20 foreach line $_cli_result "\n"

action 30  regexp "passive-interface ([A-Za-z0-9/\.:]+)" $line match intf

action 40  foreach circuit $passive

action 50   if $intf eq $circuit

action 60    puts $intf

action 70   end

action 80  end

action 90 end

Thanks, Joe!  I was finally able to get it working perfectly thanks to your help.  For clarity, here's what I ended up with:

 

event manager environment passive Serial0/0/0 Serial0/1/0
!
event manager applet UNPASSIVE
 event none
 action 10 cli command "show run | section router ospf"
 action 20 foreach line "$_cli_result" "\n"
 action 30 set intf "none"
 action 40  regexp "passive-interface ([A-Za-z0-9/\.:]+)" "$line" match intf
 action 50  foreach circuit "$passive"
 action 60   if $circuit eq $intf
 action 70    puts "$intf"
 action 97   end
 action 98  end
 action 99 end
!

Strange things going on.  The above script worked perfectly in the lab.  I changed the event to a CRON job, and it worked fine.  I added the same script to a production device...and the history shows it invoking the script, but nothing happens.

LAB - 15.1(4)M7

PROD - 12.4(24)T3

Surely this has got to be a bug, right?  I even tweaked it to run every minute and added a bunch of 'puts' so I could see how far the script was going:

 

event manager applet UNPASSIVE
 event timer cron cron-entry "0-59 * * * *"
 action 05   puts "STARTING!!!"
 action 10   cli command "show run | section router ospf"
 action 11   puts "11"
 action 20   foreach line "$_cli_result" "\n"
 action 21    puts "21"
 action 30    set intf "none"
 action 31    puts "31"
 action 40    regexp "passive-interface ([A-Za-z0-9/\.:]+)" "$line" match intf
 action 41    puts "41"
 action 50    if $intf eq $circuit1
 action 50.1   syslog msg "CRON job making $intf unpassive"
 action 50.2   cli command "enable"
 action 50.3   cli command "config t"
 action 50.4   cli command "router ospf 77"
 action 50.5   cli command "no passive-interface $intf"
 action 50.6   cli command "end"
 action 51     puts "51"
 action 98    end
 action 99   end
!

 

Still, nothing.  I then changed the event to 'none', and invoked it manually, and I got this result:

STARTING!!!
11
21
31
41
21
31
41
21
31
41
21
31
41

 

It looks like it only read the first 4 lines of the OSPF config before stopping, when in reality my 'passive-interface' commands are further down than that.

 

1) Why did this script only work manually and not with CRON?

2) How do I get this script to run ALL lines of the OSPF section, and not just the first 4?

 

 

 

The timing of these messages would be more revealing than the messages themselves.  Perhaps your applet is taking too much time on the production router.  Try increasing maxrun to 60 and see if that helps.

Tried increasing maxrun...no affect.  I also tried a much simpler version of the script:

event manager applet UNPASSIVE
 event timer cron cron-entry "0-59 * * * *"
 action 10   cli command "show run | section router ospf"
 action 11 puts 11
 action 20   regexp "passive-interface Serial4/0" "$_cli_result"
 action 21 puts 21
 action 30   if $_regexp_result eq 1
 action 30.1  syslog msg "CRON job making $circuit1 unpassive"
 action 30.2  cli command "enable"
 action 30.3  cli command "config t"
 action 30.4  cli command "router ospf 77"
 action 30.5  cli command "no passive-interface $circuit1"
 action 30.6  cli command "end"
 action 31 puts 31
 action 40   end
!

 

And here's the OSPF config:

router ospf 77
 router-id xxxxxxxxxxxxxxxxx
 log-adjacency-changes
 auto-cost reference-bandwidth 20000
 timers throttle spf 10 100 5000
 passive-interface Serial4/0
 network xxxxxxxxxxxxxxxxx area 1
 network yyyyyyyyyyyyyyy area 0

 

And this is as far as it gets when ran:

000879: Apr 28 20:26:00.016 CDT: %HA_EM-6-LOG: UNPASSIVE: 11
000880: Apr 28 20:26:00.016 CDT: %HA_EM-6-LOG: UNPASSIVE: 21

 

I am now convinced it's an IOS issue, as it works in my lab (15 code), but not in prod (12 code).  But it really stinks for me, as I still need a solution to make these interfaces unpassive.  Any ideas are appreciated.

 

In action 21, puts the contents of $_cli_result to make sure you're getting what you expect.  Perhaps you have AAA command authorization, and you're getting back "Command not authorized" or something.

Ok, Joseph, you're on to something.  I am not getting the desired $_cli_result.  I experimented with 2 very simple scripts:

event manager applet UNPASSIVE
 event none
 action 10 cli command "enable"
 action 20 cli command "show running-config"
 action 30 puts "$_cli_result"
!

and it works.  Then I tried adding the CRON event:

event manager applet UNPASSIVE
 event event timer cron cron-entry "0-59 * * * *"
 action 10 cli command "enable"
 action 20 cli command "show running-config"
 action 30 puts "$_cli_result"
!

And nothing.  I even tried adding a user called, 'EEM':

username EEM priv 15

event manager session cli username "EEM"

 

Still nothing.  Why would this work with a manual event, but not a CRON event?

 

So what do you get back from running the command?  What does the running config look like?

When I run the script manually, I get the running-config 'put' on the screen.

When I run the script via CRON, nothing happens.

 

It's got to be 'privilege' related, because when I change the script's command to 'show ip int brief'...it works manually AND with CRON.  Like I said, I even created the user, 'EEM' with full privileges and it still doesn't work.  Here's the config:

username EEM privilege 15

event manager session cli username "EEM"

event manager applet UNPASSIVE
 event timer cron cron-entry "0-59 * * * *"
 action 10 cli command "enable"
 action 20 cli command "show running-config"
 action 30 puts "$_cli_result"
!

Getting Started

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:

Innovations in Cisco Full Stack Observability - A new webinar from Cisco