domotz-remote-pawn
Version:
Domotz Agent
78 lines (67 loc) • 2.44 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 04/01/16.
*
* {
* "command": <str> command to execute
* "args": [...] arguments list
* }
*/
var sshSend = function (message, resourceLocator) {
var ssh = resourceLocator.networkTools.ssh;
var payload = message.payload;
var sshCommand = payload.cmd + ' ' + (payload.args || []).join(' ');
var sshOptions = payload.sshOptions || {};
//delete payload.cmd;
//delete payload.args;
//delete payload.sshOptions;
var logId = message.correlationId;
var user = payload.username;
var host = payload.host;
var port = payload.port;
sshOptions.log_prefix = logId;
sshOptions.timeout = payload.timeout;
sshOptions.user = user;
sshOptions.password = payload.password;
sshOptions.host = host;
sshOptions.port = port;
sshOptions.host_key_checking = 'no';
return {
describe: function () {
// 'cid - execSshCommand - <user>@<host>:<port> "<cmd> <args>"'
return 'sshSend - ' + user + '@' + host + ':' + port +
' "' + sshCommand + '"';
},
execute: function (callback) {
ssh.exec(sshCommand, sshOptions)
.then(function (res) {
res = res.trim();
console.debug('MESSAGE - (%s): SSH Send get the result: %s', logId, res);
callback({
output: res
});
})
.catch(function (res) {
callback({
error: res
});
});
}
};
};
module.exports.command = sshSend;