cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
2637
Views
1
Helpful
7
Replies

replacing . with \. in string

leogal777
Level 1
Level 1

Hello guys

I am trying to make this script work

there is environmental variable called removeip and it has several IP addresses in it...e.g. 

event manager environment removeip 172.17.1.0 172.17.100.0 172.31.8.0

I am using these addresses one by one in another script in regexp and therefore I need

to replace dots with \. So it will match correctly.

event manager applet rd
event none sync no
action 010 set string $removeip
action 020 puts $string
action 030 string first "." $string
action 040 set index $_string_result
action 050 puts $index
action 060 string replace "$string" $index $index "\."
action 070 puts $string

but action 060 wont let me use $index, it only accepts numbers - hence it is useless in this form for me.

Can you please point a way around it in applet? 

Thank you

Leo

1 Accepted Solution

Accepted Solutions

Joe Clarke
Cisco Employee
Cisco Employee

Some things are better suited for Tcl.  It would be good to see your overall solution/problem as there might be a better way to solve for the system.

However, you could use regexp to rebuild your IPs the way you want.

foreach ip $removeip

 regexp "^([0-9]+)\.([0-9]+)\.([0-9\.]+)" $ip match o1 o2 rest

 regexp "^([0-9]+)\.([0-9]+)" $rest match o3 o4

 set escip "$o1\.$o2\.$o3\.$o4"

 ! Do something with $escip here

View solution in original post

7 Replies 7

Philip D'Ath
VIP Alumni
VIP Alumni

I'm not that familiar with EEM, but if this was perl, this regex would replace . with \.

s/\./\\\./g

Can you execute such a regex?

Hello p.dath

Thanks for the reaction.

There would be greater flexibility with TCL script and there might be possibility to run something like you proposed in TCL, but since I am creating this in Cisco CLI event manager, this is not possible.

Regards

Leo

Joe Clarke
Cisco Employee
Cisco Employee

Some things are better suited for Tcl.  It would be good to see your overall solution/problem as there might be a better way to solve for the system.

However, you could use regexp to rebuild your IPs the way you want.

foreach ip $removeip

 regexp "^([0-9]+)\.([0-9]+)\.([0-9\.]+)" $ip match o1 o2 rest

 regexp "^([0-9]+)\.([0-9]+)" $rest match o3 o4

 set escip "$o1\.$o2\.$o3\.$o4"

 ! Do something with $escip here

Joe, 

any special reason, why you use \. in ([0-9\.]+)

[0-9.] could be enough.

dot in brackets matches just the dot

Thanks

Leo

Habit more than anything.

Hello Joe

When I had seen all your contributions to this supportforum I was hoping you would respond to this one.

Thank you for the snippet, it helped me to finalize it.

And just fyi, since you asked, I am creating a dumb script for adding static routes, couple of them.

And in the beginning of each of them I am calling another script that will remove static routes for

prefixes being added so I would not get into situation where there will be a static route and I will not change it, but add another one and it will end up in some kind of load shareing.

puts and syslog will be replaced by cli

no event manager applet rmstatic
event manager applet rmstatic
event none sync no
action 011 puts "$removeip"
action 020 foreach ip "$removeip"
action 021 regexp "^([0-9]+)\.([0-9]+)\.([0-9\.]+)" $ip match o1 o2 rest
action 022 regexp "^([0-9]+)\.([0-9]+)" $rest match o3 o4
action 023 set escip "$o1\.$o2\.$o3\.$o4"
action 025 cli command "enable"
action 026 cli command "show run | i ip route $escip"
action 040 foreach line "$_cli_result" "\n"
action 041 regexp "^ip route .*$" "$line" match
action 042 if $_regexp_result eq "0"
action 043 break
action 044 end
action 045 puts "no $match"
action 046 end
action 060 end

event manager applet e1direct
description add route to 1.1.1.1 via XY (and before that remove any statics for prefixes)
event none
action 010 cli command "enable"
action 011 cli command "conf t"
action 012 cli command "event manager environment removeip 1.1.1.1 2.2.2.2"
action 013 cli command "end"
action 020 policy rmstatic
action 029 wait 10
action 030 syslog msg "ip route 1.1.1.1 255.255.255.255 172.25.3.3"
action 031 syslog msg "ip route 2.2.2.2 255.255.255.255 172.25.3.3"

Have you considered using tracked routes?  A tracked route looks like:

ip route 0.0.0.0 0.0.0.0 11.b.c.d track 10

Tracked routes get added/removed based on various criteria.  This article shows using IP SLA for adding and removing routes.

https://supportforums.cisco.com/document/30296/using-ipsla-change-routing