UNPKG

domotz-remote-pawn

Version:

Domotz Agent

71 lines (65 loc) 2.6 kB
/** 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 tom on 14/09/15. */ module.exports = function (result, scannedIp, myConsole) { try { var output = {}; if (result.nmaprun.runstats[0].hosts[0].$.up === 0) { throw new Error('Host is reported DOWN by NMAP'); } // https://domotzjira.atlassian.net/browse/BT-1952 // Under certain circumstances, nmap returns an XML containing extra hosts, other than the scanned one // We must hence select the right host from the list var host = null; if (result.nmaprun.host) { result.nmaprun.host.forEach(function (candidateHost) { try { if (candidateHost.address[0].$.addr === scannedIp && candidateHost.ports !== undefined && host === null) { host = candidateHost; return; } } catch (e) { // } myConsole.error('(%s) nmap returned unexpected host: %s', scannedIp, JSON.stringify(candidateHost)); }); } if (host === null) { myConsole.warn('PortMap: Cannot parse XML.'); myConsole.warn('XML: ' + result); return {}; } var ports = host.ports[0].port; if (ports === undefined || ports.length === 0) { myConsole.info('No ports found'); return {}; } for (var i = 0; i < ports.length; i++) { var currentPort = ports[i]; var status = 'OFFLINE'; if (currentPort.state[0].$.state === 'open') { status = 'ONLINE'; } output[currentPort.$.portid] = status; } return output; } catch (err) { myConsole.warn(err.stack); throw new Error('Error during parsing: Wrong Input'); } };