domotz-remote-pawn
Version:
Domotz Agent
60 lines (55 loc) • 2.06 kB
JavaScript
/** This file is part of Domotz Agent.
* Copyright (C) 2016 Domotz Ltd
*
* Domotz Agent is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Domotz Agent is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Domotz Agent. If not, see <http://www.gnu.org/licenses/>.
*
* Created by Tommaso Latini <tommaso@domotz.com> on 14/09/15.
*/
module.exports = function (result, myConsole) {
if (myConsole === undefined) {
myConsole = console;
}
try {
if (result.nmaprun.runstats[0].hosts[0].$.up === '0') {
throw new Error('Host seems down');
}
var tmp = [];
result.nmaprun.host.forEach(function (host) {
var ports = host.ports[0].port;
if (ports === undefined || ports.length === 0) {
return;
}
for (var i = 0; i < ports.length; i++) {
var currentPort = ports[i];
if (!currentPort.state || currentPort.state[0].$.state !== 'open' || !currentPort.service) {
continue;
}
tmp[parseInt(currentPort.$.portid, 10)] = {
port: currentPort.$.portid,
service: currentPort.service[0].$.name,
method: currentPort.service[0].$.method,
};
}
});
var output = {
ip_ports: [],
};
tmp.forEach(function (port) {
output.ip_ports.push(port);
});
return output;
} catch (err) {
throw new Error('Error during parsing: ' + err.message);
}
};