2

I need to launch a command with sudo over ssh using npm ssh2 module for node.js; only reading module's documentation I can't figure out how to use the pty option:

ssh.on('ready', function() {

    //not working sudo command:
    ssh.exec('cd /var/www/website && sudo mkdir myDir', { pty: true }, function(err, stream) {
        if (err) throw err;

        stream.on('exit', function(code, signal) {

            console.log('exit code: ' + code + ' signal: ' + signal);

            ssh.end();
        });
    });

}).connect({
    host: configuration.sshAddress,
    port: 22,
    username: configuration.sshUser,
    password: configuration.sshPass
});

Can someone explain the correct use of this?

Cereal Killer
  • 3,387
  • 10
  • 48
  • 80

2 Answers2

4

You have to parse the stream output for the sudo prompt. At that point, you have to stream.write(password + '\n');. The alternative to doing all of that is to modify /etc/sudoers so that the logged in user can execute sudo for a specific program/script/whatever without a password.

mscdex
  • 104,356
  • 15
  • 192
  • 153
1

I think you should place the stream.write(password + '\n'); right after the stream.on('exit',.... line.

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
Franco Aguilera
  • 617
  • 1
  • 8
  • 25