Sometimes when writing your bash scripts, you may need some information about the network, such as the IP addresses, both IPv4 and IPv6, broadcast addresses, netmasks and such. There are two very basic ways of getting the necessary information in Linux systems, you should either choose the ip addr show method, and parse what’s coming out of it, or parse the output of ifconfig. Let’s deal with them both.

The “ip addr show” method

We will parse this output:

eaydin@eaydin:~$ ip addr show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:22:15:f6:55:e6 brd ff:ff:ff:ff:ff:ff
    inet 192.168.16.30/24 brd 192.168.16.255 scope global eth0
    inet6 fe80::222:15ff:fef6:55e6/64 scope link 
       valid_lft forever preferred_lft forever

IP ADDRESS :

ip addr show |grep -w inet |grep -v 127.0.0.1|awk '{ print $2}'| cut -d "/" -f 1
Output : 192.168.16.30

PREFIX (Netmask in CIDR Notation) :

ip addr show |grep -w inet |grep -v 127.0.0.1|awk '{ print $2}'| cut -d "/" -f 2
Output : 24

Broadcast IP :

ip addr show |grep -w inet |grep -v 127.0.0.1|awk '{ print $4}'
Output : 192.168.16.255

Device name :

ip addr show | awk 'FNR==7 {print $2}' | tr -d :
Output : eth0

IPv6 ADDRESS :

ip addr show |grep -w inet6 |grep -v ::1|awk '{ print $2}'| cut -d "/" -f 1
Output : fe80::222:15ff:fef6:55e6

IPv6 PREFIX (Netmask in CIDR Notation) :

ip addr show |grep -w inet6 |grep -v ::1|awk 'FNR==1 { print $2}'| cut -d "/" -f 2
Output : 64

Ethernet Card MAC Address :

ip addr show | grep -w ether | awk '{ print $2 }'
Output : 00:22:15:f6:55:e6

The “ifconfig” method

This time we’ll parse this output:

eaydin@eaydin:~$ ifconfig
eth0      Link encap:Ethernet  HWaddr 00:22:15:f6:55:e6  
          inet addr:192.168.16.30  Bcast:192.168.16.255  Mask:255.255.255.0
          inet6 addr: fe80::222:15ff:fef6:55e6/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:626934 errors:0 dropped:0 overruns:0 frame:0
          TX packets:363506 errors:0 dropped:0 overruns:0 carrier:2
          collisions:0 txqueuelen:1000 
          RX bytes:284072710 (284.0 MB)  TX bytes:56413838 (56.4 MB)
          Interrupt:45 

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:43339 errors:0 dropped:0 overruns:0 frame:0
          TX packets:43339 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:4834972 (4.8 MB)  TX bytes:4834972 (4.8 MB)

IP ADDRESS :

ifconfig | grep -w inet |grep -v 127.0.0.1| awk '{print $2}' | cut -d ":" -f 2
Output : 192.168.16.30

Broadcast IP :

ifconfig | grep -w inet |grep -v 127.0.0.1| awk '{print $6}' | cut -d ":" -f 2

Output : 192.168.16.255

Netmask :

ifconfig | grep -w inet |grep -v 127.0.0.1| awk '{print $4}' | cut -d ":" -f 2
Output : 255.255.255.0

Device name :

ifconfig | awk 'FNR==1 { print $1 }' | tr --d :
Output : eth0

IPv6 ADDRESS :

ifconfig | grep -w inet6 | grep -v ::1| awk '{ print $2 }'

Output : fe80::222:15ff:fef6:55e6

IPv6 PREFIX (Netmask in CIDR Notation) :

ifconfig | grep -w inet6 | awk 'FNR == 2 { print $4 }'
Output : 64

Ethernet Card MAC Address :

ifconfig | grep -w ether | awk '{ print $2 }'
Output : 00:22:15:f6:55:e6

Leave a Reply

Your email address will not be published. Required fields are marked *