Im trying to parse ifconfig with awk and setup a bunch of variables in one shot. But Im having trouble figuring out how to work with data in previous lines.
ifconfig output:
eth0 Link encap:Ethernet HWaddr 00:50:DA:10:7F:1B
inet addr:10.10.10.10 Bcast:10.10.10.127 Mask:255.255.255.192
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:1307626 errors:0 dropped:0 overruns:199 frame:0
TX packets:429249 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:104476566 (99.6 Mb) TX bytes:46971778 (44.7 Mb)
Interrupt:12 Base address:0xc000
eth1 Link encap:Ethernet HWaddr 00:0B:AD:01:11:3F
inet addr:192.168.1.200 Bcast:192.168.1.255 Mask:255.255.0.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:6 errors:0 dropped:0 overruns:0 frame:0
TX packets:3 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:456 (456.0 b) TX bytes:126 (126.0 b)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:80524 errors:0 dropped:0 overruns:0 frame:0
TX packets:80524 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:23978596 (22.8 Mb) TX bytes:23978596 (22.8 Mb)
awk command:
ifconfig | awk '/addr\:/ {if($0 ~ "192.168.1" || $0 ~ "127.0.0.1") {next};\
x=$2; y=NR-1}\
/addr\:/ {if($0 ~ "192.168.1"); a=$2; b=NF-1}\
\
END \
\
{sub("addr:","",x); split(x,ip,"."); split(b,ethlan,FS); split(y,ethwan,FS);\
\
printf "export ipaddress=%s\nexport ISPIP=%s\nexport BUSIP=%s.%s.%s.%s\nexport VIPA=%s.%s.%s.%s\nexport WAN_DEV=%s\nexport LAN_DEV=%s\n",x,x,ip[1],ip[2],ip[3],int(ip[4]+1),ip[1],ip[2],ip[3],int(ip[4]+5),ethwan[1],ethlan[1]}'
I know that's sloppy formatting.. sorry about that.
Current output is this:
export ipaddress=10.10.10.10
export ISPIP=10.10.10.10
export BUSIP=10.10.10.11
export VIPA=10.10.10.15
export WAN_DEV=3
export LAN_DEV=1
Id like for it to be this:
export ipaddress=10.10.10.10
export ISPIP=10.10.10.10
export BUSIP=10.10.10.11
export VIPA=10.10.10.15
export WAN_DEV=eth0
export LAN_DEV=eth1
My problem is getting variable y and b, to be the eth0 and eth1 line (rather then the line number). Im sure that awk line could be more efficient but im not the greatest with awk... I cant even format the spacing correctly :o
---------- Post updated 05-13-10 at 01:02 PM ---------- Previous update was 05-12-10 at 03:37 PM ----------
ok, I got this worked through... I had a c dev help me out and he cleaned up my logic, and added a hold=$0 to the end, then I just change the b and y=NF-1 to b=$0 and y=$0. which gives me a variable for the previous line.