在节点中将ssh2与pty一起使用以执行sudo命令 [英] Use pty with ssh2 in node to execute command with sudo

查看:210
本文介绍了在节点中将ssh2与pty一起使用以执行sudo命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用node.js的npm ssh2模块在ssh上使用sudo启动命令; 仅阅读模块的文档,我不知道如何使用pty选项:

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?

推荐答案

您必须解析sudo提示符的stream输出.此时,您必须stream.write(password + '\n');.完成所有这些操作的替代方法是修改/etc/sudoers,以便登录的用户可以在没有密码的情况下针对特定程序/脚本/执行sudo.

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.

这篇关于在节点中将ssh2与pty一起使用以执行sudo命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆