【问题标题】:How to ssh to a gcloud virtual machine through node.js client library如何通过 node.js 客户端库 ssh 到 gcloud 虚拟机
【发布时间】:2021-11-08 11:28:41
【问题描述】:

我有一个名为 cloudvm 的 Google Cloud VM 和一个用于联系该虚拟机的 Node.js 脚本。我想拿一个shell并执行

echo "this is fun" > a.txt

在 Node.js 中使用 ssh 客户端的命令。我用用户名、密码和私钥尝试了node-ssh,出现以下错误;

Message: All configured authentication methods failed

我用过

   const {NodeSSH} = require('node-ssh')

const ssh = new NodeSSH()

ssh.connect({
  host: 'localhost',
  username: 'steel',
  privateKey: '/home/steel/.ssh/id_rsa'
})

我的最终目标是使用 Node.js 环境将值传递给 Google Cloud VM 内的文件。有什么想法吗?

【问题讨论】:

  • Google Cloud 默认不启用用户名/密码身份验证。默认情况下,仅启用 SSH 密钥对。发布问题时,在您对代码进行一些调试后,显示您的代码以及您的代码存在的错误/问题。
  • @JohnHanley 已经输入了代码和错误。谢谢,我很早就想到了。您应该创建操作系统用户和密码以通过云外壳访问它

标签: node.js ssh virtual-machine google-compute-engine client-library


【解决方案1】:

创建后

操作系统用户和密码

在谷歌计算引擎中,您可以通过 node JS 中的 node-ssh 库连接它。

const { NodeSSH } = require("node-ssh");
const ssh = new NodeSSH();
var session = await ssh.connect({
      host: "xxx.xxx.x.xx",
      username: "xxxxxx",
      password: "xxxx",
    });
    var cmde = 'your/command to execute';
    var ot = await session.execCommand(cmde);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-13
    • 1970-01-01
    • 1970-01-01
    • 2018-07-05
    • 1970-01-01
    • 2013-07-01
    • 2016-03-16
    • 1970-01-01
    相关资源
    最近更新 更多