我想基于已经提供给我的一组IP来运行某个脚本。
我下面的脚本工作得很好,但它看起来很草率,并且像我想要完成的代码太多了。
我简化了IP地址的数量,以免弄乱屏幕。
<script>
//Initialize the array
//HostMin: 196.145.179.129 HostMax: 196.145.179.130
var $ipArray = ["196.145.179.129","196.145.179.130"];
//HostMin: 50.207.77.201 HostMax: 50.207.77.204
$ipArray.push("50.207.77.201","50.207.77.202","50.207.77.203","50.207.77.204");
//HostMin: 57.179.277.209 HostMax: 57.179.277.214
$ipArray.push("57.179.277.209","57.179.277.210","57.179.277.211","57.179.277.212","57.179.277.213","57.175.277.214");
//HostMin: 74.97.164.65 HostMax: 74.97.164.66
$ipArray.push("74.97.164.65","74.97.164.66");
$ipAddr = "74.97.164.65";
if ($.inArray($ipAddr, $ipArray ) >= 0) {
//Do Something
}else{
//Do Something else
}
</script>
我确实有一些信息,例如IP过滤器 - 50.207.77.201/30
,如果这有所不同。
任何重写这方面的帮助都将不胜感激。
答案 0 :(得分:0)
我不是一个聪明的人,并发布了PHP回复。由于这个帖子中的其他人提到了我的答案,我不会删除它。理论上你可以在Javascript中使用this function和我的答案的基本逻辑:
function ip2long (IP) {
// http://kevin.vanzonneveld.net
// + original by: Waldo Malqui Silva
// + improved by: Victor
// + revised by: fearphage (http://http/my.opera.com/fearphage/)
// + revised by: Theriault
// * example 1: ip2long('192.0.34.166');
// * returns 1: 3221234342
// * example 2: ip2long('0.0xABCDEF');
// * returns 2: 11259375
// * example 3: ip2long('255.255.255.256');
// * returns 3: false
var i = 0;
// PHP allows decimal, octal, and hexadecimal IP components.
// PHP allows between 1 (e.g. 127) to 4 (e.g 127.0.0.1) components.
IP = IP.match(/^([1-9]\d*|0[0-7]*|0x[\da-f]+)(?:\.([1-9]\d*|0[0-7]*|0x[\da-f]+))?(?:\.([1-9]\d*|0[0-7]*|0x[\da-f]+))?(?:\.([1-9]\d*|0[0-7]*|0x[\da-f]+))?$/i); // Verify IP format.
if (!IP) {
return false; // Invalid format.
}
// Reuse IP variable for component counter.
IP[0] = 0;
for (i = 1; i < 5; i += 1) {
IP[0] += !! ((IP[i] || '').length);
IP[i] = parseInt(IP[i]) || 0;
}
// Continue to use IP for overflow values.
// PHP does not allow any component to overflow.
IP.push(256, 256, 256, 256);
// Recalculate overflow of last component supplied to make up for missing components.
IP[4 + IP[0]] *= Math.pow(256, 4 - IP[0]);
if (IP[1] >= IP[5] || IP[2] >= IP[6] || IP[3] >= IP[7] || IP[4] >= IP[8]) {
return false;
}
return IP[1] * (IP[0] === 1 || 16777216) + IP[2] * (IP[0] <= 2 || 65536) + IP[3] * (IP[0] <= 3 || 256) + IP[4] * 1;
}
希望有人比我聪明(低吧)会为你发布更好的答案。
我发现ip2long
function非常适合此用例
$low = ip2long($low_address);
$high = ip2long($high_address);
$user_ip = ip2long($_SERVER['REMOTE_ADDR']);
if($user_ip >= $low && $user_ip <= $high){
// do something
}
答案 1 :(得分:-1)
这个答案并没有完全回答OP问题,而是扩充了Rob M现有的答案。它提供了一种更简单,更快速的功能,可以完成大部分工作。
// Convert an IPv4 dotted quad address to unsigned 32-bit integer
// Return FALSE if IP address is not well formed
function ip2int(ip) {
var val = false, // Assume passed IP is invalid.
// Regex to validate and parse an IP (version 4) address.
re_valid_ipv4 = /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;
// Use String.replace() with callback to validate and parse in one whack.
ip.replace(re_valid_ipv4,
function(m0,m1,m2,m3,m4){
val = ((m1 << 24) + (m2 << 16) + (m3 << 8) + (+m4)) >>> 0;
return '';
});
return val;
}
答案 2 :(得分:-2)
我发现iplib.js可能对您来说非常完美。
在他们的usage wiki中,他们谈论通过网络进行迭代(通过使用子网掩码,而不是通过实际与网络交互),我认为它将完全满足您的需求。
var ip = new IPv4("192.168.0.100");
var subnet = new Subnet("/24");
var ipnet = new IPv4Network(ip,subnet);
var network = ipnet.network(); //IPv4("192.168.0.0")
var broadcast = ipnet.broadcast(); //IPv4("192.168.255.255");
var stopIP = new IPv4("192.168.0.10");
range_ips = [];
ipnet.iter(function(ip,index) {
range_ips.push(ip);
if(ip.equals(stopIP)) return false;
});
//check IPs are in a network
var is_contained = ipnet.contains(ip) //true;
var total = ipnet.count(); //254