PC with onboard linux has several network interfaces, e.g. two ethernet cards and one wifi adapter (numbers and types may vary). Each of that interfaces could be connected to a network (and could be offline) and the network could be connected to internet(superuser.com site is reachable), and could be connected to local-only network.

I want to write a bash script to get (local) IP address and name of adapter that can be used to reach internet. If several are connected - it should be the one that would be used by browser that is "asked" to load superuser.com web pages. If no connected script should echo "internet unavailble"

How to write a check condition for such script? How could i even iterate all networking-capable devices under /dev/* in a system?

  • 1
    @KamilMaciorowski I think that you misread question. I asked for iteration method. I dont need routes and schemes, i want to know.. a wildcard for devices. i cannot put there /dev/eth* because interface could be named enp3s0 for example, or it could be wifi adapter or usb network adapter. – xakepp35 Oct 17 '18 at 9:49
  • Each paragraph in your question asks for a completely different thing. "How do I get the default interface name?" "Wait, no, I don't want the default interface, I want all the interfaces"... The (deleted) post did exactly what your question's 2nd paragraph requires. – user1686 Oct 17 '18 at 10:33
  • Is your basic question that you want a command that you can run locally that will inform you which interface will be routing traffic to the Internet? – Hogstrom Oct 17 '18 at 11:36
  • @xakepp35 a browser doesn't decide which network interface to use, the way packets go out is decided by the routing table – AnonymousLurker Oct 17 '18 at 13:15
  • @AnonymousLurker Sure thing. I wrote so for simplicity of wording. If you dont like "browser" i can replace it with a "ping" command or whatever. – xakepp35 Oct 17 '18 at 14:14

Maybe you don't need to iterate. In my Debian 9 (IPv4 only, I'm not sure how relevant it is) I can use this:

ip route get "$(host superuser.com | awk 'NR==1 {print $NF}')" \
| grep -o "dev .* src [0-9]*\.[0-9]*\.[0-9]*\.[0-9]*"

where host (from bind9-host package) is to convert superuser.com to its IP address. To handle errors etc. you need to deconstruct the pipe and build a script with some logic around its parts.

Example output:

dev wlan0  src

To iterate anyway, peek into /sys/class/net:

cd /sys/class/net
for interface in *; do
    printf "Doing something with %s\n" "$interface"

These devices are not under /dev though, at least in none of my systems.

  • More better , adding awk '{print $2, $4}' – GAD3R Oct 17 '18 at 10:42
  • Thank you, i think, i need first option. I want to assign some "device id" to a computer, based on HEX digits of its local IP. It has to be IP of its adapter, that would be used to access the Internet. Machine may have several adapters, even of different types, so i have to choose correct one. Also i have to care and warn local administrator if no adapters are capable of reaching internet during script run. – xakepp35 Oct 17 '18 at 12:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.