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

sh/bash/dash/ksh/zsh等Shell脚本
回复
gzbao9999
帖子: 627
注册时间: 2008-11-08 18:34

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

#1

帖子 gzbao9999 » 2010-08-09 17:24

代码: 全选

推荐写法
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
上次由 gzbao9999 在 2010-08-16 15:09,总共编辑 7 次。
气血鼓荡,身体发胀,偶飘上头,三时舒畅
头像
Jarson
帖子: 2371
注册时间: 2008-07-21 9:44
来自: 深圳
联系:

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

#2

帖子 Jarson » 2010-08-09 19:23

:em11
头像
zhoucga
帖子: 246
注册时间: 2009-06-12 8:55

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

#3

帖子 zhoucga » 2010-08-09 19:46

:em11
青春不是人生的一段时期,而是心灵的一种状况...
头像
trigger
帖子: 1604
注册时间: 2006-10-25 18:08

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

#4

帖子 trigger » 2010-08-10 9:39

cut不能这样用。因为ip地址是不定长的。
可以cut两次
$ curl -s "http://checkip.dyndns.org/"|cut -f 6 -d" "|cut -f 1 -d"<"
楼主真是一派胡言,真可谓:“两个黄鹂鸣翠柳,不知所云;一行白鹭上青天,不知所止“。本来不想和你辩论,今天气愤不过,和你理论一番。我国宪法写得清清楚楚:“一夜夫妻百日恩,七楼以上才有电梯”。这个想必你知道,既然知道,你就不能断章取义,就算是天气预报,它还有不准的时候呢!!!再者说了,那中国银行也不是你一家开的。人家马拉多纳都结婚了,你还拿着粮票顶什么用呢。真是滑天下之大稽。前些日子,全国人大刚刚开过会,郑重声明:“中国不搞多party制,存栏母猪给补贴”。多好的事呢,楞让你这号人给搅混了。
gzbao9999
帖子: 627
注册时间: 2008-11-08 18:34

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

#5

帖子 gzbao9999 » 2010-08-10 13:57

trigger 写了:cut不能这样用。因为ip地址是不定长的。
说的对 确实是把ip地址的长度变化给忽略了
那就推荐 第1 第4种 和你这种写法 :em01
气血鼓荡,身体发胀,偶飘上头,三时舒畅
头像
link_01
帖子: 1024
注册时间: 2008-11-05 13:24

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

#6

帖子 link_01 » 2010-08-10 14:34

这东西还是找个好点的网址比较好.
http://submit.apnic.net/templates/yourip.html

代码: 全选

w3m -dump http://submit.apnic.net/templates/yourip.html | grep -P -o '(\d+\.){3}\d+'
笔记
-------------------------------------
http://blog.163.com/wqt_1101
头像
pityonline
帖子: 3864
注册时间: 2008-12-09 12:44
来自: 北京
联系:

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

#7

帖子 pityonline » 2010-08-11 0:56

太麻烦了……

代码: 全选

curl ifconfig.me
Pity is the bomp!
活着就是折腾!生命不息,折腾不止!
看这帮家伙在干什么?@pityonline/u
Dropbox+Vps+PC 跑起来了!这是邀请链接
gzbao9999
帖子: 627
注册时间: 2008-11-08 18:34

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

#8

帖子 gzbao9999 » 2010-08-11 9:13

pityonline 写了:太麻烦了……

代码: 全选

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

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

倒是 http://ifconfig.me/ip 这个的地址返回的是纯ip
气血鼓荡,身体发胀,偶飘上头,三时舒畅
头像
pityonline
帖子: 3864
注册时间: 2008-12-09 12:44
来自: 北京
联系:

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

#9

帖子 pityonline » 2010-08-11 9:47

gzbao9999 写了:
pityonline 写了:太麻烦了……

代码: 全选

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

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

倒是 http://ifconfig.me/ip 这个的地址返回的是纯ip
网站上有使用curl返回的记录,估计是判断 user agent 而返回不同信息吧……
Pity is the bomp!
活着就是折腾!生命不息,折腾不止!
看这帮家伙在干什么?@pityonline/u
Dropbox+Vps+PC 跑起来了!这是邀请链接
头像
roylez
帖子: 1928
注册时间: 2005-10-04 10:59
来自: 上海

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

#10

帖子 roylez » 2010-08-15 16:23

curl icanhazip.com
弄个dropbox空间来备份文件或者做私人代码服务器
配置:[url]git://github.com/roylez/dotfiles.git[/url]
主页:http://roylez.heroku.com
各种稀奇玩意儿:http://dooloo.info
回复