Community discussions

MikroTik App
 
msteil
just joined
Topic Author
Posts: 19
Joined: Mon Nov 18, 2013 1:10 am

Route Checking before adding

Wed May 02, 2018 3:40 am

Hello,

I need to add a specific static ip route when a given condition exists in a script, adding the route is no problem, but checking and preventing multiple routes that are the same from being added to the routing table, is vexing me, and I am in need of some inspiration. I have searched the archives and really haven't come up with good answer to a question that has to have been asked and answered in the past. Can someone either point me towards the illusive thread, or post any examples checking for the presence of a specific route before attempting to add a route that is already in the table.

Thank you for your attention.
 
2frogs
Forum Veteran
Forum Veteran
Posts: 713
Joined: Fri Dec 03, 2010 1:38 am

Re: Route Checking before adding

Thu May 03, 2018 6:01 am

:if ([:len [/ip route find dst-address=0.0.0.0/0 and gateway=192.168.254.1]] >0) do={:log info "Route Exists";} else={/ip route add gateway=192.168.254.1 dst-address=0.0.0.0/0; :log info "Added Route";}
 
msteil
just joined
Topic Author
Posts: 19
Joined: Mon Nov 18, 2013 1:10 am

Re: Route Checking before adding

Tue May 08, 2018 12:43 am

Thanks, this is helpful somewhat. Only problem is I can't hard code the dst-address and gateway so I made the adaption

:if ([:len [/ip route find dst-address=$DIST and gateway=$DHCPGW]] >0) do={:put "Do something} else={:put "Do nothing"}

Is it possible to do the route find based on a value of a variable?
 
msteil
just joined
Topic Author
Posts: 19
Joined: Mon Nov 18, 2013 1:10 am

Re: Route Checking before adding

Tue May 08, 2018 8:18 pm

I finally got it to work, this post is so if someone else is looking for a route checking solution, and unable to find help in the archives, maybe this will help.

###
# Variables
###
: local INTERFACE "DHCP Interface Name"
: local DESTINATION "xxx.xxx.xxx.xxx/xx"
: local DISTANCE "XX"
: local COMMENT "Anything you want to add to the comment of the route you add"
: local DHCPGW [ /ip dhcp-client get [/ip dhcp-client find where interface=$INTERFACE] gateway ]
###
# IF there is no default gateway on dhcpclient interface or if interface is disabled, then error
:if ([:len $DHCPGW] = 0) do={
:put "No gateway on $INTERFACE interface"
}\
else={
:if ([:len [/ip route find dst-address=$DESTINATION and gateway=$DHCPGW]] =0) \
do={:put "No route"
:ip route add dst-address=$CLOUD gateway=$DHCPGW distance=$DISTANCE comment=$COMMENT check-gateway=ping;
:ip route add dst-address=$DESTINATION gateway=$DHCPGW distance=$DISTANCE comment=$COMMENT check-gateway=ping
}\
else={:put "Route Found"
}
}

Who is online

Users browsing this forum: No registered users and 31 guests