forked from spitzlbergerj/MMM-NetworkScanner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode_helper.js
148 lines (126 loc) · 4.81 KB
/
node_helper.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/* global require, module */
/* jshint esversion: 6 */
/* Magic Mirror
* Node Helper: MMM-NetworkScanner
*
* By Ian Perrin http://ianperrin.com
* MIT Licensed.
*/
const NodeHelper = require("node_helper");
const ping = require("ping");
const sudo = require("sudo");
module.exports = NodeHelper.create({
start: function function_name () {
this.log("Starting module: " + this.name);
},
// Override socketNotificationReceived method.
socketNotificationReceived: function(notification, payload) {
this.log(this.name + " received " + notification);
if (notification === "CONFIG") {
this.config = payload;
return true;
}
if (notification === "SCAN_NETWORK") {
this.scanNetworkMAC();
this.scanNetworkIP();
return true;
}
},
scanNetworkMAC: function() {
this.log(this.name + " is performing arp-scan");
var self = this;
// Target hosts/network supplied in config or entire localnet
var arpHosts = this.config.network || '-l';
var arp = sudo(['arp-scan', '-q', arpHosts]);
var buffer = '';
var errstream = '';
var discoveredMacAddresses = [];
var discoveredDevices = [];
arp.stdout.on('data', function (data) {
buffer += data;
});
arp.stderr.on('data', function (data) {
errstream += data;
});
arp.on('error', function (err) {
errstream += err;
});
arp.on('close', function (code) {
if (code !== 0) {
self.log(self.name + " received an error running arp-scan: " + code + " - " + errstream);
} else {
// Parse the ARP-SCAN table response
var rows = buffer.split('\n');
for (var i = 2; i < rows.length; i++) {
var cells = rows[i].split('\t').filter(String);
// Update device status
if (cells && cells[1]) {
var macAddress = cells[1].toUpperCase();
if (macAddress && discoveredMacAddresses.indexOf(macAddress) === -1) {
discoveredMacAddresses.push(macAddress);
var device = self.findDeviceByMacAddress(macAddress);
if (device) {
device.online = true;
discoveredDevices.push(device);
}
}
}
}
}
self.log(self.name + " arp scan addresses: ", discoveredMacAddresses);
self.log(self.name + " arp scan devices: ", discoveredDevices);
self.sendSocketNotification("MAC_ADDRESSES", discoveredDevices);
return;
});
},
scanNetworkIP: function() {
if (!this.config.devices) {
return;
}
this.log(this.name + " is performing ip address scan");
var discoveredDevices = [];
var self = this;
this.config.devices.forEach( function(device) {
self.log(self.name + " is checking device: ", device.name);
if ("ipAddress" in device) {
self.log(self.name + " is pinging ", device.ipAddress);
ping.sys.probe(device.ipAddress, function(isAlive) {
device.online = isAlive;
self.log(self.name + " ping result: ", [device.name, device.online] );
if (device.online) {
discoveredDevices.push(device);
}
self.sendSocketNotification("IP_ADDRESS", device);
});
}
});
},
findDeviceByMacAddress: function (macAddress) {
// Find first device with matching macAddress
for (var i = 0; i < this.config.devices.length; i++) {
var device = this.config.devices[i];
if (device.hasOwnProperty("macAddress")) {
if (macAddress.toUpperCase() === device.macAddress.toUpperCase()){
this.log(this.name + " found device by MAC Address", device);
return device;
}
}
}
// Return macAddress (if showing unknown) or null
if (this.config.showUnknown) {
return {macAddress: macAddress, name: macAddress, icon: "question", type: "Unknown"};
} else {
return null;
}
},
log: function(message, object) {
// Log if config is missing or in debug mode
if (!this.config || this.config.debug) {
if (object) {
console.log(message, object);
} else {
console.log(message);
}
}
},
});