How to find the name of the network interface

I have a bash script that runs on different Linux Ubuntu machines. Its task is to find out the LAN IPv4 address of the local host.

script uses

ip addr show eth0 | sed -n '/inet /{s/^.*inet \([0-9.]\+\).*$/\1/;p}' 

which is good, but some machines for some reason use eth1 instead of eth0. I would like to be able to open the LAN name iface, so I can replace it here instead of eth0.

Of course, if you can come up with another oneliner that does the same thing, all is well.

+4
source share
7 answers

Not sure if this helps, but it seems that ip route get will show which interface it uses to connect to the remote host.

 ubuntu@ip-10-40-24-21 :/nail/srv/elasticsearch$ ip route get 8.8.8.8 8.8.8.8 via <gateway address> dev eth0 src <eth0 IP Address> 

of course you could automate this in a shell script with something like

 ip route get 8.8.8.8 | awk '{ print $NF; exit }' 
+11
source

The primary network adapter usually has a default route. So:

 ip -o -4 route show to default 

Network Card:

 ip -o -4 route show to default | awk '{print $5}' 

Gateway:

 ip -o -4 route show to default | awk '{print $3}' 

Unlike ifconfig, ip has serial and collapsible output. It works only with Linux; it will not work on other Unixen.

+3
source

What about inet and brd string lookups (for translation)? This will give you:

 ip addr show|egrep '^ *inet'|grep brd|awk -- '{ print $2; }'|sed -e 's:/[0-9]*$::' 

Note that I use more commands than necessary; you can probably achieve the same with sed and the more sophisticated regexp, but I prefer a command that makes it obvious which steps I get to the result.

If you want to run it in one command, I suggest trying awk :

 ip addr show|awk -- '$1 == "inet" && $3 == "brd" { split($2,a,"/"); print a[1]; }' 

which is not much longer than the sed version, but more readable.

+1 A little more readable:

 ip addr show | awk '$1 == "inet" && $3 == "brd" { sub (/\/.*/,""); print $2 }' 
+2
source

Believe it or not, there is no standard and easy way to get this information. No standard, give me the current IP and interface command. There is even a standard format for the information returned by ifconfig .

I was going to recommend abandoning the clean shell and go with a scripting language like Python where you can do this:

 import socket socket.gethostbyname(socket.gethostname()) 

Except that it does not work on most Linux systems, because usually in the /etc/host file the entry 127.0.0.1 indicated, the feedback address. Perl has the same problems.

You have a clear understanding of the scenarios and you have seen problems. The only thing I can recommend is to check it on every machine you are going to run it on and see what pops up. There will not be a general purpose that works with all operating systems or even with different systems in the same operating system due to the way the network is configured and how the interfaces can be named by each location.

+2
source

I would still like to know if there is an easier way to do this, but this is my workaround: I know the LAN subnet, so ...

 ip addr show | grep "inet 10.67.5." \ | sed -n '/inet /{s/^.*inet \([0-9.]\+\).*$/\1/;p}' 
+1
source

1) This is one name for printing only for the interface (I needed to transfer upcoming and pending PPP links):

 for i in $( ifconfig | grep 'ppp' | awk '{print $1}' ); do printf "$i "; ## Or echo done 

Result:

ppp0 ppp1 ppp2

2) This printer prints interface names and IP

 declare -a IPADDR index=0 for i in $( ifconfig | grep 'inet addr' | awk '{print $2}'| sed 's#addr:##g' ); do IPADDR[$index]=$i let "index += 1" done index=0 for i in $( ifconfig | grep 'ppp' | awk '{print $1}' ); do echo $i let "index += 1" done 

Result:

ppp0 addr: IP

ppp1 addr: IP

ppp2 addr: IP

+1
source

Recently, systemd / udev has automatically started to assign interface names for all local Ethernet, WLAN, and WWAN interfaces for something we are used to. This is a departure from the traditional interface naming scheme ("eth0", "eth1", "wlan0", ...). Now we need to first check what the name of the local interface is before we can use it, until we had a fairly accurate assumption that "eth0" was the correct name. What you are asking for is a network name. Here is a small script to solve the problem

  • Use "ip route get 8.8.8.8" to find out which ACTIVE interface has a route to the Internet (or is currently using). The result should look like this:

    8.8.4.4 through 10.10.1.1 dev enp0s3 src 10.10.1.118 Cache

  • Use awk to print the 5th text block for the NAME interface

    ] # ip route get 8.8.8.8 | awk - '{print $ 5}' Exit: enp0s3

  • Use awk to print the 7th text block for an interface address

    ] # ip route get 8.8.8.8 | awk - '{print $ 7}' Exit: 10.10.1.118

+1
source

Source: https://habr.com/ru/post/1434800/


All Articles