4

I want to show the IP address like below

lo : 127.0.0.1 
eth0 : 192.168.5.123
eth1 : 192.172.0.212
wlan0 : 10.1.0.124

I'm able to print all IP address by ifconfig | awk '/inet addr/{print substr($2,6)}'. But it is only printing the IPs. Every system have their own interface names and addresses. So my script has to show their interfaces with respect to IP addresses.

7
  • What OS? An interface can have multiple addresses.
    – jordanm
    Feb 2, 2015 at 6:50
  • I do run my script on many OSes(mainly embedded devices like raspberry, intel edison..) @jordanm
    – gangadhars
    Feb 2, 2015 at 6:53
  • I assume they are all Linux? Do you want interfaces without addresses to be ignored?
    – jordanm
    Feb 2, 2015 at 6:54
  • yes. Like I shown above @jordanm.
    – gangadhars
    Feb 2, 2015 at 6:55
  • 1
    well you're probsably best to start with ip address show instead of ifconfig. which is what jordann has done.
    – Jasen
    Feb 2, 2015 at 7:02

6 Answers 6

8

The following will do what you want:

$ ip addr | awk '
/^[0-9]+:/ { 
  sub(/:/,"",$2); iface=$2 } 
/^[[:space:]]*inet / { 
  split($2, a, "/")
  print iface" : "a[1] 
}'
lo : 127.0.0.1
br0 : 10.1.10.12
8
  • +1 for using the "iproute2" method instead of ifconfig.
    – Jasen
    Feb 2, 2015 at 7:06
  • 1. Only works for IPv4, and 2. foobar <(command) is better written as command | foobar. But +1 anyway.
    – Celada
    Feb 2, 2015 at 7:32
  • But I'm not getting any thing. If I run with sh getting error: Syntax error: "(" unexpected. When I run command with bash it's not retuning anything.
    – gangadhars
    Feb 2, 2015 at 8:35
  • @linux_inside, did you try the command with the | instead: ip addr | awk '...etc....' Feb 2, 2015 at 14:28
  • @eewanco, getting same problem
    – gangadhars
    Feb 3, 2015 at 3:03
6
ip -o addr | awk '{split($4, a, "/"); print $2" : "a[1]}'

Or if you are not interested in the local addresses:

ip -o addr show scope global | awk '{split($4, a, "/"); print $2" : "a[1]}'
1
  • +1 for the variant with excluding local addresses.
    – izkeros
    Jan 11, 2021 at 11:30
4

Try this:

ip -o a show | cut -d ' ' -f 2,7

lo 127.0.0.1/8

lo ::1/128

enp0s31f6 10.35.4.166/23

enp0s31f6 2620:52:0:2304:367:c01c:fe74:22ed/64

enp0s31f6 fe80::9a54:7adb:839e:fdb6/64

wlp58s0 10.201.132.132/22

wlp58s0 fe80::fa34:41ff:feb3:a06/64

virbr0 192.168.122.1/24

docker0 172.17.0.1/16

1
  • Why does this only have one upvote? it is the one answer that gets you out of the weeds "-o" ftw!
    – AndreasT
    May 19, 2021 at 7:57
1

This works for me:

for IF in $(ip link show | awk -F: '$1>0 {print $2}')
do
  echo -n "$IF : "
  ip addr show dev "$IF" | awk '$1=="inet"{print gensub("/.*","","",$2)}' | xargs
done

You can run it all on one line if you prefer but I thought it would be marginally easier to understand as a loop.

The ip link show delivers the list of interfaces. The ip addr show dev "$IF" delivers the list of IP addresses associated with the named interface. The awk chops out the IP address for each line that starts with inet.

It does not match inet6 and so will not give you IPv6 addresses. However, if you want to include these you can change the $1=="inet" construct to $1~/^inet/.

3
  • getting error lo : awk: line 2: function gensub never defined
    – gangadhars
    Feb 3, 2015 at 3:00
  • @linux_inside this solution requires gawk as the awk implementation.
    – jordanm
    Feb 3, 2015 at 4:00
  • 1
    @linux_inside if you really want to use vanilla awk then this construct will give you identical results: awk '$1=="inet"{ip=$2; gsub("/.*","",ip); print ip}' Feb 3, 2015 at 9:24
1

For only IPv4 addresses, use

ip -o addr | awk '{split($4, a, "/"); print $2" : "a[1]}' | grep -v '::'

I came up with this because the accepted answer is not convenient to paste into a .bashrc file as an alias.

Typing out such a long command everytime is strenuous.

so i created an alias in my bashrc file

sudo nano ~/.bashrc

add this line to the end of the file

alias myip="ip -o addr | awk '{split(\$4, a, \"/\"); print \$2\" : \"a[1]}' | grep -v '::'"

Save using Ctrl+O then exit using Ctrl+X

source ~/.bashrc

you might have to prefix sudo for the above command

now you can just give the command "myip" in terminal as shown below

enter image description here

0

Typing this below, will list all of interfaces which have ips -

ip r show|grep " src "|cut -d " " -f 3,12

Output:

tun0 10.0.0.1
eth0 10.0.0.2
enp1 10.0.0.3

bare in mind - it won't pick up interfaces which have no ips assigned to them.

1
  • 2
    Welcome to U&L! More recent versions of ip print a different number of spaces between output columns, so this command won't always extract the parts you intended. There may also be more than one route defined per interface, resulting in duplicates.
    – JigglyNaga
    Dec 13, 2018 at 12:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .