forked from lair-framework/browser-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
negate_hosts_by_cidr.js
46 lines (39 loc) · 1.43 KB
/
negate_hosts_by_cidr.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
var negateHostsByCIDR = function () {
// Generate a list of hostname[string_addr] targets which do not exist in CIDR range
//
// Created by: Matt Burch
// Usage: negateHostsByCIDR('x.x.x.x/x') or negateHostsByCIDR('x.x.x.x/x','y.y.y.y/y');
//
var nets = Array.prototype.slice.call(arguments, 0);
var hosts = Hosts.find({
project_id: Session.get('projectId')
}).fetch();
var hostip = {};
function dec2Bin(octet, cidr) {
var pad = '00000000';
var bin = parseInt(octet[0], 10).toString(2);
var bincidr = (bin.length >= pad.length ? bin : pad.slice(0, pad.length - bin.length) + bin);
for (i = 1; i <= octet.length; i++) {
bin = parseInt(octet[i], 10).toString(2);
bincidr += (bin.length >= pad.length ? bin : pad.slice(0, pad.length - bin.length) + bin);
}
return bincidr.slice(0, parseInt(cidr, 10));
}
hosts.forEach(function (host) {
var ip = host.string_addr.split('.');
hostip[dec2Bin(ip, 32)] = host.string_addr;
});
nets.forEach(function (cidr) {
cidr = cidr.split('/');
var net = cidr[0].split('.');
var netbin = dec2Bin(net, cidr[1]);
for (var key in hostip) {
if ((key.slice(0, parseInt(cidr[1], 10))) == netbin) {
delete hostip[key];
}
}
});
for (var key in hostip) {
console.log(hostip[key]);
}
};