I have a long list of IP addresses that show some pattern of closeness
Example:
XX.249.91.16
XX.249.91.21
XX.249.91.32
XX.249.91.160
XX.249.91.165
XX.249.92.15
XX.249.92.25
XX.249.92.51
XX.249.92.234
and sometimes a whois reveals a range like this
XX.163.224.0 - XX.163.239.255
I would like to create a list of CIDR that gives me the lowest common number
It seems Python already has such a thing - iprange_to_cidrs - but I need it in JavaScript
XX.249.90.0/16
if that is what would handle
XX.249.91.0 - XX.249.92.255
ditto XX.163.200.0/nn to handle
XX.163.224.0 - XX.163.239.255
A calculator that can do it one range at a time when I fiddle with the mask is here http://www.subnet-calculator.com/cidr.php
My preferred language is JavaScript and I have the following start but need the algorithm that does the match and transform
var list = [
"10.249.91.16",
"10.249.91.21",
"10.249.91.32",
"10.249.91.160",
"10.249.91.165",
"10.249.92.15",
"10.249.92.25",
"10.249.92.51",
"10.249.92.234"
]
function dec2bin(ip) {
return ip.map(function(num) {
return ("00000000"+parseInt(num,10).toString(2)).slice(-8);
}).join(".");
}
const result = list.map(ip => dec2bin(ip));
UPDATE after reviewing an answer
const getDec = ip => {
let octet = ip.split(".");
return (octet[0] << 24) | (octet[1] << 16) | (octet[2] << 8) | octet[3];
};
var adr1 = "111.163.224.0";
var adr2 = "111.163.239.255"
var XOR = getDec(adr1)^getDec(adr2);
// now what?
Update 2:
could the result of the range 111.163.224.0 - 111.163.239.255 be 111.163.224.0/20
? since we have
01101111.10100011.11100000.00000000 - 0,111.163.224.0
01101111.10100011.11101111.11111111 - 0,111.163.239.255