首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何根据nodejs中的第一个八进制过滤IP地址并将它们分组到不同的列表中

如何根据nodejs中的第一个八进制过滤IP地址并将它们分组到不同的列表中
EN

Stack Overflow用户
提问于 2019-12-17 14:14:30
回答 1查看 504关注 0票数 0

有一个IP地址列表,原始= "1.2.3.4“、"1.2.4.1”、"1.3.45.67“、"1.3.67.89”。这个列表有1000 s的IP地址。我想要创建与前两个八位数匹配的组。Group1 = "1.2.3.4“、"1.2.4.1”Group2 = "1.3.45.67“、"1.3.67.89”如何在nodejs中实现?

下面的代码可以工作,但是想知道更简单的方法来实现同样的.

代码语言:javascript
运行
复制
let initArray=['172.25.17.42','172.21.17.5','172.22.45.2','172.22.66.5','172.25.5.2','172.21.8.27','172.25.13.30','172.21.14.13','172.21.17.5','172.05.17.42','172.06.17.5','172.07.45.2','172.06.66.5','172.05.5.2','172.07.8.27','172.05.13.30','172.06.14.13','172.07.17.5','172.11.17.42','172.12.17.5','172.11.45.2','172.12.66.5','172.33.5.2','172.40.8.27','172.33.13.30','172.40.14.13','172.50.17.5'];
initArray.sort();

var resArray = [];
initArray.forEach(function (eachIP) {
 let ocTets = eachIP.split('.');
let twoOctets = ocTets[0]+'.'+ocTets[1];
resArray.push(twoOctets);
})
const counts = {};
 for (var i = 0; i < resArray.length; i++) {
var num = resArray[i];//num = 171.21
counts[num] = counts[num] ? counts[num] + 1 : 1;//counts[171.21  = 1]
}

let sum = 0;
var newMap = new Map(Object.entries(counts));
var resMap = new Map();
console.log(newMap);
let keyarray = [];
let j=0;

newMap.forEach((value, key, thisMap) => {
    sum+=value;
    if(sum<=5){
        keyarray.push(key);
    }
    else
    {
        keyarray.push(key);
        console.log('keyarray:'+keyarray);
        let keyStr = keyarray.toString();
        resMap.set(keyStr, sum);
        while (keyarray.length) { keyarray.pop(); }
        sum = 0;
    }
})

console.log(resMap)
EN

Stack Overflow用户

发布于 2019-12-18 19:35:54

ipv4 IP地址仅为32位数字.虽然人们可以方便地将IP看作一个四重组字符串,但是使用数字编写IP逻辑通常更容易。例如,在代码中对is进行排序,但是is是按字母数字排序的,这可能不是您想要的。因此,一种方法是将所有IP地址映射到数字,然后应用分组逻辑。

以IP地址172.25.17.42172.25.5.2为例。这些分别映射到0xAC19112A0xAC190502的十六进制。您可以立即看到前两个八位数匹配(0xAC19)。

给定IP的数字表示,只需将每个数字右移16位就可以得到所需的分组。或者,您可以屏蔽所需的两个八进制以外的位(请记住,JavaScript数字是64位)。

我会遵循这个食谱:

octets.

  • Reduce

  • 将IP转换为数字。

  • 将每个数字右移16位,以获得前两个IP列表,即IP的列表,降至一个从八位到IP的映射/所需格式(例如,八位组以计数或其他方式)。

这是一些密码。

代码语言:javascript
运行
复制
const ips = ['172.25.17.42', '172.21.17.5', '172.22.45.2', '172.22.66.5',
  '172.25.5.2', '172.21.8.27', '172.25.13.30', '172.21.14.13', '172.21.17.5',
  '172.05.17.42', '172.06.17.5', '172.07.45.2', '172.06.66.5', '172.05.5.2',
  '172.07.8.27', '172.05.13.30', '172.06.14.13', '172.07.17.5', '172.11.17.42',
  '172.12.17.5', '172.11.45.2', '172.12.66.5', '172.33.5.2', '172.40.8.27',
  '172.33.13.30', '172.40.14.13', '172.50.17.5'];

// Reduce the array of IPs down to a map of first-two-octets to IP.
const octetMap = ips
  .reduce((map, ip) => {
    // The first two octets of the IP as a number.
    const firstTwo = ip2int(ip) >> 16;

    // Alternative approach (JS numbers are 64 bit).
    //const firstTwo = ip2int(ip) & 0x00FF0000;

    // First time seeing firstTwo initialize an array, then add the IP.
    if (!map.has(firstTwo))
      map.set(firstTwo, []);
    map.get(firstTwo).push(ip);

    return map;
  }, new Map());

console.log(octetMap);

// This returns an unsigned 32-bit integer representation of the ip string.
function ip2int(ip) {
  return ip
    .split('.')
    .reduce((num, oct) => (num << 8) + parseInt(oct, 10), 0) >>> 0;
}
票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59376122

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档