简体   繁体   English

获取我的WAN IP地址

[英]Get my WAN IP address

How can i go about programaticaly getting the IP address of my network as seen from the Internet? 我如何从互联网上看programaticaly获取我的网络的IP地址? Its obviously a property that my router has access to when it connects to the ISP. 它显然是我的路由器连接到ISP时可以访问的属性。 Is there any way to get this info from a router using a standard protocol. 有没有办法从使用标准协议的路由器获取此信息。 My only other option is to either find a WS which returns my IP address (suprisingly difficult to do), or just go to something like whatismyip.com and strip out all the HTML (very dirty and susceptable to change). 我唯一的另一个选择是找到一个返回我的IP地址的WS(令人惊讶的难以做到),或者只是去找whatismyip.com这样的东西并删除所有的HTML(非常脏和易于改变)。 Is there any other way??? 有没有其他方法???

Don't scrape whatismyip.com, see here for how you can call their API which just returns your address. 不要刮不到whatismyip.com,请看这里如何调用他们的API,只返回你的地址。

If you don't use this, you have to write something like it yourself, ie a host beyond your router which can report back your apparent address. 如果您不使用它,您必须自己编写类似的东西,即路由器之外的主机,它可以报告您的明显地址。

Note that webserver might not see your real WAN IP address because: 请注意,Web服务器可能看不到您的真实WAN IP地址,因为:

  • your ISP might be transparently proxying HTTP traffic, and the server would see the IP of the proxy. 您的ISP可能透明地代理HTTP流量,服务器将看到代理的IP。 In that case, you'd typically need to look for and parse a X-Forwarded-For header. 在这种情况下,您通常需要查找并解析X-Forwarded-For标头。

  • 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. 或者,奥拉夫指出,有可能是你和广泛开放的互联网之间的另一个NAT路由器,换句话说, 你的路由器的WAN地址是一个专用网络上。 The best you'd get from a service like whatismyip.com is the IP of the outermost NAT router. 从whatismyip.com这样的服务中获得的最好的是最外层NAT路由器的IP。

If your router supports snmp you could use that to ask it about it's external ip. 如果您的路由器支持snmp,您可以使用它来询问它的外部IP。 A small example is found here: 这里有一个小例子:

http://www.rohitab.com/discuss/index.php?showtopic=31901 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: 我已经投了Paul Dixons的答案,因为它看似完整,但还有一个方面:

  • Your ISP might provide private addresses for you - this does happen with some ISPs. 您的ISP可能会为您提供私人地址 - 某些ISP 发生这种情况。 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 如果您自己拥有代理,Paul提到的代理信息(HTTP-Header X-Forwarded-For)可能是不可路由的地址
  • based on mixing all these aspects (getting a nonroutable address from your ISP and having a proxy yourself) you might get bizarre results. 基于混合所有这些方面(从您的ISP获得不可路由的地址并自己拥有代理),您可能会得到奇怪的结果。

These aspects are not the typical day to day situation, but depending on your needs you might want to take these into account. 这些方面并不是典型的日常情况,但根据您的需求,您可能需要考虑这些因素。

I don't see a language specifacation but I did it here in python: Finding a public facing IP address in Python? 我没有看到语言规范,但我在python中这样做:在Python中查找面向公众的IP地址?
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. 基本上没有办法不依赖外部服务器,在这种情况下我使用http://www.whatismyip.com/automation/n09230945.asp只提供IP地址。 Alternatively you could use your own script which in PHP would look like: 或者,您可以使用自己的脚本,在PHP中看起来像:

<?php
echo $_SERVER['REMOTE_ADDR'];
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM