正则表达式得到网卡代号和IP

正则表达式得到网卡代号和IP

正则表达式得到网卡代号和IP
目前又遇到一个小小问题,头都转不过弯了,先来段源代码 open(FH,"ifconfig |") or die "Can not ifconfig :$!\n";
while(<FH>)
{
#if (/(eth0:\d+)(.*)(addr:\d+\.\d+.\d+\.\d+)/m)
if (/(eth0:\d+)|(\d+\.\d+.\d+\.\d+)/)
#if (/(\d{2,3}\.\d{2,3}\.\d{2,3}\.\d{2,3})/)
{
print "$1\n";
print "===>$2\n";

}
}
close(FH);
#######
我的目的是想得到机器上eth0:1,eth0:2这种一个网卡邦定的多个ip的信息,但是打出来的东西事与愿违
结果如下:


===>10.143.158.174
eth0:0
===>

===>192.168.66.1
eth0:1
===>

===>192.168.0.1
eth0:2
===>

===>192.168.0.2
eth0:3
===>

===>192.168.0.3

===>127.0.0.1

####得到了所有显示出来的ip和eth0:x这种
主要因为ifconfig输出的东西是多行的,能不能有一种正则表达式
能够提取eth0:x里面的addr:xxx.xxx.xxx.xx这种ip,而不管其他的呢。
看看那位兄弟能帮忙想想,给个表达式,嘿嘿
ifconfig输出如下
eth0 Link encap:Ethernet HWaddr 00:0C:29:96:35:D3
inet addr:10.143.158.174 Bcast:10.143.158.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:690411 errors:0 dropped:0 overruns:0 frame:0
TX packets:11784 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:100
RX bytes:87382880 (83.3 Mb) TX bytes:864230 (843.9 Kb)
Interrupt:11 Base address:0x10c0

eth0:0 Link encap:Ethernet HWaddr 00:0C:29:96:35:D3
inet addr:192.168.66.1 Bcast:192.168.66.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
Interrupt:11 Base address:0x10c0

eth0:1 Link encap:Ethernet HWaddr 00:0C:29:96:35:D3
inet addr:192.168.0.1 Bcast:192.168.0.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
Interrupt:11 Base address:0x10c0

eth0:2 Link encap:Ethernet HWaddr 00:0C:29:96:35:D3
inet addr:192.168.0.2 Bcast:192.168.0.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
Interrupt:11 Base address:0x10c0

eth0:3 Link encap:Ethernet HWaddr 00:0C:29:96:35:D3
inet addr:192.168.0.3 Bcast:192.168.0.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
Interrupt:11 Base address:0x10c0

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:4 errors:0 dropped:0 overruns:0 frame:0
TX packets:4 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:348 (348.0 b) TX bytes:348 (348.0 b)
改成这样行不行?
flag=0;
while(<FH>){
if (/(eth0:\d+)/){
print "$1\n";
flag=1;
}
if((/(\d+\.\d+.\d+\.\d+)/)&&(flag==1){
print "===>$1\n";
flag=0;
}
}
close(FH);
得到结果!----eth0:0.
得到结果!

eth0:0
===>123.123.1.4
eth0:1
===>1.123.1.4
eth0:2
===>192.192.168.1
eth0:3
===>192.192.168.2
eth0:4
===>192.192.168.3
######哈哈看来可行,加一个判断就好,只要有eth0:匹配的采取匹配下面的ip这个主意不错!
成功了,谢谢