Use pty with ssh2 in node to execute command with sudo

孤人 提交于 2019-12-08 01:50:33

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!