引言:目前獲取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.
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。