先看看ifconfig获得什么内容:

[root@nginx01 ~]# ifconfig
eth1      Link encap:Ethernet  HWaddr 00:0C:29:7E:7B:73  
          inet addr:192.168.42.130  Bcast:192.168.42.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fe7e:7b73/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:519 errors:0 dropped:0 overruns:0 frame:0
          TX packets:241 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:50071 (48.8 KiB)  TX bytes:29441 (28.7 KiB)
          Interrupt:19 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:60 errors:0 dropped:0 overruns:0 frame:0
          TX packets:60 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:4064 (3.9 KiB)  TX bytes:4064 (3.9 KiB)

如何获得:

[root@nginx01 ~]# ifconfig |grep 'inet addr'|awk {'print $2'}|grep -v '127.0.0.1'|cut -d: -f2
192.168.42.130
[root@nginx01 ~]# 
  • grep 'inet addr'获取ip部分

  • grep -v '127.0.0.1'排除不需要的部分

  • awk {'print $2'} 获得ip部分

  • cut -d: -f2 根据冒号分割,获得ip部分


Profeel
289 声望17 粉丝

Brilliant's everywhere.


引用和评论

0 条评论