D
D
dlinyj2020-05-18 23:41:06
linux
dlinyj, 2020-05-18 23:41:06

How to dynamically change the default gateway?

There are a lot of lte modems. It is necessary to alternately work with each modem (drive traffic without turning off the rest).

5ec2f07453d21800964275.jpeg

Each modem, when connected, creates an eth interface with an ip address of 192.168.8.100 and is a gateway 192.168.8.1. And configures all settings via dhcp. That is, we have a bunch of identical network interfaces with the same ip-addresses and the same default gateways.

Manually change ip-addresses on network interfaces according to the following scheme.

5ec2f133806c2911064851.jpeg

Next, I work as follows with modems:

for iface in network_list:
  os.system("sudo ip route flush all") #удаляем все маршруты
#добавляем маршрут через текущий интерфейс
  os.system("sudo route add default gw 192.168.8.1 " + iface)   
#добавляем DNS-сервр  
  os.system("sudo bash -c 'echo nameserver 192.168.8.1 > /etc/resolv.conf'")
#что-то делаем нужное нам
  SpeedTest() 
  SaveResult()


And I ran into a problem that this approach does not always work. For some reason, the network does not always rise. And most importantly, it would not work stably. And sometimes this approach works, sometimes it doesn't.
What am I doing wrong that I do not take into account, what else should be added or changed? I'll take any advice!

Z.Y. This is in a sense an extension of the question How to automatically test speed on multiple wan interfaces without losing the ssh connection?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
K
Karpion, 2020-05-19
@Karpion

Each modem, when connected, creates an eth interface with an ip address of 192.168.8.100 and is a gateway 192.168.8.1. And configures all settings via dhcp.
Perhaps this parameter can be changed somehow - configure the modem so that it makes other IP addresses on a USB connection (EVPOCHYA).
os.system("sudo bash -c 'echo nameserver 192.168.8.1 > /etc/resolv.conf'")
Why overwrite nameserver every time?
For some reason, the network does not always rise. And most importantly, it would not work stably. And sometimes this approach works, sometimes it doesn't.
If you do this remotely (via SSh), then the commands to change the settings should. in a package (command file); and the package must be executed separately from the terminal (nohup for the entire package. If not, then the execution of commands may be interrupted, especially if some message is output to the terminal.
I'll take any advice!
Raise several virtual machines. Forward your USB port to each. And test the speed separately for each virtual machine.
(I don't suffer from perversions - I enjoy them.)

V
Vladimir, 2020-05-20
@nero211

And if you don’t reinvent the wheel and use almost any Internet gateway (pfSense / X / Kerio) on which you can already set up a multivan and, depending on the need, switch between networks.

V
Victor Taran, 2020-05-20
@shambler81

Let's start from the beginning, why do you need this art, it seems to me that everything can be solved in a different way.
What and why are you doing. Why one interface? etc.
Perhaps a much more efficient solution will be proposed

A
Anton Teremshonok, 2020-05-28
@TerAnYu

Maybe this approach is right for you?
Isolate an application with an IP address from a VPN other...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question