cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
2605
Views
5
Helpful
8
Replies

EEM - How to get ip address into variable

fsebera
Level 4
Level 4

Hi,

I have asked my EEM questions in other groups but have received no responses so perhaps I was in the wrong groups.

I'm trying to get the following IP addresses into different variables.

 

The show crypto session | i port cli command provides this result:

Peer: 10.0.3.1 port 500

Peer: 10.0.2.1 port 500

Peer: 20.0.3.1 port 500

Peer: 20.0.2.1 port 500

 

I can get this complete list of addresses into a single variable and I can get 10.0.3.1 into one variable and 20.0.3.1 into a different variable but cannot figure out how to get each address into 4 different variables.

 

Using regexp:

regexp "([1]+)([0-9.]+)" "$_cli_result" IPADDR1

regexp "([2]+)([0-9.]+)" "$_cli_result" IPADDR2

 

The result:

ADDR1 = 10.0.3.1

ADDR2 = 20.0.3.1

 

Is there a way to make a single EEM regexp line read and capture for a second or more matches?  The problem with my EEM script is the number of addresses or the subnet addresses may not always be know before hand and obviously is not dynamic.

Also, I am not restricted to using regexp as I have tried other features with similar mixed results.

Note, sometimes the addresses will began with 172 or 192 or some other valid range.

 

Any help would be appreciated

Thank you

Frank

 

 

8 Replies 8

Hello,

 

--> Is there a way to make a single EEM regexp line read and capture for a second or more matches? The problem with my EEM script is the number of addresses or the subnet addresses may not always be know before hand and obviously is not dynamic.

 

I am not clear on what result you want the regexp to have. What do you want to match ? Which IP address(es) ? Give an example. Do you want to match ANY address ?

 

 

Hey Georg,

 

Yes I would like to match all addresses that appear in the cli command result: show crypto session | i port

I would like have each address assigned to a different variable.

The final solution is not locked into using EEM regexp, it just my 4th attempt in trying to make this work.

 

EXAMPLE

R1# show crypto session | i port

Peer: 10.0.3.1 port 500

Peer: 10.0.2.1 port 500

Peer: 20.0.3.1 port 500

Peer: 20.0.2.1 port 500

Peer: 192.168.55.129 port 500

Peer: 172.16.199.205 port 500

Peer: 192.168.43.86 port 500

Peer: 172.31.49.104 port 500

 

 

assign 10.0.3.1 to VAR1

assign 10.0.2.1 to VAR2

assign 20.0.3.1 to VAR3

assign 20.0.2.1 to VAR4

assign 192.168.55.129 to VAR5

assign 172.16.199.205 to VAR6

assign 192.168.43.86 to VAR7

assign 172.31.49.104 to VAR8

Tks

Frank

Hello,

 

I see. So the number of peers can be any number, and the IP addresses could be any as well.

 

I'll see what I can find, will get back with you.

Hello, 

 

the below EEM script is what I have come up with. I have set the event to 'none' so you have to run it manually (event manager run IP_ADDR). You can of course also schedule it to run at certain intervals.

 

event manager applet IP_ADDR
event none
action 010 cli command "enable"
action 011 set var 0
action 012 cli command "show crypto session | i port"
action 013 foreach line "$_cli_result"
action 014 regexp "([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)" "$line" ip
action 015 if $_regexp_result eq "1"
action 016 increment var
action 017 puts "ADDR$var $ip"
action 018 end
action 019 end

 

Using your example, this should be the output:

 

ADDR1 10.0.3.1
ADDR2 10.0.2.1
ADDR3 20.0.3.1
ADDR4 20.0.2.1
ADDR5 192.168.55.129
ADDR6 172.16.199.205
ADDR7 192.168.43.86
ADDR8 172.31.49.104

Hey Georg

 

Forgive me as I clearly did not specify in my original request that I also needed to be able to use (call) the different variables elsewhere in my script.

 

ABSOLUTLY your solution works as requested!!!!!!! VERY NICE.

Perhaps just a little tweak

 

THANK YOU

Frank

 

Hello,

 

you need to be able to call every line as a variable...I see. Working on it...will get back with you.

Hello,

 

I am not making any progress unfortunately. I am not sure EEM can do this at all, I don't seem to be able to call a variable from another variable.

 

I saw that you also posted in 'Network Management', does the TCL script Daniel Frey suggests work ?

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:

Review Cisco Networking products for a $25 gift card