获取计算机外网ip的几种写法

推荐写法
1 curl "http://checkip.dyndns.org/" 2>/dev/null|awk '{print $6}'|cut -d '<' -f1
2 curl -s "http://checkip.dyndns.org/"|cut -f 6 -d" "|cut -f 1 -d"<"      
3 w3m -dump http://submit.apnic.net/templates/yourip.html | grep -P -o '(\d+\.){3}\d+'         
4 curl -s "http://checkip.dyndns.org/"| sed 's/.*Address: \([0-9\.]*\).*/\1/g'
5 curl -s "http://checkip.dyndns.org/"|cut -d "<" -f7|cut -c 26-
6 curl ifconfig.me        
7 curl icanhazip.com    

不推荐写法(因为ip长度是可变的)
8 curl "http://checkip.dyndns.org/" 2>/dev/null|while read line;do echo ${line:76:12};done
9 curl "http://checkip.dyndns.org/" 2>/dev/null|cut -c 77-88
10 curl -s "http://checkip.dyndns.org/"|cut -c 77-88

不知道为什么最近碰到这么多获取外网IP的帖子,搞不懂啊搞不懂,不过很使用,会有人喜欢的

这个来源:http://forum.ubuntu.org.cn/viewtopic.php?f=21&t=288217&start=0

你可能感兴趣的:(计算机,外网,ip)