2

I have two internet connection from two different ISP. ADSL on eth0 and 4G on wlan0. Is there any way that if one ISP fails, the other take over automatically without a physical router and by Host OS (Ubuntu 18.04)?

Daniyal Javani
  • 247
  • 1
  • 5
  • 13
  • 2
    See [this](https://unix.stackexchange.com/questions/125114/using-wifi-port-as-redundant-link) – xenoid Jun 06 '18 at 09:33
  • 2
    @xenoid: I have serious doubts that bonding could _possibly_ work with two different upstream ISPs. – u1686_grawity Jun 06 '18 at 11:00
  • How are you going to detect if one "ISP fails"? Ping probes in regular intervals? Router information? Choose a method, implement it, write a script to update routes when the status changes. Sorry, no out-of-the-box way AFAIK. – dirkt Jun 06 '18 at 11:09
  • 1
    @grawity For plenty of uses with soft connections, it would (web surfing, mail..). Of course, a long HTTP transfer would get interrupted. – xenoid Jun 06 '18 at 11:29
  • What router? Typically this is not handled by the PC regardless of OS, but by the router... many high-end SOHO routers and most commercial grade routers can handle this smoothly and automatically as this is actually a common application. – acejavelin Jun 06 '18 at 14:34
  • [Load balancing with multiple ISP, two routers and firewall](https://networkengineering.stackexchange.com/q/19721/16289), [Multiple ISP link aggregation](https://superuser.com/q/848001/241386) – phuclv Jun 06 '18 at 16:39
  • @Tim_Stewart as even you noted in your solution, this requires telling the computer to operate as a router (pfsense), as phuclv notes, router link redundancy and load balancing are questions that have been asked and answered here before. – music2myear Jun 15 '18 at 16:13
  • What makes you say that? One of the suggested answers is a SU answer. – music2myear Jun 15 '18 at 16:34
  • @Tim_Stewart sure, any suggestion? I just added `without a physical router and by Host OS` – Daniyal Javani Jun 16 '18 at 12:23
  • 1
    @Tim_Stewart Also I've changed the script to exactly what I want: https://gist.github.com/Daniyal-Javani/6ea632dbbbb744f0e4f6dd0004b0351e – Daniyal Javani Jun 16 '18 at 13:36

1 Answers1

4

I have never personally tried to do this from the host OS. But after a back and forth, I decided to dig around on the internet to see if I could find what you are looking for.

I found an example script for gateway fail-over at gist.github.com created by the user "Apsu". you can download the bash script here.

You will have to modify this to your specific setup, add your interface names, gateway addresses & you should be good to go.

Script for the Host OS:

#!/bin/bash

# Set defaults if not provided by environment
CHECK_DELAY=${CHECK_DELAY:-5}
CHECK_IP=${CHECK_IP:-8.8.8.8}
PRIMARY_IF=${PRIMARY_IF:-eth0}
PRIMARY_GW=${PRIMARY_GW:-1.2.3.4}
BACKUP_IF=${BACKUP_IF:-eth1}
BACKUP_GW=${BACKUP_GW:-2.3.4.5}

# Compare arg with current default gateway interface for route to healthcheck IP
gateway_if() {
  [[ "$1" = "$(ip r g "$CHECK_IP" | sed -rn 's/^.*dev ([^ ]*).*$/\1/p')" ]]
}

# Cycle healthcheck continuously with specified delay
while sleep "$CHECK_DELAY"
do
  # If healthcheck succeeds from primary interface
  if ping -I "$PRIMARY_IF" -c1 "$CHECK_IP" &>/dev/null
  then
    # Are we using the backup?
    if gateway_if "$BACKUP_IF"
    then # Switch to primary
      ip r d default via "$BACKUP_GW" dev "$BACKUP_IF"
      ip r a default via "$PRIMARY_GW" dev "$PRIMARY_IF"
    fi
  else
    # Are we using the primary?
    if gateway_if "$PRIMARY_IF"
    then # Switch to backup
      ip r d default via "$PRIMARY_GW" dev "$PRIMARY_IF"
      ip r a default via "$BACKUP_GW" dev "$BACKUP_IF"
    fi
  fi
done

With network hardware:

There are considerable advantages to using a router for this function, as opposed to just fail-over from the host OS. What immediately comes to mind is using both connections bandwidth simultaneously. (Fully utilizing both ISP connections)

You can use PFsense / OpenSense as a dual WAN router. (Most old PC hardware will work.) I believe DD-WRT supported routers are also capable of WAN fail-over with some tweaking. Or you could go with an out of the box solution, like Sophos or Netgear prosafe, I am sure there are others, But most out of the box solutions are more expensive than home users are willing to pay for. A dual wan router setup allows you to increase your internet bandwidth. You can load balance traffic for your specific needs. You can get internet connection redundancy and fail-over.

Regards,

Tim_Stewart
  • 5,884
  • 3
  • 11
  • 33