在节点中使用带有ssh2的pty以sudo执行命令

我需要使用npm ssh2模块为node.js启动sudo over ssh命令; 只读模块的文档我无法弄清楚如何使用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 }); 

有人可以解释这个的正确使用吗?

您必须parsingsudo提示符的stream输出。 在这一点上,你必须stream.write(password + '\n'); 。 做所有这些的替代方法是修改/etc/sudoers以便login的用户可以为特定的程序/脚本/无论什么密码都执行sudo。