I'm trying to generate a random IP address given a subnet of IP address. There are plenty of resources available to generate a random IP, but my requirement it to have it generated from within a specific subnet.
I've used an npm module called netmask - however the implementation is absolutely not elegant. Can anyone please give some slick pointers to this?
var netmask = require("netmask").Netmask
var block = new netmask('10.0.0.0/24')
console.log(block) // gives block details
var blockSize = block.size - 1 ;
var randomIndex = Math.floor(Math.random() * blockSize ) +1; // generate a random number less than the size of the block
console.log("randomIndex is: " + randomIndex);
block.forEach(function(ip, long, index){
if(index == randomIndex){
console.log('IP: ' + ip)
console.log('INDEX: ' + index)
// cannot break! this is a forEach :(
}
});