Linux – Get network interface information

ifconfiglinux

I know there is ifconfig command that we can list network interface info.
but i want to get information in the following pattern

Interface_Name IP_Address Net_Mask Status(up/down)

for example

eth0 192.168.1.1 255.255.255.0 down

I tried ifconfig and grep command but can't get right pattern.
There is another command or some trick to do this?

Best Solution

Python is good :D but let see in bash:

Interfaces=`ifconfig -a \
    | grep -o -e "[a-z][a-z]*[0-9]*[ ]*Link" \
    | perl -pe "s|^([a-z]*[0-9]*)[ ]*Link|\1|"`

for Interface in $Interfaces; do
    INET=`ifconfig $Interface | grep -o -e "inet addr:[^ ]*" | grep -o -e "[^:]*$"`
    MASK=`ifconfig $Interface | grep -o -e "Mask:[^ ]*"      | grep -o -e "[^:]*$"`
    STATUS="up"
    if [ "$INET" == "" ]; then
        INET="-"
        MASK="-"
        STATUS="down";
    fi
    printf "%-10s %-15s %-16s %-4s\n" "$Interface" "$INET" "$MASK" "$STATUS"
done

It is quite straightforward.

This is done on an assumption that 'ifconfig interface does not show an internet address' to means that the interface is down.

I hope this helps.

Related Question