简体   繁体   中英

Launch interactive ssh from nodejs

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 ie child_process concerns launching and controlling the process within node, but I simply want to exit back and take over once ssh is started.

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");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM