如何通过node.js客户端库ssh到gcloud虚拟机



我有一个名为cloudvm的谷歌云虚拟机和一个Node.js脚本来联系该虚拟机。我想拿一个shell执行

echo "this is fun" > a.txt

命令使用Node.js中的ssh客户端。我尝试过使用userid、密码和私钥的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内的文件。有什么想法吗?

创建

OS用户和密码

在谷歌计算引擎中,您可以通过nodeJS中的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);

最新更新