如何检查给定的IP是否为内部IP ?
How do you check whether the given IP is internal or not
如何检查给定的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地址
相关文章:
- 访问布局信息是否也会导致浏览器重排
- 当包含另一个asp文件时,是否也包含所有引用的样式和脚本页面
- 如何检测是否有溢出
- jQuery中是否内置了任何字符串格式化函数
- 是否有任何snippet或jQuery插件可以列出easylist.txt模式匹配的DOM中的所有元素
- 测试索引值是否等于某个数字的倍数
- 除了用于跟踪的IP地址之外,是否有任何计算机特定信息
- jQuery+检查数组是否只包含 IP 地址
- 如何检查两个不同的页面或网址是否从相同的浏览器和 ip 触发
- 是否可以将访问者的IP和位置写入文件
- 使用 JavaScript 获取 IP 地址是否与通过 PHP 获取IP地址一样准确
- 是否可以记录用户'的IP地址
- 检查给定的网址是否阻止了访问者的ip
- jquery检查是否每500ms从ip加载一次流
- 是否可以使用本地TCP/IP连接作为两个脚本之间标准I/O的替代
- 是否有一种方法可以防止某些IP连接到我的Node.js ('net')服务器
- 如何知道一个ip地址是否查看了所有的链接
- 是否有一种方法可以在上线时自动将localhost更改为ip地址?
- 是否有一个node.js模块可以确定IP的地理位置
- 如何检查给定的IP是否为内部IP ?