分页: 1 / 1

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

发表于 : 2010-08-09 17:24
gzbao9999

代码: 全选

推荐写法
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"<"          (5楼提供)
3 w3m -dump http://submit.apnic.net/templates/yourip.html | grep -P -o '(\d+\.){3}\d+'         (7楼提供)
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          (10楼提供)  ----------------------重点推荐这个,实在是好记
7 curl icanhazip.com     (11楼提供)

不推荐写法(因为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

学习兼整理兼挖坟头 :em05

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

发表于 : 2010-08-09 19:23
Jarson
:em11

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

发表于 : 2010-08-09 19:46
zhoucga
:em11

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

发表于 : 2010-08-10 9:39
trigger
cut不能这样用。因为ip地址是不定长的。
可以cut两次
$ curl -s "http://checkip.dyndns.org/"|cut -f 6 -d" "|cut -f 1 -d"<"

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

发表于 : 2010-08-10 13:57
gzbao9999
trigger 写了:cut不能这样用。因为ip地址是不定长的。
说的对 确实是把ip地址的长度变化给忽略了
那就推荐 第1 第4种 和你这种写法 :em01

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

发表于 : 2010-08-10 14:34
link_01
这东西还是找个好点的网址比较好.
http://submit.apnic.net/templates/yourip.html

代码: 全选

w3m -dump http://submit.apnic.net/templates/yourip.html | grep -P -o '(\d+\.){3}\d+'

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

发表于 : 2010-08-11 0:56
pityonline
太麻烦了……

代码: 全选

curl ifconfig.me

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

发表于 : 2010-08-11 9:13
gzbao9999
pityonline 写了:太麻烦了……

代码: 全选

curl ifconfig.me
这个等价于 curl “http://ifconfig.me

可是有点不明白为什么
curl “http://ifconfig.me
和浏览器打开http://ifconfig.me 查看到的源代码不同

倒是 http://ifconfig.me/ip 这个的地址返回的是纯ip

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

发表于 : 2010-08-11 9:47
pityonline
gzbao9999 写了:
pityonline 写了:太麻烦了……

代码: 全选

curl ifconfig.me
这个等价于 curl “http://ifconfig.me

可是有点不明白为什么
curl “http://ifconfig.me
和浏览器打开http://ifconfig.me 查看到的源代码不同

倒是 http://ifconfig.me/ip 这个的地址返回的是纯ip
网站上有使用curl返回的记录,估计是判断 user agent 而返回不同信息吧……

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

发表于 : 2010-08-15 16:23
roylez
curl icanhazip.com