INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

get address of specific interface stanza in config file

get address of specific interface stanza in config file

get address of specific interface stanza in config file

(OP)

hello.

I have network interfaces configuration file on debian - with a few stanzas like:

allow-hotplug eth2
iface eth2 inet static
address 192.168.198.24
netmask 255.255.255.0
broadcast 192.168.198.255

I'd like to extract from this file IP of address attribute of specific interface - for example eth2:

what I have now is:
awk 'BEGIN{RS=ORS="\n\n";FS=OFS="\n"}/eth2/' /etc/network/interfaces|awk '$1=="address"{print $2;exit}'

how to rid of last awk (pipe) and do in one?


RE: get address of specific interface stanza in config file

Hi

As far as I know, the iface must occur before the address, so I would toggle a flag when encountering an iface, to tell whether we are after the desired iface instance or not.

CODE

awk '$1=="iface"{this=$2=="eth2"}this&&$1=="address"{print$2;exit}' /etc/network/interfaces 

I am not a networking wizard, so there may be situations when this fails. So continuing with your code :

CODE

awk 'BEGIN{RS=ORS="\n\n";FS=OFS="\n"}/eth2/{for(i=1;i<=NF;i++)if($i~/^address /){sub(/^address\s*/,"",$i);print$i;exit}}' /etc/network/interfaces 

Feherke.
feherke.ga

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close