cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
273
Views
3
Helpful
8
Replies

eem regexp multiple variables

markrobinson
Level 1
Level 1

Hello, 

I need some regexp help in EEM

I'm trying to extract the IP and the Metric out of a show ip route statement

example of show ip route:

E    192.172.0.0 [200/3565] via 131.119.254.244, 0:02:22, Ethernet2

Would like to extract 192.172.0.0 to a variable as well as the metric 3565 into a different variable.

I have this statement that correctly pulls the IP

foreach line "_cli_result" "\n"

regexp "([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)" "$line" IP

puts "$IP"

What regex can i add to the statement to also pull the "3565" into a variable called "metric"

I tried so many combinations from searching the forums and I keep getting a null result so nothing is printed from the puts

Thank you

1 Accepted Solution

Accepted Solutions

 The first variable is the Matchvar followed by subvar that are captured by paren().  The first set of paren is the first sub var, second set of parent is second subvar, etc..

Try this policy.

 

event manager applet showiprouteMetric
 event none
 action 010 cli command "enable"
 action 020 cli command "term length 0"
 action 030 cli command "show ip route ospf"
 action 040 foreach line "$_cli_result" "\n"
 action 050  regexp "([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/[0-9]+ \[[0-9]+/([0-9]+)\]" "$line" match IP metric
 action 060  if $_regexp_result eq "1"
 action 070   puts "$IP and $metric"
 action 080  end
 action 090 end

 

View solution in original post

8 Replies 8

action 3.0 regexp "\\[[0-9]+/([0-9]+)\\]" "$_cli_result" metric <<- try this

MHM

Thanks for the super fast reply

action 3.0 regexp "\\[[0-9]+/([0-9]+)\\]" "$_cli_result" metric  This command returns both the distance and metric values

"[200/3565]"  

 I need to only get the "3565"

 

can I see how you config puts ?

MHM

Sure,  I apologize if there are any typos.  I do not have access to the router from any internet facing computer so have to hand type the configs.

**this applet returns the [AD/Metric] for all routes in the routing table.  including the brackets.  I can say that the AD will remain a constant number if that is easier to make an expression for.  [200/(metric)]


event manager applet showiprouteMetric
event none
action 1.1 cli command "enable"
action 1.2 cli command "term length 0"
action 1.3 cli command "show ip route ospf"
action 2.0 foreach line "$_cli_result" "\n"
action 2.0.1 regexp "\\[[0-9]+/([0-9]+)\\]" "$line" metric
action 2.0.2 if $_regexp_result eq "1"
action 2.0.3 puts "$metric"
action 2.1 end
action 2.2 end

event manager applet showiprouteMetric_Workaround_05
event none
action 1.1 cli command "enable"
action 1.2 cli command "term length 0"
action 1.3 cli command "show ip route ospf"
action 2.0 regexp "\\[[0-9]+/[0-9]+\\]" "$_cli_result" bracket
action 2.1 if $_regexp_result eq "1"
action 2.2 regexp "/([0-9]+)" "$bracket" metric
action 2.3 puts "Extracted OSPF metric: $metric"
action 2.4 end
action 3.0 if $_regexp_result ne "1"
action 3.1 puts "No metric matched"
action 3.2 end

after long  try I get this check it 
the result will be 


Extracted OSPF metric: /2

 

 The first variable is the Matchvar followed by subvar that are captured by paren().  The first set of paren is the first sub var, second set of parent is second subvar, etc..

Try this policy.

 

event manager applet showiprouteMetric
 event none
 action 010 cli command "enable"
 action 020 cli command "term length 0"
 action 030 cli command "show ip route ospf"
 action 040 foreach line "$_cli_result" "\n"
 action 050  regexp "([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/[0-9]+ \[[0-9]+/([0-9]+)\]" "$line" match IP metric
 action 060  if $_regexp_result eq "1"
 action 070   puts "$IP and $metric"
 action 080  end
 action 090 end

 

Thank you sir!

Your explanation of the parent and sub var is what I was missing.  I was able to change you regexp to also pull the subnet mask also as a test.

One question.  do you only use "match" in the regexp statement when matching more than one expression?

The match variable can be any variable name that you assign to the regexp.