node.jslinuxsshssh2-exec

How do i execute sudo command over ssh in node JS.?


I tried remote-exec and node-ssh packages, but both are resulting the same as mentioned below.

var rexec = require('remote-exec');

var connection_options = {
       port: 22,
       username: 'user', 
       privateKey: require('fs').readFileSync('privateKey/file.pem'),
       passphrase:''};

var hosts = [
    '54.xxx.xxx.xxx'
];

var cmds = [
   'sudo adduser testuser'
];

rexec(hosts, cmds, connection_options, function(err){

    if (err) {
        console.log(err);
    } else {
        console.log('Great Success!!');
    }
});

It returns the error :

[Error: 554.xxx.xxx.xxx : sudo adduser testuser [Exit 1]]

sudo: sorry, you must have a tty to run sudo

Please advise how to run sudo command using node js.


Solution

  • Comment below line in /etc/sudoers file.

    #Defaults requiretty