Page 1 of 1

Script to fetch Gateway IP

Posted: Wed Sep 27, 2017 12:13 pm
by stamatis123
Hello everybody!

I'm in desperate need of a scripting God to help me with a script. I have six VDSL lines bridged to a RB1100AHx2 which does the PPPoE connections to the ISP (pppoe1, pppoe2, pppoe3, etc). I use PCC load balancing but my gateway IPs are dynamic and change whenever there is a disconnection and this can happen at any given time (due to power failures, reboots, ISP disconnects etc.). So I'm need of a script that will fetch the gateway ip of each of the connections and then apply the appropriate route in the /ip route list (XXX.XXX.XXX.XXX%pppoe1, etc). If anyone already has a similar script that he can share or can at least point me at the right direction, would be very much appreciated!

Re: Script to fetch Gateway IP

Posted: Sat Oct 07, 2017 4:10 pm
by stamatis123
Anyone?

I've been trying to write the script myself but I ran into some problems
First this is the script I have
____________________________________________________________________________________________
:global WAN1GatewayIP
:global WAN1LastGatewayIP
#Grab current Gateway IP on WAN1
:global WAN1GatewayIP [ /ip address get [/ip address find interface="pppoe-wan1" ] network ]
:if ([:typeof $WAN1LastGatewayIP ] = nil ) do={ :global WAN1LastGatewayIP "0" }
#Check if Gateway IP has changed and apply rules
:if ($WAN1GatewayIP != $WAN1LastGatewayIP) do={
/ip route
remove [/ip route find comment=RuleWAN1-1]
remove [/ip route find comment=RuleWAN1-2]
add check-gateway=ping comment=RuleWAN1-1 distance=1 gateway=$WAN1GatewayIP%pppoe-wan1 routing-mark=to_WAN1
add check-gateway=ping comment=RuleWAN1-2 distance=1 gateway=$WAN1GatewayIP%pppoe-wan1
:global WAN1LastGatewayIP $WAN1GatewayIP
}
____________________________________________________________________________________________________________________
The idea is that I get the network ip of the interface, compare it with the previous value and if it is different then run the rest of the script in which I remove two rules (which I find having them commented) add the new ones and at the end renew the value of the LastGatewayIP so it has the latest value.

The thing is that if I run the first part of the script (#Grab current Gateway IP on WAN1) everything seems to be ok (or at least I think so, I get the variables and their respective values in the Environment tab). I added the second line (:if ([:typeof $WAN1LastGatewayIP ] = nil ) do={ :global WAN1LastGatewayIP "0" }) to make sure I get a value when I run the script for the fisrt time (remember I am not script savvy!)

When I run the whole thing (including the second part (#Check if Gateway IP has changed and apply rules)) nothing seems to happen, not even the values of the first part appear so I suppose the script breaks somewhere but I don't know where.

Notes: pppoe-WAN1 is the name of my interface and RuleWAN1-1 and RuleWAN1-2 are the comments I added on the rules I want to be able to find

Any help would be greatly appreciated :)

Re: Script to fetch Gateway IP

Posted: Wed Oct 11, 2017 11:25 am
by aacable
If pppoe-outX [name of pppoe dialer] is being dialed from the RB itself, then why you are trying to hardcode the gateway IP,
Just create Static Route with 'ppoe-outX' [name of pppoe dialer] as gateway.
/ip route
add check-gateway=ping comment="DSL1 Default ROUTE" distance=1 dst-address=0.0.0.0/0 gateway=pppoe-out1 routing-mark=wan1_users

Re: Script to fetch Gateway IP

Posted: Sun Oct 15, 2017 10:26 am
by stamatis123
Thanks for the answer,

The RB is dialing the pppoe, all ok till that point but load balancing seems to have problems. I've read (correct me if I'm mistaking) that if you want to do load balancing through the same gateway you have to have the routes' gateways in the form of XXX.XXX.XXX.XXX%pppoe-wan1, XXX.XXX.XXX.XXX%pppoe-wan2 etc., where XXX.XXX.XXX.XXX is the gateway IP of each of the pppoe connections. My problem is that XXX.XXX.XXX.XXX is not static but changes from time to time (eg reboot) and not all connections share the same gateway each time (among 6 connections I might have 2 to 4 different gateway IPs so some connections always have the same gateway IP). If for example wan1, wan2 and wan5 share one gateway, wan3 and wan6 share another and wan5 has yet another gateway the routes should be in that form
/ip route
add check-gateway=ping distance=1 gateway=XXX.XXX.XXX.XXX%pppoe-wan1 routing-mark=to_WAN1
add check-gateway=ping distance=1 gateway=XXX.XXX.XXX.XXX%pppoe-wan2 routing-mark=to_WAN2
add check-gateway=ping distance=1 gateway=YYY.YYY.YYY.YYY%pppoe-wan3 routing-mark=to_WAN3
add check-gateway=ping distance=1 gateway=ZZZ.ZZZ.ZZZ.ZZZ%pppoe-wan4 routing-mark=to_WAN4
add check-gateway=ping distance=1 gateway=XXX.XXX.XXX.XXX%pppoe-wan5 routing-mark=to_WAN5
add check-gateway=ping distance=1 gateway=YYY.YYY.YYY.YYY%pppoe-wan6 routing-mark=to_WAN6
where XXX.XXX.XXX.XXX, YYY.YYY.YYY.YYY and ZZZ.ZZZ.ZZZ.ZZZ are the gateway IPs of each connection
So what I'm trying to do is to automate the procedure of adding the gateway IP of each of the connections into the route's gateway.

Thanks again!

Re: Script to fetch Gateway IP

Posted: Sun Oct 15, 2017 10:35 am
by stamatis123
Just realized who you are,

much respect, I've read and keep looking for solutions to many of my problems in your blog and scripts! :)

Re: Script to fetch Gateway IP

Posted: Thu Nov 30, 2017 6:11 am
by ADahi
Sure :wink:
to set the dynamic gateway for each pppoe-client, you can use this script
# Get the dynamic gateway for each pppoe-client
:global pppoe1-gateway [/ip route get number=[find gateway=pppoe1 && distance=0 && scope=10] dst-address];
:global pppoe2-gateway [/ip route get number=[find gateway=pppoe2 && distance=0 && scope=10] dst-address];
:global pppoe3-gateway [/ip route get number=[find gateway=pppoe3 && distance=0 && scope=10] dst-address];
:global pppoe4-gateway [/ip route get number=[find gateway=pppoe4 && distance=0 && scope=10] dst-address];
:global pppoe5-gateway [/ip route get number=[find gateway=pppoe5 && distance=0 && scope=10] dst-address];
:global pppoe6-gateway [/ip route get number=[find gateway=pppoe6 && distance=0 && scope=10] dst-address];

# Route each Traffic
/ip route
add check-gateway=ping distance=1 gateway=$pppoe1-gateway routing-mark="to_WAN1" 
add check-gateway=ping distance=1 gateway=$pppoe2-gateway routing-mark="to_WAN2" 
add check-gateway=ping distance=1 gateway=$pppoe3-gateway routing-mark="to_WAN3" 
add check-gateway=ping distance=1 gateway=$pppoe4-gateway routing-mark="to_WAN4" 
add check-gateway=ping distance=1 gateway=$pppoe5-gateway routing-mark="to_WAN5" 
add check-gateway=ping distance=1 gateway=$pppoe6-gateway routing-mark="to_WAN6" 
add distance=2 gateway=$pppoe1-gateway 
add distance=3 gateway=$pppoe2-gateway 
add distance=4 gateway=$pppoe3-gateway 
add distance=5 gateway=$pppoe4-gateway 
add distance=6 gateway=$pppoe5-gateway 
add distance=7 gateway=$pppoe6-gateway