Home > Mobile >  How to switch Internet connections using Python in Ubuntu?
How to switch Internet connections using Python in Ubuntu?

Time:05-21

I have two internet connections via LAN Cable. I am from India, so they are not reliable. If one internet stops working, I want to automatically switch to another internet connection.

I wrote this script to check, If Internet is working or not:

try:
    request = requests.get("http://www.google.co.in", timeout=5)
except (requests.ConnectionError, requests.Timeout) as exception:
    try:
        request = requests.get("http://www.amazon.com", timeout=5)
    except (requests.ConnectionError, requests.Timeout) as exception:
        print("Internet not working, switching internet.....")

If there is no ping from google & amazon, I need to switch internet.

My connection name is "Wired connection 1" and "Wired connection 2".

Edit: If someone has a solution in any other language or using software, please let me know.

CodePudding user response:

It is a bit tricky, because there can be different network connection management interfaces on Ubuntu.

One of the most popular is NetworkManager.

  1. If your machine uses it, you can try getting and using NetworkManager python module.

It says that "You can use this interface to query NetworkManager about the overall state of the network and details of network devices like current IP addresses or DHCP options, and to configure, activate and deactivate network connections."

So it should work, provided your Ubuntu uses NetworkManager.

As I have never used it, I can't provide any technical details on how to set up connection switching.

  1. If it turns out that module doesn't work or your Ubuntu doesn't use NetworkManager, you can always switch connections by using shell commands from within Python to change the connection, using whichever commands you would use from shell via os.system() calls or subprocess module.

CodePudding user response:

I figured it out

import requests
import os 
import time
sudoPassword = 'pass'
pingFailStreak = 0

url1 = "http://www.google.co.in"
url2 = "http://www.amazon.com"
timeout = 5
def doesInternetWork(url,timeout):
    try:
        request = requests.get(url, timeout=timeout)
        return True
    except (requests.ConnectionError, requests.Timeout) as exception:
        return False

def connectInternet(netID):
    if netID==1:
        command = 'nmcli dev disconnect enp6s0'
        p = os.system('echo %s|sudo -S %s' % (sudoPassword, command))
        command = 'nmcli dev connect enp5s0'
        p = os.system('echo %s|sudo -S %s' % (sudoPassword, command))
    elif netID==2:
        command = 'nmcli dev disconnect enp5s0'
        p = os.system('echo %s|sudo -S %s' % (sudoPassword, command))
        command = 'nmcli dev connect enp6s0'
        p = os.system('echo %s|sudo -S %s' % (sudoPassword, command))

while True:
    if doesInternetWork(url1,timeout) == False and doesInternetWork(url2,timeout) == False:
        print("Internet not working")
        pingFailStreak  = 1
        if pingFailStreak > 2:
            print("Switching intetnet to 1")
            connectInternet(1)
            time.sleep(5)
            if doesInternetWork(url1,timeout) == False and doesInternetWork(url2,timeout) == False:
                print("Switching intetnet to 2")
                connectInternet(2)
                time.sleep(5)

    time.sleep(2)
  • Related