For a project I need to come up with a failover script (2 LAN paths, 1 3G path). No prior knowledge of the LAN the router is sitting in is a requirement. So in order to ie. try if a LAN segment came back while running on 3G backup, I wanted to ping a server using a specific path (preventing the packets to go out the 3G door). To this end, finding out what the local gateway on the LAN is would be helpfull.
Tried this :
:local WANGateway [/ip arp get [/ip arp find interface=“ether1-gateway”] address];
:if ([:len $WANGateway] > 0) do={
:put “Found WAN gateway : $WANGateway”
};
Works a charm, except for when you pull the ethernet plug. Then the above crashes the script ungracefully with a ‘no such item’ message. It would be nice if ie. an empty string was returned …
Other methods to find the local gateway failed as well. Given no prior knowledge of the LAN, I could not add a comment and do a find on that later on.
/ip route find dst-address=“0.0.0.0/0” in version 4.0RC1 still does not produce any results. Any thoughts on how to get the gateway IP address would be appreciated !
It crashes when [/ip arp find interface=“ether1-gateway”] returns an empty string.
You could re-write it like this:
:local WANGatewayArp [/ip arp find interface="ether1-gateway"]
:local WANGateway
:if ([:len $WANGatewayArp] > 0 ) do={
:set WANGateway [/ip arp get $WANGatewayArp address]
:if ([:len $WANGateway] > 0 ) do={
:put "Found WAN gateway : $WANGateway"
}
}
Another way you could find the gateway (using the routing table) would be:
:local WANGatewayIf "ether1-gateway"
:local WANGateway
/ip route
:foreach r in=[find] do={
:if ( [get $r interface] = $WANGatewayIf && [get $r dst-address] = "0.0.0.0/0") do={
:set WANGateway [get $r gateway]
}
}
:if ([:len $WANGateway] > 0 ) do={
:put "Found WAN gateway : $WANGateway"
}
The reason I do a foreach + [find] is because I’ve have 100% success with it. As you mentioned, /ip route find dst-address=“0.0.0.0/0” didn’t work for you; I’ve had the same problem trying to narrow search down in one command. It seems intermittent between releases, and thus I stick with just a generic [find] every time, then search through the results returned.
Good solution, thanks Doug. Can’t wait for the scripting engine to be improved further though, get rid of the ungracefull exit on a ‘no such item’, and include functions / subroutines. In the mean while, since my router gets it’s address from a DHCP server on the LAN, I went a slightly different route :
:put "Trying to determine WAN gateway address"
/interface ethernet monitor once 0 do={
:set stat $status
};
:if ($stat = "link-ok") do={
:set WANGateway [/ip dhcp-client get [/ip dhcp-client find interface="ether1-gateway"] gateway]
:if ([:typeof $WANGateway] = "ip") do={
:put "WAN gateway set to $WANGateway"
} else= {
:put "Cable connected, but no valid WAN gateway found"
};
};
Jan
I’m still waiting for the Lua scripting language to appear. It was in beta v4, but they took it out in the final v4 stable release. It’s supposed to let you create functions and whatnot.