douwuying4709
2012-01-11 11:59
浏览 47
已采纳

是公共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?

图片转代码服务由CSDN问答提供 功能建议

我想知道是否有任何简单的方法可以检查IP地址是公开访问还是私有。

更具体地说。 我知道例如,127.0.0.1是指向同一台机器的私有IP地址,如果用于广播到同一网络则为255.255.255.255,192.168.1.0用于本地网络地址,依此类推。 但是,如何区分给定的IP地址是否属于私有IP并且是否可公开访问?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • dongxian1921 2012-01-11 12:03
    已采纳

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

    已采纳该答案
    打赏 评论
  • dsfdsf21312 2012-01-11 12:06

    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.

    打赏 评论
  • doulan1073 2012-01-11 12:06

    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.

    打赏 评论
  • dongxian8048 2012-11-02 22:41
    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;
    }
    
    打赏 评论

相关推荐 更多相似问题