8

Following is my ifconfig output

eth0      Link encap:Ethernet  
          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)
          Interrupt:28 Base address:0x2000 

eth1      Link encap:Ethernet  
          inet addr:192.168.1.2  Bcast:192.168.1.255  Mask:255.255.255.0
      UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:36497 errors:0 dropped:0 overruns:0 frame:14515
          TX packets:44884 errors:1352 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:20781745 (20.7 MB)  TX bytes:17776225 (17.7 MB)
          Interrupt:17 Base address:0xc000 

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:12 errors:0 dropped:0 overruns:0 frame:0
          TX packets:12 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:720 (720.0 B)  TX bytes:720 (720.0 B)

virbr0    Link encap:Ethernet  
          inet addr:192.168.122.1  Bcast:192.168.122.255  Mask:255.255.255.0

          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:24 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:0 (0.0 B)  TX bytes:4416 (4.4 KB)

vmnet1    Link encap:Ethernet 
          inet addr:192.168.185.1  Bcast:192.168.185.255  Mask:255.255.255.0

          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:24 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

vmnet8    Link encap:Ethernet  
          inet addr:192.168.207.1  Bcast:192.168.207.255  Mask:255.255.255.0

          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:25 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

How do I use grep to extract the IP address corresponding to each LAN card?

Is that possible? How can it be achieved?

John Gardeniers
  • 27,458
  • 12
  • 55
  • 109
Registered User
  • 1,463
  • 5
  • 18
  • 37

9 Answers9

15
$ ip -o addr show | awk '/inet/ {print $2, $3, $4}'
lo inet 127.0.0.1/8
lo inet6 ::1/128
eth0 inet 192.168.0.1/24
eth0 inet6 fe80::2a0:feed:dead:beef/64
Dennis Williamson
  • 62,149
  • 16
  • 116
  • 151
2

Aw man I can't resist trying to improve my perl oneliner scripting ability when I see a post like this. Here's a first hacky attempt:

$ /sbin/ifconfig | \
perl -ane "(\$n)=(/^(\S+)/) and print \"\$n: \"; print (/addr:(\S+)/) and print \"\n\" if /inet/"
eth0: 192.168.0.11
lo: 127.0.0.1

That could be tightened up a lot, and it has lots of problem like it doesn't handle ipv6 addresses, etc.

Phil Hollenback
  • 14,947
  • 4
  • 35
  • 52
1

This is how I do it:

ETHERADD=$(/sbin/ifconfig |grep -e ^[a-z] |grep -v lo | awk '{ printf $1 FS}')

for i in $ETHERADD  
do
    IP=$(/sbin/ifconfig $i | grep -o '[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*' | head -1)
    printf "%-16s%-42s*\n" "IP $i:"      "$IP"
done
Bart De Vos
  • 17,911
  • 6
  • 63
  • 82
1

ip addr show | grep inet | awk -F " " '{print $NF"|"$2}'

Mark
  • 740
  • 5
  • 5
  • The ip command is from [iproute2](http://www.linuxfoundation.org/collaborate/workgroups/networking/iproute2). "New" kid on the block. – Mark Mar 06 '11 at 21:56
1

This is what I use

#!/bin/bash
interfaces=$(/sbin/ifconfig |grep -e ^[a-z] |  awk '{ printf $1 " "}')
for i in $interfaces
do
    addr=$(/sbin/ifconfig $i | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}')
    addr6=$(/sbin/ifconfig $i | grep 'inet6 addr:' |sed 's/   *//g'| cut -d' ' -f3 | cut -d'/' -f1)
    echo "$i  inet4 $addr    inet6 $addr6"
done
user9517
  • 115,471
  • 20
  • 215
  • 297
0

How about

ifconfig -a|awk '{print $1 " " $2}'|egrep -w 'Link|inet'|sed 's/ Link//'|sed 's/inet addr://'

On your output, that returns

eth0
eth1
192.168.1.2
lo
127.0.0.1
virbr0
192.168.122.1
vmnet1
192.168.185.1
vmnet8
192.168.207.1

for me.

MadHatter
  • 79,770
  • 20
  • 184
  • 232
0

Horribly ugly, and only good for IPv4:

ifconfig | egrep "^[a-z]|inet " | sed -e "s/ [ ]*Link.*/@/" -e "s/.*inet addr://" -e "s/ .*/#/" | tr -d '\012' | tr '@' ' ' | tr '#' '\012'
Cry Havok
  • 1,845
  • 13
  • 10
0

ifconfig | grep addr > myipaddr.txt

Eddy
  • 7
  • 2
  • 10
0

Not sure how portable this is, but you can avoid the whole sed/grep/awk/perl etc with the hostname command and either -i or -I (I recommend hostname -I):

   -i, --ip-address
          Display the network address(es) of the host name. Note that this works only if the host name can be resolved. Avoid using this option;  use  hostname
          --all-ip-addresses instead.

   -I, --all-ip-addresses
          Display all network addresses of the host. This option enumerates all configured addresses on all network interfaces. The loopback interface and IPv6
          link-local addresses are omitted. Contrary to option -i, this option does not depend on name resolution. Do not make any assumptions about the  order
          of the output.

For example:

[kbrandt@alpine: ~/] hostname -I
192.168.1.100 10.1.0.42 
Kyle Brandt
  • 83,619
  • 74
  • 305
  • 448