获取我的 WAN IP 地址
我怎样才能以编程方式获取从互联网上看到的我的网络的IP地址? 显然,我的路由器在连接到 ISP 时可以访问该属性。 有没有办法使用标准协议从路由器获取此信息。 我唯一的选择是找到一个返回我的 IP 地址的 WS(非常难做到),或者直接访问 之类的东西whatismyip.com 并删除所有 HTML(非常肮脏且容易更改)。 还有别的办法吗???
How can i go about programaticaly getting the IP address of my network as seen from the Internet? Its obviously a property that my router has access to when it connects to the 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). Is there any other way???
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不要抓取 Whatismyip.com,请参阅此处了解如何调用仅返回您的地址的 API。
如果您不使用它,则必须自己编写类似的内容,即路由器之外的主机可以报告您的明显地址。
请注意,网络服务器可能看不到您真实的 WAN IP 地址,因为:
您的 ISP 可能是透明的
代理 HTTP 流量,服务器将看到该代理的 IP
代理人。 在这种情况下,您通常会
需要寻找并解析
X-Forwarded-For 标头。
或者,正如 Olaf 指出的,您和开放的互联网之间可能存在另一个 NAT 路由器,换句话说,您的路由器的 WAN 地址位于专用网络上。 您从 Whatismyip.com 等服务中获得的最好结果是最外层 NAT 路由器的 IP。
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.
如果您的路由器支持 snmp,您可以使用它来询问其外部 IP。 这里有一个小例子:
http://www.rohitab.com/discuss /index.php?showtopic=31901
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
我对 Paul Dixons 的答案投了赞成票,因为它看起来很完整,但还有一个方面:
这些方面并不是典型的日常情况,但根据您的需求,您可能需要考虑这些。
I've voted up Paul Dixons answer because it seemed complete, but there's one more aspect to this:
These aspects are not the typical day to day situation, but depending on your needs you might want to take these into account.
我没有看到语言规范,但我在 python 中做到了: 在Python中查找面向公众的IP地址?
基本上没有办法在不依赖外部服务器的情况下做到这一点,在本例中我使用http://www.whatismyip.com/automation/n09230945.asp 仅提供 IP 地址。
或者,您可以使用自己的脚本,在 PHP 中如下所示:
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: