是公网IP地址吗?

发布于 2024-12-26 04:18:43 字数 170 浏览 2 评论 0原文

我想知道是否有任何简单的方法来检查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 技术交流群。

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

发布评论

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

评论(4

離人涙 2025-01-02 04:18:43

http://en.wikipedia.org/wiki/Private_network 列出了各种范围。只需构造一个 if 语句即可。

http://en.wikipedia.org/wiki/Private_network lists the various ranges. Just construct an if statement.

撞了怀 2025-01-02 04:18:43

公共/私有 IPv4 地址在 RFC 5735 中定义。简而言之:

  • 0.0.0.0/8 无效
  • 127.0.0.0/8 是 localhost
  • 169.254.0.0/16 是未配置的 IP。您应该将其视为本地地址
  • 10.0.0.0/8、172.16.0.0/12、192.168.0.0/16 是专用网络
  • 224.0.0.0/4 是多播
  • 其他所有内容均可公开访问或保留

对于 IPv6,请参阅 RFC 5165。简而言之

  • :::/128 是未指定的地址,::1/128 是 localhost
  • ::ffff:0:0/96 是 IPv4 映射地址
  • fe80::/10 和 fc00::/7 是专用网络
  • ff00:: /8 是多播的
  • ,其他所有内容都是可公开访问的,或保留的

。请注意,在端口转发或其他防火墙规则的帮助下,没有公共 IP 的计算机上的服务仍然可以从 Internet 访问。

Pulic/private IPv4 addresses are defined in RFC 5735. In short:

  • 0.0.0.0/8 is invalid
  • 127.0.0.0/8 is localhost
  • 169.254.0.0/16 is an unconfigured IP. You should treat it like a local address
  • 10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16 are private networks
  • 224.0.0.0/4 is multicast
  • Everything else is publicly reachable, or reserved

For IPv6, refer to RFC 5165. In short:

  • ::/128 is the unspecified address, ::1/128 is localhost
  • ::ffff:0:0/96 are IPv4-mapped addresses
  • fe80::/10 and fc00::/7 are private networks
  • ff00::/8 is multicast
  • Everything else is publicly reachable, or reserved

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.

原谅过去的我 2025-01-02 04:18:43
function validateIpAddress($ip_addr)
{
    $result = true;
    if(preg_match("/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/",$ip_addr))
    {
        $parts=explode(".",$ip_addr);
        foreach($parts as $ip_parts)
        {
            if(intval($ip_parts)>255 || intval($ip_parts)<0)
                $result=false;
        }
        if (intval($parts[0])==0 || intval($parts[0])==10 || intval($parts[0])==127 || (intval($parts[0])>223 && intval($parts[0])<240))
        {
            $result=false;
        }
        if ((intval($parts[0])==192 && intval($parts[1])==168) || (intval($parts[0])==169 && intval($parts[1])==254))
        {
            $result=false;
        }

        if (intval($parts[0])==172 && intval($parts[1])>15 && intval($parts[1])<32 )
        {
            $result=false;
        }
    }
    else
    {
        $result = false; //if format of ip address doesn't matches 
    }
    return $result;
}
function validateIpAddress($ip_addr)
{
    $result = true;
    if(preg_match("/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/",$ip_addr))
    {
        $parts=explode(".",$ip_addr);
        foreach($parts as $ip_parts)
        {
            if(intval($ip_parts)>255 || intval($ip_parts)<0)
                $result=false;
        }
        if (intval($parts[0])==0 || intval($parts[0])==10 || intval($parts[0])==127 || (intval($parts[0])>223 && intval($parts[0])<240))
        {
            $result=false;
        }
        if ((intval($parts[0])==192 && intval($parts[1])==168) || (intval($parts[0])==169 && intval($parts[1])==254))
        {
            $result=false;
        }

        if (intval($parts[0])==172 && intval($parts[1])>15 && intval($parts[1])<32 )
        {
            $result=false;
        }
    }
    else
    {
        $result = false; //if format of ip address doesn't matches 
    }
    return $result;
}
大姐,你呐 2025-01-02 04:18:43

Ed Heal 提到了一种解决方案,但还有另一种解决方案:

只需连接到某个外部主机并询问它所看到的 IP,就像这样(PHP 的示例):

$my_public_ip = file_get_contents('http://ip.42.pl/raw');

我知道这个特定示例将返回仅包含 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):

$my_public_ip = file_get_contents('http://ip.42.pl/raw');

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.

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