How can I add a static route only if a host is reachable via a network interface?

1

In Raspbian (Debian) on the RaspberryPi, I've got two interfaces, eth0 and wlan0, and I'm using eth0 (a cellular modem) as the primary connection, with only some heavy traffic going out through wlan0. To that end, I'm using the ip route command.
Eg: (Routing Google's stuff through wlan0)

ip route add 74.125.228.0/24 dev wlan0

But, unfortunately, the power in our building occasionally goes out, rendering wlan0 useless.

How can I keep this static connection routed only when the host (eg. Google.com) is reachable via wlan0 (ie. wireless), but switch to eth0 (ie. cellular) when the power goes out (eg. Hurricane Sandy), and the wireless router is useless?

tamsanh

Posted 2013-02-19T03:32:16.857

Reputation: 13

Write a script that pings the first hop (the AP) IP address. If it's not available, reconfigure so that the wireless is disabled. Note, you can either then write a script that attempts to reconnect every 30 mins by turning the port on and attempting to ping the AP, or you have to reset it by hand. – Everett – 2013-02-19T03:37:23.050

There are routing protocols that do this for you. You just need to have OSPF or BGP enabled (instead of using a static route). – Everett – 2013-02-19T03:38:00.817

@Everett That first suggestion sounds promising. Could you point me towards some material I could use as examples for that kind of scripting? As for the second suggestion, while it is true that those routing protocols could handle it, I'm worried that they may forego the use of the cellular eth0 all together (by definition, that's what OSPF would want to do, I feel). – tamsanh – 2013-02-19T04:41:23.227

@Everett, it seems like going the script route will be the best option. Probably I'll set up something on a cron job which checks if it can ping a certain domain via the wlan0. If it can't ping it, then it'll just reroute everything. – tamsanh – 2013-02-20T22:06:58.073

Answers

2

Does the command support an argument called metric - if two routes are all equal except for the metric then the lower metric is used Normally typing in ip route add 74.125.228.0/24 dev wlan0 will add it into the table with metric =1 ie a manually added route is always used first But you should be able to adda second route to your less desirable card with metric =2

Ross

Posted 2013-02-19T03:32:16.857

Reputation: 1 096

I think that's probably the answer I'm looking for, and indeed it is available in the route command. However, according to the manpage, it says it's not used by recent kernels. Once I get the opportunity, I will test it, and report back. – tamsanh – 2013-02-19T04:50:24.090

The metric command is still available in recent kernels. (The difficulty you might have is if, when the power goes out, the WLAN connection does not loose its route). BTW, if you can't use a metric, you may be able to hack a similar solutions with 2 routes - 0.0.0.0 netmask 127.0.0.0 and 128.0.0.0 netmask 127.0.0.0 on the LAN interface. Because these are more specific routes they will be prefered if they are available - of-course, the problem is making them unavailable when WIFI goes down, and automatically adding them when WIFI comes back. – davidgo – 2013-02-19T05:57:14.207

@davidgo, you're absolutely correct with that wifi problem. Ross's metric solution was half correct, but (as you said) the wlan0 is still technically a route. From what my tests have shown, it isn't the full solution. – tamsanh – 2013-02-20T22:04:50.533

So, for now, while the proposed answer doesn't completely answer the question, it's the best one available, and certainly did help with most of the problems. – tamsanh – 2013-02-21T16:06:17.893