-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathnegate_hosts_by_cidr.js
49 lines (41 loc) · 1.34 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
47
48
49
/* eslint-disable no-unused-vars */
/* globals Session Hosts Meteor */
function negateHostsByCIDR () {
// Generate a list of hostname[ipv4] 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({
projectId: 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 (var 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.ipv4.split('.')
hostip[dec2Bin(ip, 32)] = host.ipv4
})
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])
}
}