Search code examples
node.jssshshelljs

Executing SSH with ShellJs


I have been using shelljs to execute certain set of commands. So i tried to connect to a remote ubuntu server over ssh via shelljs. This is my nodejs file.

var shelljs = require('shelljs');
shelljs.exec('ssh -i [path/to/pem/file] ubuntu@[taregt_ip]');

But i get this error:

Pseudo-terminal will not be allocated because stdin is not a terminal.

So how to solve this issue or is there any other utility to connect to ssh with one single command.

EDIT: the link you guys are posting for me to refer in not the duplicate of my problem.


Solution

  • SSH doesn't always allocate a pseudo-terminal. That is explained in this existing answer. You should add the -tt flag to force this.

    That said, I wouldn't use shelljs to do ssh. As you are just executing a local binary, this solution a) is not portable and b) will not know how to parse errors or deal with unexpected output. I have used ssh2 in the past for similar projects, as it has an interactive SSH option, which makes it easy to create streams (STDOUT and STDIN) from an SSH connection.