Question

I was going to write a bash script to pull out info I wanted from ifconfig -a

but then I saw this bash one liner:

/sbin/ifconfig | awk -v RS="\n\n" '{ for (i=1; i<=NF; i++) if ($i == "inet" && $(i+1) ~   /^addr:/) address = substr($(i+1), 6); if (address != "127.0.0.1") printf "%s\t%s\n", $1, address }'

Which produces output like:

eth1    10.91.5.189
eth2    10.101.3.96

I would like to strip more info out than this though... so I would like:

ethX MAC-add IP Bcast Mask

... how can I alter the above one liner to get the output I am after?

Was it helpful?

Solution

Here is a working version based on Kents verson:

ifconfig -a|awk -v RS="" '{for(i=1;i<=NF;i++){
    if($i=="HWaddr") mac=$(i+1)
        else if($i~/addr:[0-9]/) {split($i,a,":");ip=a[2]}
        else if($i~/Mask/) {split($i,a,":");mask=a[2]}}
    if(ip!="127.0.0.1")print $1,mac,ip,bcast,mask}'
eth0 00:18:71:6a:f0:45 192.168.1.30 255.255.255.0

I have skipped the Broad Cast address, since its always the last IP in the segment and normally not needed.

OTHER TIPS

try this, should work:

 ifconfig -a|awk -v RS="" '{for(i=1;i<=NF;i++){
        if($i=="inet")ip=$(i+1);
        else if($i=="broadcast")bcast=$(i+1);
        else if($i=="ether")mac=$(i+1);
        else if($i=="netmask")mask=$(i+1)}
        if(ip!="127.0.0.1")print $1,mac,ip,bcast,mask}'
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top