1. 程式人生 > >多種取ip地址方法

多種取ip地址方法

取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地址方法