NodeJS 如何连接到不同环境的远程服务器?

jhkqcmku  于 2023-06-29  发布在  Node.js
关注(0)|答案(1)|浏览(128)

我正在尝试连接远程计算服务器并尝试在其上运行命令。
连接成功,但命令未运行。它看起来像,它正在改变到另一个环境。所以我需要在那边运行命令。

conn.on('ready', () => {
    conn.exec(command, (err, stream) => {
        if (err) throw err;
        let x = '';
        stream.on('close', (code, signal) => {
            console.log("x is ",x)
            conn.end();
        }).on('data', (data) => {
            x += data.toString();
        }).stderr.on('data', (data) => {
            node.send(msg);

        });
    });
}).connect(sshConfig);

使用上面的代码,它只打印下面的行Your last successful login was at 2023-6-28 11:43:55,但不运行命令

l2osamch

l2osamch1#

嗯,我不知道你想要什么,但我希望这段代码能帮助你。

conn
  .on("ready", function () {
    conn.shell(function (err, stream) {
      if (err) throw err;
      stream
        .on("close", function () {
          console.log("x is ", x);
          conn.end();
        })
        .on("data", function (data) {
          x += data.toString();
        })
        .stderr.on("data", function (data) {
          node.send(msg);
        });
      stream.end("service something reload\nexit\n");
    });
  })
  .connect(sshConfig);

相关问题