是公网IP地址吗?
我想知道是否有任何简单的方法来检查IP地址是公开的还是私有的。
更具体地说。例如,我知道127.0.0.1是指向同一台机器的私有IP地址,255.255.255.255如果用于广播到同一网络,192.168.1.0用于本地网络地址等等。但是我如何区分给定的 IP 地址是否不是私有 IP 之一并且可以公开访问?
I would like to know whether there is any easy method to check whether an IP address is publicly accessible or is private.
More specifically. I know for example, that 127.0.0.1 is a private IP address that point into the same machine, 255.255.255.255 if for broadcasting into the same network, 192.168.1.0 is for local network addresses and so on. But how can I distinguish whether a given IP address is not one of the private IP and is publicly accessible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
http://en.wikipedia.org/wiki/Private_network 列出了各种范围。只需构造一个 if 语句即可。
http://en.wikipedia.org/wiki/Private_network lists the various ranges. Just construct an if statement.
公共/私有 IPv4 地址在 RFC 5735 中定义。简而言之:
对于 IPv6,请参阅 RFC 5165。简而言之
。请注意,在端口转发或其他防火墙规则的帮助下,没有公共 IP 的计算机上的服务仍然可以从 Internet 访问。
Pulic/private IPv4 addresses are defined in RFC 5735. In short:
For IPv6, refer to RFC 5165. In short:
Note that services on machines without a public IP may still be reachable from the internet with the help of port forwarding or other firewall rules.
Ed Heal 提到了一种解决方案,但还有另一种解决方案:
只需连接到某个外部主机并询问它所看到的 IP,就像这样(PHP 的示例):
我知道这个特定示例将返回仅包含 IP 的单个字符串地址。我不知道其他服务提供这种服务,尽管可能有很多。上述脚本/服务的主页是:http://ip.42.pl/。
如果您知道类似的服务,请在评论中发布它们的 URL,以便未来的读者有其他选择。
One solution is mentioned by Ed Heal, but there is another one:
Just connect to some external host and ask it for the IP it sees, like that (example for PHP):
This specific example I know will return single string containing only an IP address. I do not know other services offering this, although there are probably plenty of them. The main page of the above script / service is: http://ip.42.pl/.
If you know similar services, please post their URLs in the comments, so future readers have other options.