您的位置:首页 > 运维架构 > Linux

linux获取外网ip

2015-02-10 21:12 190 查看
  引言:目前获取ip的方法中,ifconfig和ip获取函数得到的都是内网ip。有时候需要获取外网ip,目前通用的做法,是向外部服务器发送请求,解析外部服务器响应,从而得到的自己的外网ip。linux下的 curl可以替我们完成这些工作,当然,不怕麻烦的话,可以自己分析http协议,自己实现以上过程。如果熟悉python的话,那就更简单了,就像我们所知道的,python总是有现成的库函数可供我们调用。一下总结几种获取外网ip的方法,以供查询,资料来源互联网。

参看资料:
http://www.cnblogs.com/nhlinkin/p/3535214.html http://www.2cto.com/os/201302/190269.html
$ curl ifconfig.me
$ curl icanhazip.com
$ curl ident.me
$ curl ipecho.net/plain
$ curl whatismyip.akamai.com
$ curl tnx.nl/ip
$ curl myip.dnsomatic.com
$ curl ip.appspot.com
$ curl -s checkip.dyndns.org | sed 's/.*IP Address: \([0-9\.]*\).*/\1/g'

$ curl http://ip.3322.net
$ curl icanhazip.com

方法一:curl -s "http://checkip.dyndns.org/"|cut -f 6 -d" "|cut -f 1 -d"<" www.2cto.com
方法二:w3m -dump http://submit.apnic.net/templates/yourip. href="http://www.2cto.com/kf/qianduan/css/" target=_blank>html | grep -P -o '(\d+\.){3}\d+'
方法三:curl ifconfig.me

方法四:curl icanhazip.com
方法五:curl -s www.123cha.com | grep -o "[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}" | head -n 1

把它们添加到 .bashrc (Bash 专用) 或者 .profile (非 Bash 专用)里面去

alias myip='curl ifconfig.me'

source .bashrc 或者 . .profile 即可生效

输入 myip 就能拿到 WAN IP。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: