Linux 获取当前IP

来源:互联网 发布:数据录入员前景怎么样 编辑:程序博客网 时间:2024/05/01 17:39

Linux:

LC_ALL=C ifconfig  | grep 'inet addr:'| grep -v '127.0.0.1' |cut -d: -f2 | awk '{ print $1}'

LC_ALL=C 英文输出
ifconfig 输出ip相关信息
复制代码
# LC_ALL=C ifconfigeth0      Link encap:Ethernet  HWaddr C2:AC:C3:E7:4A:33            inet addr:192.168.1.247  Bcast:0.0.0.0  Mask:255.255.255.0          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1          RX packets:84080 errors:0 dropped:80 overruns:0 frame:0          TX packets:3595264 errors:0 dropped:0 overruns:0 carrier:0          collisions:0 txqueuelen:1000           RX bytes:14627750 (13.9 MiB)  TX bytes:417627051 (398.2 MiB)          Interrupt:27 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:127 errors:0 dropped:0 overruns:0 frame:0          TX packets:127 errors:0 dropped:0 overruns:0 carrier:0          collisions:0 txqueuelen:0           RX bytes:18079 (17.6 KiB)  TX bytes:18079 (17.6 KiB)
复制代码

 

grep 'inet addr:'  截取包含ip的那两行
# LC_ALL=C ifconfig|grep "inet addr:"          inet addr:192.168.1.247  Bcast:0.0.0.0  Mask:255.255.255.0          inet addr:127.0.0.1  Mask:255.0.0.0
grep -v '127.0.0.1'   去掉本地指向的那行
# LC_ALL=C ifconfig|grep "inet addr:"|grep -v "127.0.0.1"          inet addr:192.168.1.247  Bcast:0.0.0.0  Mask:255.255.255.0 
cut -d: -f2    -d: 以:分割字符串  -f2:取第二组数据
# LC_ALL=C ifconfig|grep "inet addr:"|grep -v "127.0.0.1"|cut -d: -f2192.168.1.247  Bcast
awk '{ print $1}'  $1 表示默认以空格分割的第一组 同理 $@表示第二组
# LC_ALL=C ifconfig|grep "inet addr:"|grep -v "127.0.0.1"|cut -d: -f2|awk '{print $1}'192.168.1.247# LC_ALL=C ifconfig|grep "inet addr:"|grep -v "127.0.0.1"|cut -d: -f2|awk '{print $2}'Bcast

 

0 0
原创粉丝点击