I'm trying to pass-through a command in node js received from elsewhere. For the sake of keeping the question simple, imagine there's a function that returns the following (though I have no way to know that ahead of time):
echo "your password is test" & ssh root@1.1.1.1
I would like to take the text above and run it correctly. Because ssh
uses stdin, exec
won't do the trick, and spawn
seems to be the best option. But because spawn expects a command and list of arguments, it's not very well suited to something with an &
in it.
Ideally, I'd like to be able to just run:
const command = mysteryCommand(); // echo "your password is test" & ssh root@1.1.1.1
spawn(command, {
stdio: "inherit",
});
But so far this hasn't proven to work. Other things I've tried include:
spawn(command, { stdio: "ignore", detached: true})
// errored
spawn(command, { stdio: "inherit", shell: true})
// outputs, but input appears not to work
How can I run my mystery command in a way that inherits stdio?
I've found the issue.
First off, the answer to the question above is spawn(command, { stdio: "inherit", shell: true})
. Works just fine.
And the issue I was having was that the password part of ssh does not use stdin/out. It uses something else.
https://github.com/nodejs/node-v0.x-archive/issues/1157#issuecomment-7339123