获取我的 WAN IP 地址

发布于 2024-07-06 14:28:28 字数 251 浏览 9 评论 0原文

我怎样才能以编程方式获取从互联网上看到的我的网络的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

赢得她心 2024-07-13 14:28:28

不要抓取 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.

许一世地老天荒 2024-07-13 14:28:28

如果您的路由器支持 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

往昔成烟 2024-07-13 14:28:28

我对 Paul Dixons 的答案投了赞成票,因为它看起来很完整,但还有一个方面:

  • 您的 ISP 可能会为您提供私人地址 - 这确实发生在某些 ISP 上。 根据您的期望,您可能需要一个您没有的可路由地址
  • 的代理,Paul提到的代理信息(HTTP-Header X-Forwarded-For)可能是一个不可路由的地址
  • 如果您自己有一个基于混合所有内容 在这些方面(从 ISP 获取不可路由的地址并自己拥有代理)您可能会得到奇怪的结果。

这些方面并不是典型的日常情况,但根据您的需求,您可能需要考虑这些。

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.

蓝海 2024-07-13 14:28:28

我没有看到语言规范,但我在 python 中做到了: 在Python中查找面向公众的IP地址?
基本上没有办法在不依赖外部服务器的情况下做到这一点,在本例中我使用http://www.whatismyip.com/automation/n09230945.asp 仅提供 IP 地址。
或者,您可以使用自己的脚本,在 PHP 中如下所示:

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

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'];
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文