0

I have two network interfaces, for ethernet and wifi. I would like to tell my computer to preferrably use the wifi connection (whenever it is connected) to access certain domains, and use the ethernet cable connection for everything else.

Is there any way I can set this up fiddling with /etc/hosts, resolv.conf or whatever?

4
  • Does this answer your question? askubuntu.com/a/4890
    – Dev
    Commented Jul 23, 2021 at 4:13
  • 1
    Does this answer your question? bind software to different network interfaces
    – Dev
    Commented Jul 23, 2021 at 4:14
  • It's a similar question, posing a slightly different problem, but the answer does seem instead to solve almost the same problem I had (but routing a sub-network, instead of just a few IP addresses). Meanwhile I have answered my own question. The details in my solution look slightly different, so I'm not sure the other answer would have worked exactly for me. Maybe some details also differ due to the other answer being 11 years old. (My environment is Linux Mint 20.1). So the answer is "maybe".
    – trollkotze
    Commented Jul 26, 2021 at 14:29
  • 1
    I didn't know SE added that second comment for me. It's actually funny that they both look identical
    – Dev
    Commented Jul 26, 2021 at 18:51

1 Answer 1

0

Okay, it seems I've found the solution.

Check what routes are set up:

$ ìp route show

which in my example gives this result:

default via 192.168.0.1 dev enp0s31f6 proto dhcp metric 100 
default via 192.168.200.68 dev wlp4s0 proto dhcp metric 600 
169.254.0.0/16 dev enp0s31f6 scope link metric 1000 
192.168.0.0/24 dev enp0s31f6 proto kernel scope link src 192.168.0.151 metric 100 
192.168.200.0/24 dev wlp4s0 proto kernel scope link src 192.168.200.78 metric 600 

The first two lines here show the default gateways of my ethernet (enp0s31f6) and wifi (wlp4s0) respectively.

Now to find the IPs of the domains where I always get banned, I use the dig command as follows:

$ dig +short a discord.com
162.159.137.232
162.159.135.232
162.159.128.233
162.159.136.232
162.159.138.232
$ dig +short a discord.gg
162.159.136.234
162.159.135.234
162.159.133.234
162.159.134.234
162.159.130.234

Then add a route for each of these destination IPs over the Wifi interface (using the Wifi interface's default gateway that we got from the first command):

sudo route add -host 162.159.137.232 gw 192.168.200.68

etc.

To do this all automatically whenever the wifi connects, if you're using NetworkManager, you can add a script to the /etc/NetworkManager/dispatcher.d directory. For example:

sudo nano /etc/NetworkManager/dispatcher.d/10-bloody-routes

The 10- here at the start of the file name denotes the priority. Scripts in the dispatcher.d directory are run in lexicographical order on network events (interface connected, disconnected, etc.)

I enterd this bash script here to automate the above-mentioned process:

#!/bin/bash

if [ "$1" == "wlp4s0" ] && [ "$2" == "up" ]; then
  gateway=`ip route | awk '/default/ { print $3 " " $5 }' | awk '/wlp4s0/ { print $1 }'`;
  echo "What's up, $gateway: "`date` >> /home/trollkotze/smackmybitch.up;
  echo discord.com >> /home/trollkotze/smackmybitch.up;
  for x in `dig +short a discord.com`; do
    echo route add -host $x gw $gateway >> /home/trollkotze/smackmybitch.up;
    route add -host $x gw $gateway >> /home/trollkotze/smackmybitch.up;
  done;
  echo discord.gg >> /home/trollkotze/smackmybitch.up;
  for x in `dig +short a discord.gg`; do
    echo route add -host $x gw $gateway >> /home/trollkotze/smackmybitch.up;
    route add -host $x gw $gateway >> /home/trollkotze/smackmybitch.up;
  done;
fi;

As you can see from the script, the first and second argument are the interface name (in my case I'm looking for wlp4s0) and the event (I'm looking for the up event when the interface goes online). I added some logging to a file in my home directory here to verify that all is working.

Whenever I connect my wifi now this script is run and adds the appropriate routes to Discord over the wifi's default gateway.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .