Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a linux (debian) router with two internet connections (A) and (B).

(A) is preferred, (B) is fallback.

I want to monitor the internet connection (and not only the availability of the gateways!) and change the default route appropriately.

  1. If (A) is not providing internet, switch to (B)
  2. If (A) is providing internet again, switch back to (A).

Only problem I have is in case (2). My routing table points towards a working internet so I cannot easily detect whether internet is working over link (A) again.

I am search for a ping or traceroute (or other diagnosis-tool) which can select the next-hop explicitly.

  • ping -r looks promising, but can only ping a host on the lan. (It only has to write another destination address in the packet, damnit!)
  • traceroute -g gateway looks even more promising and nearly does what I want - but sets source routing options which my next-hops deny. (Not within my administrative boundary...)

I just want a $ping, that can:

  • select a source interface (and address)
  • select a next-hop on that interface
  • ping any arbitrary ip address

I could do evil trickery with policy-based routing but that would have production impact for all users. I would like to see a side-effect-free solution....

share|improve this question
up vote 1 down vote accepted

Are the upstream gateways on the same Ethernet segment(s) as your router? If so nping --dest-mac … might work. (Nping is one of the many tools that come with nmap so it may already be on your system.)

share|improve this answer
Perfect! Has a --interface`` option which in combination with --dest-mac` seems to be what I need! nping is contained in the debian wheezy nmap package. – Michuelnik Sep 15 '12 at 7:30

How about this?



route add -host $IP gw $GW
ping $IP
route del -host $IP gw $GW
share|improve this answer
+1 a Simple route for the external host your are testing against should be easy, so long as that external address doesn't need to be accessed for anything other then testing. Though I do wonder why you would keep adding/removing the host route. I am sure you could find an address for some piece of equipment at the ISP, that you would only want to every access across the link from from that ISP. – Zoredache Sep 13 '12 at 20:34
+1, like KISS solutions! But - should be a little more robust and some work on an elaborate script is acceptable. Your idea was the kind of solution I had in mind when I was talking about policy-based routing. Would have done your solution but a bit more narrow, just icmp to the mentioned host. But: want to test more than one host, something really high available. Would have to do dns-resolution and so this all is growing. – Michuelnik Sep 13 '12 at 20:54
+1. I do basically the same thing w/ Cisco routers. Works great. – petrus Sep 15 '12 at 20:49

You can try using iptables with the Route Patch to specify the gateway to use on your ICMP packets sourcing from that (A) interface.

Supposing your (A) interface is with a default gw of

iptables -A PREROUTING -t mangle -p icmp -s -j ROUTE --gw

Then ping -I x.x.x.x

share|improve this answer
Yeah, think since ping-with-nexthop-selection seems from another universe I'll take this solution. Small conceptual changes here, though: assign 2 loopback IPs, explicitly use them with ping and force them via ip rule from to the one or other gateway. – Michuelnik Sep 14 '12 at 14:51
That sounds like a great plan. Good luck! – resmon6 Sep 14 '12 at 15:14

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.