Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I know that the command ifconfig will give you ip, mask and gateway of a given network interface. I want to "programmatically" extract each of those paramaters separately. At the moment the best solution I can think of is to do some parsing by using grep.

Are there ifconfig flags to single out ip, mask or gateway? Are there other *nix commands that will extract just one parameter of a given network interface?

share|improve this question

1 Answer 1

up vote 1 down vote accepted

Theses are not very elaborates but seems to work at least:

for bond0:

Get the IP

ifconfig | awk '/bond0/ { getline; print}' | awk '{print $2}' | awk 'BEGIN {FS=":"} ; {print $2}'

Get the the broadcast address:

ifconfig | awk '/bond0/ { getline; print}' | awk '{print $3}' | awk 'BEGIN {FS=":"} ; {print $2}'

Get the netmask:

ifconfig | awk '/bond0/ { getline; print}' | awk '{print $4}' | awk 'BEGIN {FS=":"} ; {print $2}'

and for the Gateway:

route | awk '/default/ { print $2}'

sure someone will get there with something more simple or straighforward.

Hope this helps.

share|improve this answer
    
Thanks. Not pretty, but gets the job done, I guess. –  Randomblue Feb 11 '13 at 10:57

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.