-
Notifications
You must be signed in to change notification settings - Fork 0
/
nmap-js.js
57 lines (55 loc) · 2.3 KB
/
nmap-js.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
50
51
52
53
54
55
56
57
var sys = require('sys');
var exec = require('child_process').exec;
var os = require('os');
module.exports = {
getDevices: (addresses, successCallback, errorCallback) => {
var command = "nmap -sP ";
if(process.platform === 'linux' || process.platform === 'darwin'){
command = `sudo -S true ${command}`;
}
if(addresses && Array.isArray(addresses)) {
command = `${command} ${addresses.join(' ')}`;
}
exec(command, function(error, stdout, stderr) {
if(error) {
errorCallback(error);
return;
}
var devices = [];
var mac = [];
var tmpArr = stdout.split("\n");
for (x in tmpArr) {
if(tmpArr[x].indexOf('Nmap') !== -1) {
tmpArr[x] = tmpArr[x].substring(21);
if(tmpArr[x].indexOf('(') === -1) {
tmpArr[x] = 'unknown ' + tmpArr[x];
}
tmpArr[x] = tmpArr[x].replace("(","");
tmpArr[x] = tmpArr[x].replace(")","");
var tmpSplit = tmpArr[x].split(" ");
var tmpJson = {
"name": tmpSplit[0],
"ip": tmpSplit[1],
}
devices[x] = tmpJson;
// If no mac in nmap output for host, fill in Unknown.
if(tmpArr[x++].indexOf('MAC') === -1) {
mac[x] = "Unknown";
}
}
if(tmpArr[x].indexOf('MAC') !== -1) {
tmpArr[x] = tmpArr[x].substring(13);
tmpArr[x] = tmpArr[x].replace("(","");
tmpArr[x] = tmpArr[x].replace(")","");
mac[x] = tmpArr[x];
}
}
devices = devices.filter(function(n){return n}); //Removes all empty elements
mac = mac.filter(function(n){return n}); //Removes all empty elements
for (x in devices) {
devices[x].mac = mac[x];
}
successCallback(devices);
});
}
}