回忆之城
生命在于折腾
posts - 575,comments - 9,trackbacks - 0
方法一:
 #! /bin/bash
i=`ifconfig -a |grep "Link encap"|awk '{print $1}'`
for i in $i
do
echo "$i"
echo "`ifconfig $i |grep "inet addr:" |awk '{print $2}'|awk -F: '{print $2}' `"
let i++
done
确定网卡命令直接取即为:
ifconfig eth0 |grep "inet addr:" |awk '{print $2}'|awk -F: '{print $2}'
ifconfig eth0 |grep "inet addr:" |awk '{print $2}'|awk -F: '{print $2}' |awk -F. '{print $4}' 
方法二:
我的机器是as4,ifconfig 输出的结果和你的不太一样,我按照我的输出结果写了个awk脚本,仅供参考。

awk.sc
BEGIN{RS="\n\n"}
{
  for(i = 1;i< NF; i++)
        {
          if(($i ~ /MTU/)&&($1 != "lo"))
             res[$1]=substr($i,5)
        }
}
END{
for(a in res)
 print a,res[a]
}
ifconfig输出结果

eth0      Link encap:Ethernet  HWaddr 00:0C:29:24:7E:73 
          inet addr:192.168.5.8  Bcast:192.168.5.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fe24:7e73/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:1202 errors:0 dropped:0 overruns:0 frame:0
          TX packets:969 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:108326 (105.7 KiB)  TX bytes:155456 (151.8 KiB)
          Interrupt:67 Base address:0x2024

lo        Link encap:Local Loopback 
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:2132 errors:0 dropped:0 overruns:0 frame:0
          TX packets:2132 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:3106880 (2.9 MiB)  TX bytes:3106880 (2.9 MiB)

执行结果
$ /sbin/ifconfig |awk -f awk.sc   
eth0 1500
posted on 2014-04-20 12:51 回忆之城 阅读(1228) 评论(0)  编辑 收藏 引用 所属分类: unix/linuxshell
只有注册用户登录后才能发表评论。