linux 获取本机 ip 地址
ifconfig | grep inet | grep -v '127.0.0.1' | grep -v inet6 | cut -d: -f2 | awk '{print $1}'
分解下上面的命令组合
eth0 Link encap:Ethernet HWaddr f0:de:f1:d8:19:f6
UP BROADCAST MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
Interrupt:20 Memory:f2500000-f2520000
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:65536 Metric:1
RX packets:32486 errors:0 dropped:0 overruns:0 frame:0
TX packets:32486 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1
RX bytes:22767938 (22.7 MB) TX bytes:22767938 (22.7 MB)
wlan0 Link encap:Ethernet HWaddr 24:77:03:28:ab:0c
inet addr:192.168.1.105 Bcast:192.168.1.255 Mask:255.255.255.0
inet6 addr: fe80::2677:3ff:fe28:ab0c/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:83327 errors:0 dropped:0 overruns:0 frame:0
TX packets:82573 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:75036136 (75.0 MB) TX bytes:19201767 (19.2 MB)
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
inet addr:192.168.1.105 Bcast:192.168.1.255 Mask:255.255.255.0
inet6 addr: fe80::2677:3ff:fe28:ab0c/64 Scope:Link
ifconfig | grep inet | grep -v inet6
inet addr:127.0.0.1 Mask:255.0.0.0
inet addr:192.168.1.105 Bcast:192.168.1.255 Mask:255.255.255.0
ifconfig | grep inet | grep -v inet6 | grep -v '127.0.0.1'
inet addr:192.168.1.105 Bcast:192.168.1.255 Mask:255.255.255.0
ifconfig | grep inet | grep -v inet6 | grep -v '127.0.0.1' | cut -d: -f2
192.168.1.105 Bcast
ifconfig | grep inet | grep -v inet6 | grep -v '127.0.0.1' | cut -d: -f2 | awk '{print $1}'
192.168.1.105