1

I have two network connections - wifi and ethernet. How to choose a specific network to use while using command line eg. ping google.com.

PS: I mentioned ping as an example. I may be running a python script or curl and it should use same interface for all these.

Bytemare
  • 21
  • 1
  • Did you try reading the **man** pages for the commands you want to use (e.g. `man ping`)? – sawdust Aug 07 '17 at 04:47
  • I think the downvote was harsh - this is a legitimate question, albeit not one that is easy to answer. – davidgo Aug 07 '17 at 05:10
  • https://superuser.com/questions/241178/how-to-use-different-network-interfaces-for-different-processes this is a way to expose only certain interfaces to your process. Also this: https://unix.stackexchange.com/questions/210982/bind-unix-program-to-specific-network-interface – wvxvw Aug 07 '17 at 07:56
  • Possible duplicate of [How to use different network interfaces for different processes?](https://superuser.com/questions/241178/how-to-use-different-network-interfaces-for-different-processes) – Dmitry Grigoryev Aug 07 '17 at 13:01
  • @davidgo - The downvote is justified because the question "does not show any research effort". The **man** page for `ping` (the command specifically mention in the question) has a switch to specify an interface (aka *"network connection"*), which answers the question for that shell command. – sawdust Aug 08 '17 at 19:48

1 Answers1

0

It may not be possible to do this without disabling one of the connections, unless you have a special setup.

Generally speaking Linux routes are controlled by the route table, and traffic will be sent out the route with the lowest metric - you can see what this is by using the command "ip route" or "route". In this cas the easiest solution would be to remove the route you don't want (using ip route or route) or tear down the interface you don't want (using ifconfig or ip addr).

If you have more complex routing, its possible your system is using source routing, and then routes packets based on the source or target IP through different tables. In this case the easiest way to do it would be to tell the command (if supported) to send packets with the given source IP address or interface (use man COMMAND to see how this would work). In ping it would be ping -I INTERFACE, in mtr its mtr -a SOURCE.IP.ADDRESS

davidgo
  • 68,623
  • 13
  • 106
  • 163