问题
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?
回答1:
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.
回答2:
I think you should place the stream.write(password + '\n');
right after the stream.on('exit',....
line.
来源:https://stackoverflow.com/questions/27559265/use-pty-with-ssh2-in-node-to-execute-command-with-sudo