Get my WAN IP address

我怕爱的太早我们不能终老 提交于 2019-12-03 20:44:43

Don't scrape whatismyip.com, see here for how you can call their API which just returns your address.

If you don't use this, you have to write something like it yourself, i.e. a host beyond your router which can report back your apparent address.

Note that webserver might not see your real WAN IP address because:

  • your ISP might be transparently proxying HTTP traffic, and the server would see the IP of the proxy. In that case, you'd typically need to look for and parse a X-Forwarded-For header.

  • or, as Olaf noted, there may be another NAT router between you and the wide open Internet, in other words, the WAN address of your router is on a private network. The best you'd get from a service like whatismyip.com is the IP of the outermost NAT router.

If your router supports snmp you could use that to ask it about it's external ip. A small example is found here:

http://www.rohitab.com/discuss/index.php?showtopic=31901

I've voted up Paul Dixons answer because it seemed complete, but there's one more aspect to this:

  • Your ISP might provide private addresses for you - this does happen with some ISPs. Depending on what you expect you might need a routable address that you don't have
  • The proxy information that Paul mentions (HTTP-Header X-Forwarded-For) might be a non-routable address if you yourself have a proxy
  • based on mixing all these aspects (getting a nonroutable address from your ISP and having a proxy yourself) you might get bizarre results.

These aspects are not the typical day to day situation, but depending on your needs you might want to take these into account.

UnkwnTech

I don't see a language specifacation but I did it here in python: Finding a public facing IP address in Python?
Basicly there is no way of doing it without relying on an external server, in this case I use http://www.whatismyip.com/automation/n09230945.asp which only provide the ip address. Alternatively you could use your own script which in PHP would look like:

<?php
echo $_SERVER['REMOTE_ADDR'];
?>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!