多种取ip地址方法

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了多种取ip地址方法相关的知识,希望对你有一定的参考价值。

方法一:

[[email protected]~]# ifconfig eth0|sed -n ‘2p‘|sed ‘s#^.*addr:##g‘|sed ‘s#Bc.*$##g‘
10.0.0.201 

方法二:

[[email protected]~]# ifconfig eth0|sed -n ‘2p‘|sed ‘s#^.*addr:\(.*\)Bc.*$#\1#g‘
10.0.0.201 

方法三:(推荐)

[[email protected]~]# ifconfig eth0|sed -nr‘2s#^.*addr:(.*) Bca.*$#\1#g‘p
10.0.0.201

方法四:

[[email protected]~]# ifconfig eth0|sed -n ‘2p‘|sed -r ‘s#(^.*addr:)(.*)(Bc.*$)#\2#g‘
10.0.0.201

方法五:

[[email protected]~]# ifconfig eth0|sed -n ‘2p‘|egrep -o "([0-9]{1,3}.?){4} "|head -1
10.0.0.201 

 

方法六:(推荐)

[[email protected]~]# ifconfig eth0 |awk -F "[ :]+" ‘NR==2{print $4}‘
10.0.0.201

方法七:(推荐)

[[email protected]~]# ifconfig eth0|sed -n ‘2p‘|sed ‘s#:# #g‘|awk ‘{print $3}‘
10.0.0.201

方法八:awk在默认的情况下,会自动忽略前面的空格

[[email protected]~]# ifconfig eth0|sed -n ‘2p‘|sed ‘s#:##g‘|awk ‘{print $2}‘|sed ‘s#addr##g‘

10.0.0.201

方法九:

[[email protected]~]# ifconfig eth0|awk ‘NR==2‘|cut -c 21-30
10.0.0.201

方法十:

[[email protected]~]# ifconfig eth0 |grep "inet addr"|cut -d ":" -f2|tr "Bcast" " "
10.0.0.201  

方法十一:

[[email protected]~]# ifconfig eth0|grep "inet addr"|awk -F "[ :]+" ‘{print$4}‘
10.0.0.201


以上是关于多种取ip地址方法的主要内容,如果未能解决你的问题,请参考以下文章

如何取eth0中的IP地址

QT通过IP地址定位地址(用get方法取数据)

多级反向代理下,Java获取请求客户端的真实IP地址多种方法整合

NAT地址转换

取ip地址,最简单粗暴的2种方法

Linux取ip地址