linux獲取外網ip

  引言:目前獲取ip的方法中,ifconfig和ip獲取函數獲得的都是內網ip。有時候須要獲取外網ip,目前通用的作法,是向外部服務器發送請求,解析外部服務器響應,從而獲得的本身的外網ip。linux下的 curl能夠替咱們完成這些工做,固然,不怕麻煩的話,能夠本身分析http協議,本身實現以上過程。若是熟悉python的話,那就更簡單了,就像咱們所知道的,python老是有現成的庫函數可供咱們調用。一下總結幾種獲取外網ip的方法,以供查詢,資料來源互聯網。css

參看資料:html

http://www.cnblogs.com/nhlinkin/p/3535214.htmlpython

http://www.2cto.com/os/201302/190269.htmllinux

 

$ 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'bash

$ curl http://ip.3322.net服務器

$ curl icanhazip.comapp

 


方法一: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。
相關文章
相關標籤/搜索