Created
November 21, 2017 06:44
-
-
Save danielgindi/0cc7c4063684cbf32de40f04120299e7 to your computer and use it in GitHub Desktop.
Generate a list of the common-denominator CIDR IP ranges from a list of IPs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getLowestIpRanges(ips) { | |
let ipRanges = []; | |
ips.forEach(x => { | |
let x3 = x.replace(/\d+$/, ''); | |
let x2 = x3.replace(/\d+\.?$/, ''); | |
let x1 = x2.replace(/\d+\.?$/, ''); | |
let ex = ipRanges.filter(y => y == x3 + '0' || y == x2 + '0.0' || y == x1 + '0.0.0'); | |
if (ex.length) return; | |
ex = ipRanges.filter(y => y.startsWith(x2)); | |
if (ex.length) { | |
ipRanges.splice(ipRanges.indexOf(x[0]), 1) | |
ipRanges.push(x2 + '0.0') | |
} else { | |
ex = ipRanges.filter(y => y.startsWith(x1)); | |
if (ex.length) { | |
ipRanges.splice(ipRanges.indexOf(x[0]), 1) | |
ipRanges.push(x1 + '0.0.0') | |
} else { | |
ipRanges.push(x3 + '0') | |
} | |
} | |
}); | |
ipRanges = ipRanges.map(x => x.endsWith('.0.0.0') ? (x + '/8') : (x.endsWith('.0.0') ? (x + '/16') : (x + '/24'))); | |
return ipRanges; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment