-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathget_hosts_by_cidr.js
More file actions
47 lines (39 loc) · 1.29 KB
/
get_hosts_by_cidr.js
File metadata and controls
47 lines (39 loc) · 1.29 KB
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
/* eslint-disable no-unused-vars */
/* globals Session Hosts Meteor */
function getHostsByCIDR () {
// Generate a list of hostname[ipv4] targets from supplied CIDR range
//
// Created by: Matt Burch
// Usage: getHostsByCIDR('x.x.x.x/x') or getHostsByCIDR('x.x.x.x/x','y.y.y.y/y')
//
var hostTargets = []
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) {
console.log(hostip[key])
}
}
})
}