4

I have a text blob that contains ip, port, user and passord and would like to write a small utility script where I can:

  • paste the text in stdin
  • extract the connection details using regex or any other means
  • use node to launch ssh interactively using my parsed values

How would I launch the ssh command from node with the arguments, exit node and continue on ssh'ing? The documentation I've found regarding i.e. child_process concerns launching and controlling the process within node, but I simply want to exit back and take over once ssh is started.

1 Answer 1

2

The following code will fit your requirements, adjust it to your needs. The program does the following:

  • reads the port from a file
  • prompts you for the hostname, reading data from stdin
  • launches ssh using child_process.fork

Code:

var port = require('fs').readFileSync('port.txt');

const rl = require('readline').createInterface({
  input: process.stdin,
  output: process.stdout
});

rl.question('Enter hostname: ', (answer) => {
  require('child_process').spawn('ssh', [answer, port],
    {stdio: [process.stdin, process.stdout, process.stderr]});
});

The question is answered except for the following point:

exit node and continue on ssh'ing

I am not aware of this being possible in any programming language, but a better answer can correct me. Instead, you launch a child process and redirect all input/output to it. From your perspective, you only interact with ssh, so the fact that the node process still exists as a parent to ssh is transparent to you.

An equivalent command in perl for example would be system("ssh");

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.