Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

get address of specific interface stanza in config file 1

Status
Not open for further replies.

w5000

Technical User
Nov 24, 2010
223
PL

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?


 
Hi

As far as I know, the [tt]iface[/tt] must occur before the [tt]address[/tt], so I would toggle a flag when encountering an [tt]iface[/tt], to tell whether we are after the desired [tt]iface[/tt] instance or not.
Code:
awk '[navy]$1[/navy][teal]==[/teal][i][green]"iface"[/green][/i][teal]{[/teal][navy]this[/navy][teal]=[/teal][navy]$2[/navy][teal]==[/teal][i][green]"eth2"[/green][/i][teal]}[/teal]this[teal]&&[/teal][navy]$1[/navy][teal]==[/teal][i][green]"address"[/green][/i][teal]{[/teal][b]print[/b][navy]$2[/navy][teal];[/teal][b]exit[/b][teal]}[/teal]' /etc/network/interfaces

I am not a networking wizard, so there may be situations when this fails. So continuing with your code :
Code:
awk '[b]BEGIN[/b][teal]{[/teal][navy]RS[/navy][teal]=[/teal][navy]ORS[/navy][teal]=[/teal][i][green]"[/green][/i][lime]\n\n[/lime][i][green]"[/green][/i][teal];[/teal][navy]FS[/navy][teal]=[/teal][navy]OFS[/navy][teal]=[/teal][i][green]"[/green][/i][lime]\n[/lime][i][green]"[/green][/i][teal]}[/teal][fuchsia]/eth2/[/fuchsia][teal]{[/teal][b]for[/b][teal]([/teal][navy]i[/navy][teal]=[/teal][purple]1[/purple][teal];[/teal]i[teal]<=[/teal]NF[teal];[/teal]i[teal]++)[/teal][b]if[/b][teal]([/teal][navy]$i[/navy][teal]~[/teal][fuchsia]/^address /[/fuchsia][teal]){[/teal][b]sub[/b][teal]([/teal][fuchsia]/^address\s*/[/fuchsia][teal],[/teal][i][green]""[/green][/i][teal],[/teal][navy]$i[/navy][teal]);[/teal][b]print[/b][navy]$i[/navy][teal];[/teal][b]exit[/b][teal]}}[/teal]' /etc/network/interfaces

Feherke.
feherke.ga
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top