Return a list of all interfaces and currently assigned IP addresses

bash - array traversal example

This script will do as the title to this blogs suggests:-) On it's own this in not much use however, it serves as an example of how to traverse an array in bash, and of course is a snippet you can use within other scripts.

The script will only return details about ethX devices. For example the information for the following devices will not be returned -lo, lo0:X. vmnetX ...  

Regards, Bawdo2001

 ---- CUT ----
#!/bin/bash
declare -a IPADDR

index=0

for i in $( ifconfig | grep 'inet addr' | awk '{print $2}'| sed 's#addr:##g' );
do
        IPADDR[$index]=$i
        let "index += 1"
done

index=0

for i in $( ifconfig | grep 'eth' | awk '{print $1}' );
do
        echo $i ${IPADDR[$index]}
        let "index += 1"
done
---- CUT ----

Here is a sample of the output:

eth0 192.168.1.75
eth0:0 192.168.1.250
eth0:1 192.168.120.250
eth1 10.10.120.246

tags: bash, Linux

Mon 13 Feb 2006, 18:58

0 comments

Back