在node.js中用tty生成子进程 [英] Spawning a child process with tty in node.js

查看:82
本文介绍了在node.js中用tty生成子进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用ssh在远程服务器上做一些工作-并从node.js在本地计算机上调用ssh

I am trying to do some work on a remote server using ssh--and ssh is called on the local machine from node.js

该脚本的精简版本如下:

A stripped down version of the script looks like this:

var execSync = require("child_process").execSync;
var command =
  'ssh -qt user@remote.machine -- "sudo mv ./this.thing /to/here/;"';
execSync(command,callback);

function callback(error,stdout,stderr) {

  if (error) {
    console.log(stderr);
    throw new Error(error,error.stack);
  }
  console.log(stdout);
}

我收到 requiretty 错误 sudo:抱歉,您必须有一个tty才能运行sudo .

如果我直接从命令行运行 ssh -qt user@remote.machine-"sudo mv ./this.thing/to/here/;" 从tty中获取-我没有错误,并且 this.thing /to/there/很好地移动了.

If I run ssh -qt user@remote.machine -- "sudo mv ./this.thing /to/here/;" directly from the command line--in other words, directly from a tty--I get no error, and this.thing moves /to/there/ just fine.

这是部署脚本的一部分,在其中将!requiretty 添加到sudoers文件中确实不是理想的选择.

This is part of a deploy script where it really wouldn't be ideal to add !requiretty to the sudoers file.

反正有没有办法让node.js在tty中运行命令?

Is there anyway to get node.js to run a command in a tty?

推荐答案

有一些选择:

  • 如果您不介意为子进程重复使用父进程的stdin/stdout/stderr(假设它可以访问真实的tty),则可以使用 stdio:'inherit'(或如果需要,仅继承单个流)在您的 spawn()选项中.

  • If you don't mind re-using stdin/stdout/stderr of the parent process (assuming it has access to a real tty) for your child process, you can use stdio: 'inherit' (or only inherit individual streams if you want) in your spawn() options.

通过 pty

使用像 ssh2 这样的ssh模块完全涉及子进程(并具有更大的灵活性).使用 ssh2 ,您只需将 pty:true 选项传递给 exec() sudo 就可以正常工作.

Use an ssh module like ssh2 that doesn't involve child processes at all (and has greater flexibility). With ssh2 you can simply pass the pty: true option to exec() and sudo will work just fine.

这篇关于在node.js中用tty生成子进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆