如何检查给定的IP是否为内部IP ?

How do you check whether the given IP is internal or not

本文关键字:IP 是否 内部 何检查 检查      更新时间:2023-09-26

如何检查给定的IP是内部的还是不使用javascript?

例如,如果给你一个192.168.1.1的IP,脚本应该验证它,并警告这是一个内部或外部的IP。

如果你的意思是private,请确保它在以下范围之一:

私网IP地址范围

可用IP的范围和数量如下:

10.0.0.0 - 10.255.255.255地址:16,777,216

172.16.0.0 - 172.31.255.255地址:1,048,576

192.168.0.0 - 192.168.255.255地址:65,536

这样的函数应该有帮助:

function isPrivateIP(ip) {
   var parts = ip.split('.');
   return parts[0] === '10' || 
      (parts[0] === '172' && (parseInt(parts[1], 10) >= 16 && parseInt(parts[1], 10) <= 31)) || 
      (parts[0] === '192' && parts[1] === '168');
}

您可以使用ipaddr.js库并检查它是否返回"private":

const ipaddrJs = require('ipaddr.js');
ipaddrJs.parse('192.168.5.1').range()
> 'private'
https://github.com/whitequark/ipaddr.js/blob/master/lib/ipaddr.js

如果你使用Node.js,看看这里:https://www.npmjs.com/package/ipaddr.js

内部ip如下:

10.0.0.0 - 10.255.255.255
172.16.0.0 - 172.31.255.255
192.168.0.0 - 192.168.255.255

为它们写出正确的正则表达式。

/10'.'d+'.'d+'.'d+/
/192'.168'.'d+'.'d+/

我把它留给你去找出172的正确正则表达式。xxx。

试试这个!

  var id = "10.10.10.10";
  if (/^(10)'.(.*)'.(.*)'.(.*)$/.test(id)){
    //10.x.x.x
  }else if (/^(172)'.(1[6-9]|2[0-9]|3[0-1])'.(.*)'.(.*)$/.test(id)){
    //172.16.x.x - 172.31.255.255
  }else if (/^(192)'.(168)'.(.*)'.(.*)$/.test(id)){
    //192.168.x.x
  }else {
    //else
  }

要包含IPv6地址并使用一个功能检查任何IP地址,您可以使用以下命令:

    var net = require('net') // requires node.js
    ipIsPrivate(ip) {
        if (ip.substring(0,7) === "::ffff:")
            ip = ip.substring(7);
        if (net.isIPv4(ip)) {
            //         10.0.0.0 - 10.255.255.255        ||   172.16.0.0 - 172.31.255.255                          ||    192.168.0.0 - 192.168.255.255
            return  /^(10)'.(.*)'.(.*)'.(.*)$/.test(ip) || /^(172)'.(1[6-9]|2[0-9]|3[0-1])'.(.*)'.(.*)$/.test(ip) || /^(192)'.(168)'.(.*)'.(.*)$/.test(ip)
        }
        
        // else: ip is IPv6
        const firstWord = ip.split(":").find(el => !!el); //get first not empty word
            
        // The original IPv6 Site Local addresses (fec0::/10) are deprecated. Range: fec0 - feff
        if (/^fe[c-f][0-f]$/.test(firstWord))
            return true;
        // These days Unique Local Addresses (ULA) are used in place of Site Local.
        // Range: fc00 - fcff
        else if (/^fc[0-f]{2}$/.test(firstWord))
            return true;
            
        // Range: fd00 - fcff
        else if (/^fd[0-f]{2}$/.test(firstWord))
            return true;
        // Link local addresses (prefixed with fe80) are not routable
        else if (firstWord === "fe80")
            return true;
        
        // Discard Prefix
        else if (firstWord === "100")
            return true;
            
        // Any other IP address is not Unique Local Address (ULA)
        return false;
    }

如果检查包括本地主机(127.0.0.1, ::ffff:127.0.0.1::1)使用:

    var net = require('net') // requires node.js
    ipIsPrivateOrLocalhost(ip) {
        if (ip.substring(0,7) === "::ffff:")
            ip = ip.substring(7);
        if (net.isIPv4(ip)) {
            // check localhost
            if (ip === '127.0.0.1')
                return true;
            //         10.0.0.0 - 10.255.255.255        ||   172.16.0.0 - 172.31.255.255                          ||    192.168.0.0 - 192.168.255.255
            return  /^(10)'.(.*)'.(.*)'.(.*)$/.test(ip) || /^(172)'.(1[6-9]|2[0-9]|3[0-1])'.(.*)'.(.*)$/.test(ip) || /^(192)'.(168)'.(.*)'.(.*)$/.test(ip)
        }
        
        // else: ip is IPv6
        const firstWord = ip.split(":").find(el => !!el); //get first not empty word
        // equivalent of 127.0.0.1 in IPv6
        if (ip === "::1")
            return true;
            
        // The original IPv6 Site Local addresses (fec0::/10) are deprecated. Range: fec0 - feff
        else if (/^fe[c-f][0-f]$/.test(firstWord))
            return true;
        // These days Unique Local Addresses (ULA) are used in place of Site Local.
        // Range: fc00 - fcff
        else if (/^fc[0-f]{2}$/.test(firstWord))
            return true;
            
        // Range: fd00 - fcff
        else if (/^fd[0-f]{2}$/.test(firstWord))
            return true;
        // Link local addresses (prefixed with fe80) are not routable
        else if (firstWord === "fe80")
            return true;
        
        // Discard Prefix
        else if (firstWord === "100")
            return true;
            
        // Any other IP address is not Unique Local Address (ULA)
        return false;
    }

基于Minko Gechev答案和这个实现来检查c#中的私有IPv6地址