2014-12-19 115 views
1

我需要使用npm ssh2模块为node.js启动sudo over ssh的命令; 只读取模块的文档,我无法弄清楚如何使用PTY选项:在节点中使用pty和ssh2与sudo执行命令

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 
}); 

有人能解释的正确使用呢?

回答

2

您必须解析sudo提示符的stream输出。那时,你必须stream.write(password + '\n');。完成这一切的另一种方法是修改/etc/sudoers,以便登录的用户可以为特定的程序/脚本/不管密码执行sudo。

+0

我不能得到这个工作。如何/在哪里添加'stream.write(密码+'\ n');' – user1513388 2017-11-03 17:01:38

0

我想你应该把stream.write(password + '\n');放在stream.on('exit',....的后面。