سؤال

I wanted to show th device name and the ip. can any one knows how i can extract this ?

ifconfig -a 

eth0      Link encap:Ethernet  HWaddr 10:60:4b:db:60:86  
          UP BROADCAST MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

lo        Link encap:Local Loopback  
          inet addr: 1.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:1366 errors:0 dropped:0 overruns:0 frame:0
          TX packets:1366 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:245049 (245.0 KB)  TX bytes:245049 (245.0 KB)
هل كانت مفيدة؟

المحلول

May I propose:

ifconfig -a | tr '\n' '~' | sed 's/~~/\n/g' | \
sed 's/^\([^ ]*\) .*inet addr:\([0-9.]*\) .*$\|^\([^ ]*\) .*$/\3\1 \2/g'

Output:

eth0 10.0.41.21
eth1 
lo 127.0.0.1

Or, if you want to omit any interfaces without IP addresses:

ifconfig -a | tr '\n' '~' | sed 's/~~/\n/g' | grep 'inet addr:' | \
sed 's/^\([^ ]*\) .*inet addr:\([0-9.]*\) .*$/\1 \2/g'

Output:

eth0 10.0.41.21
lo 127.0.0.1

This works by converting all the line breaks to tildes, then converts the double line break between interfaces back into a single line break, so that each interface is on a single line. Then, for every line, it extracts just the wanted pieces of information (interface name and IP address) and replaces the entire line with them.

(Edited since being accepted to accomodate interfaces without IP addresses, per comment below.)

نصائح أخرى

This is by far the best way I have found to get the IP.

IP=$(ip route get 8.8.8.8 | awk '/8.8.8.8/ {print $NF}')
echo "$IP"
192.168.1.102

To get the interface

IF=$(ip route get 8.8.8.8 | awk '/8.8.8.8/ {print $5}')
echo "$IF"
eth0

The reason for this working well, is that it gives you the IP and interface.
On VPS servers interface may have name like veth0.
This solution get correct interface by asking for where to find a route to a public IP.
You may use other than 8.8.8.8 (googles dns), but for most this should be fine.

Using ifconfig will fail if you just look for eth0 and you are using eth1 etc.
It may fail if multiple interface have IP and is connected, but only one is used to get to internet.

Your command did not work for me at all.

Just because the IP address is NOT on the same line as device name - it will not be easy. You will need to combine some additional commands or may be write a little script.

to see only ip addresses you can use this :

ifconfig -a | grep "inet add" | awk -F ":" {'print $2'} | awk {'print $1'}

check if you have "nm-tool" installed on you linux machine and figure it out how to use this command with additional sed and awk tools to find out what you need.

Please try this: Please save it as somefile.py and run it by python somefile.py

import commands

output=commands.getoutput('ifconfig')
count=0
for line in output.split("\n"):
        count+=1
        if  str("eth") in line:
                devicename = line.split()[0]
                lineipadd =  output.split("\n")[count]
                ipaddress = lineipadd.split(":")[2].split()[0]
                print devicename , ipaddress
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top