我正在try 使用NodeJS启动到Linux机器的远程ssh会话.我的要求是在同一个ssh会话中一个接一个地运行多个shell命令.目前我正在使用simple-ssh nPM包,该包有效,但我的要求要求不依赖第三方工具/包.相反,建议我使用child_process进行远程ssh会话.

以下是使用simple-ssh包的示例:

var SSH = require("simple-ssh")

var ssh = new SSH({
    host: 'remote_host',
    user: 'username',
    pass: 'password'
});

ssh.exec('pwd', {
        out: console.log.bind(console)
    })
    .exec('ls', {
        out: console.log.bind(console)
    })
    .exec('date', {
        out: console.log.bind(console)
    })
    .start();

以下是我用child_processtry 的一个:

  const { spawn } = require('child_process');
  const child1 = spawn('sshpass', ['-p', 'password', 'ssh', '-T', '-o', 'StrictHostKeyChecking=no', `'username'@'remote_host'`]);
       
   child1.stdin.write('password\n');

   child1.stdout.on('data', (data) => {
       console.log(`stdout: ${data}`);
   });
   child1.stderr.on('data', (data) => {
       console.error(`stderr: ${data}`);
   });
   child1.on('close', (code) => {
       console.log(`child process exited with code ${code}`);
   });


   const child2 = spawn('sshpass', ['-p', 'password', 'ssh', '-T', '-o', 'StrictHostKeyChecking=no', `'username'@'remote_host'`]);
       
   child2.stdin.write('password\n');

   child2.stdout.on('data', (data) => {
       console.log(`stdout: ${data}`);
   });
   child2.stderr.on('data', (data) => {
       console.error(`stderr: ${data}`);
   });
   child2.on('close', (code) => {
       console.log(`child process exited with code ${code}`);
   });
  
   const child3 = spawn('sshpass', ['-p', 'password', 'ssh', '-T', '-o', 'StrictHostKeyChecking=no', `'username'@'remote_host'`]);
       
   child3.stdin.write('password\n');

   child3.stdout.on('data', (data) => {
       console.log(`stdout: ${data}`);
   });
   child3.stderr.on('data', (data) => {
       console.error(`stderr: ${data}`);
   });
   child3.on('close', (code) => {
       console.log(`child process exited with code ${code}`);
   });

此外,如果我按顺序运行这些命令,然后打印输出,我将获得所有命令的单个输出(这是我不想要的):

    const { spawn } = require('child_process');
    const child = spawn('sshpass', ['-p', 'password', 'ssh', '-T', '-o', 'StrictHostKeyChecking=no', `'username'@'remote_host'`]);
       
    child.stdin.write('pwd\n');

    child.stdin.write('ls -al\n');
        
    child.stdin.write('date\n');
        
    child.stdout.on('data', (data) => {
        console.log(`stdout: ${data}`);
    });

    child.stderr.on('data', (data) => {
        console.error(`stderr: ${data}`);
    });

    child.on('close', (code) => {
        console.log(`child process exited with code ${code}`);
    });

我的问题是如何在持久会话中将三个ssh命令与child_process spawn链接起来,并一个接一个地同步运行这些命令?

推荐答案

将"shell"选项设置为"true",以在默认为"false"的shell内部运行命令.还可以查看node js child process spawn command documentation

const ssh = spawn(
  "sshpass",
  [
    "-p",
    password,
    "ssh",
    "-T",
    "-o",
    "StrictHostKeyChecking=no",
    `${username}@${host}`,
  ],
  { shell: true }
);

Node.js相关问答推荐

使用Moongose处理node.js中重定向的then()块链

Mongoose查询-如何根据当前查找ID获取其他集合并将其插入到当前查找中?

如何在JavaScript中使用Mongoose将项推送到MongoDB中的重嵌套数组

如果我加入另一个公会且我的​​机器人已在其中,欢迎消息发送错误

如何使用 Node.js 连接到 Cloud SQL?

处理嵌套元素时,使用xml2js库解析XML时发生错误

Gulp 能否向 Docker 发出增量构建的第一次迭代完成的信号?

如何使用 Remix 仅在客户端呈现组件?

级联定时器和Jest 的异步功能

NodeJS 中的流 API 数据如何流动?

Node.js |如何在微服务之间转发标头?

FirebaseCloudMessaging : PlatformException (PlatformException(null-error, Host platform returned null value for non-null return value., null, null))

'{ id: string; 类型的参数}' 不可分配给FindOneOptions类型的参数

在安装 tensorflow 时遇到问题

什么是nestjs错误处理方式(业务逻辑错误vs.http错误)?

如何在 Node.js 的 console.log() 中创建换行符

我可以向NPM添加调试脚本吗?

错误:大多数中间件(如 bodyParser)不再与 Express Bundle

node.js 中存储的模块变量在什么范围内?

nodejs - 如何读取和输出 jpg 图像?