我需要使用 Node.js 的 npm ssh2 模块通过 sudo 启动命令; 仅阅读模块的文档我无法弄清楚如何使用 pty 选项:
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
});
谁能解释一下它的正确用法吗?
最佳答案
您必须解析 sudo 提示符的 stream
输出。此时,您必须stream.write(password + '\n');
。完成所有这些操作的替代方法是修改 /etc/sudoers
,以便登录用户无需密码即可对特定程序/脚本/任何内容执行 sudo。
关于node.js - 在 Node 中使用 pty 和 ssh2 来使用 sudo 执行命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27559265/