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
1 Answer
Reset to default 6There'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 addprocess.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.