简体   繁体   English

从nodejs启动交互式ssh

[英]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: 我有一个包含ip,port,user和passord的文本blob,我想写一个小实用程序脚本,我可以:

  • paste the text in stdin 将文本粘贴到stdin中
  • extract the connection details using regex or any other means 使用正则表达式或任何其他方法提取连接详细信息
  • use node to launch ssh interactively using my parsed values 使用node以使用我的解析值以交互方式启动ssh

How would I launch the ssh command from node with the arguments, exit node and continue on ssh'ing? 如何从带有参数的节点启动ssh命令,退出节点并继续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. 我发现的关于ie child_process的文档涉及在节点内启动和控制进程,但我只想退出并在ssh启动后接管。

The following code will fit your requirements, adjust it to your needs. 以下代码符合您的要求,根据您的需求进行调整。 The program does the following: 该计划执行以下操作:

  • reads the port from a file 从文件中读取port
  • prompts you for the hostname, reading data from stdin 提示您输入主机名,从stdin读取数据
  • launches ssh using child_process.fork 使用child_process.fork启动ssh

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 退出节点并继续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. 从您的角度来看,您只与ssh交互,因此node进程仍然作为ssh的父进程存在这一事实对您来说是透明的。

An equivalent command in perl for example would be system("ssh"); 例如, perl的等效命令是system("ssh");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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