最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Node.js child_process exec, stdin not being passed through to ssh - Stack Overflow

programmeradmin6浏览0评论

I have the following Node.js code to ssh into a server and it works great forwarding stdout, but whenever I type anything it is not forwarding to the server. How do I forward my local stdin to the ssh connections stdin?

var mand = 'ssh -tt -i ' + keyPath + ' -o StrictHostKeyChecking=no ubuntu@' + hostIp;

var ssh = child_proc.exec(mand, {
    env: process.env
});

ssh.stdout.on('data', function (data) {
    console.log(data.toString());
});

ssh.stderr.on('data', function (data) {
    console.error(data.toString());
});

ssh.on('exit', function (code) {
    process.exit(code);
});

I have the following Node.js code to ssh into a server and it works great forwarding stdout, but whenever I type anything it is not forwarding to the server. How do I forward my local stdin to the ssh connections stdin?

var mand = 'ssh -tt -i ' + keyPath + ' -o StrictHostKeyChecking=no ubuntu@' + hostIp;

var ssh = child_proc.exec(mand, {
    env: process.env
});

ssh.stdout.on('data', function (data) {
    console.log(data.toString());
});

ssh.stderr.on('data', function (data) {
    console.error(data.toString());
});

ssh.on('exit', function (code) {
    process.exit(code);
});
Share Improve this question asked Aug 17, 2015 at 17:40 JustinJustin 45.5k83 gold badges213 silver badges312 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

There's two ways to go about this if you want to pipe the process.stdin to the child process:

  • Child processes have a stdin property that represents the stdin of the child process. So all you should need to do is add process.stdin.pipe(ssh.stdin)

  • You can specify a custom stdio when spawning the process to tell it what to use for the child process's stdin:

    child_proc.exec(mand, { env: process.env, stdio: [process.stdin, 'pipe', 'pipe'] })
    

Also, on a semi-related note, if you want to avoid spawning child processes and have more programmatic control over and/or have more lightweight ssh/sftp connections, there is the ssh2 module.

发布评论

评论列表(0)

  1. 暂无评论