How to get IP address and the connected interface name in bash

2

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?

xakepp35

Posted 2018-10-17T09:28:32.613

Reputation: 287

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 – 2018-10-17T09:49:28.490

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 – 2018-10-17T10:33:12.897

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 – 2018-10-17T11:36:29.043

@xakepp35 a browser doesn't decide which network interface to use, the way packets go out is decided by the routing table – AnonymousLurker – 2018-10-17T13:15:17.357

@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 – 2018-10-17T14:14:06.340

Answers

3

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 192.168.1.2

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

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

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

Kamil Maciorowski

Posted 2018-10-17T09:28:32.613

Reputation: 38 429

More better , adding awk '{print $2, $4}' – GAD3R – 2018-10-17T10:42:55.677

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 – 2018-10-17T12:57:48.453