domotz-remote-pawn
Version:
Domotz Agent
74 lines (68 loc) • 2.69 kB
JavaScript
/** This file is part of Domotz Agent.
* Copyright (C) 2021 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/>.
*
* Command for executing SSH commands on Windows hosts
*/
;
var winsshExecute = function (message, resourceLocator) {
var winssh = resourceLocator.sshSendFactory;
var data = {
username: message.payload.username,
password: message.payload.password,
port: message.payload.port || 22,
mac_address: message.payload.hw_address,
host: message.payload.host,
command: message.payload.command,
parserCode: message.payload.parser,
};
var baseConsole = resourceLocator.log.decorateLogs();
var executeWinSSHCommand = function (callback) {
var myConsole = baseConsole.decorate('EXECUTE - ' + data.mac_address);
var ipAddress = data.host || resourceLocator.interfacesBindingStorage.getIpsFromMac(data.mac_address.toUpperCase())[0];
var payload = {
cmd: data.command,
username: data.username,
password: data.password,
host: data.ip_address,
port: data.port,
timeout: 30000,
};
var parser = function (x) {
return x;
};
if (data.parserCode !== undefined) {
parser = new Function('output', data.parserCode);
}
myConsole.info('Executing ssh command `%s`on %s:%s with username %s', data.command, ipAddress, data.port, data.username);
winssh
.sshSend(payload, 'api')
.then(function (output) {
callback({ outcome: parser(output), error: null });
})
.catch(function (err) {
callback({ outcome: null, error: err });
});
};
return {
execute: function (callback) {
return executeWinSSHCommand(callback);
},
describe: function () {
return 'winsshExecute' + JSON.stringify(require('../').removePassword(data));
},
};
};
module.exports.command = winsshExecute;