node.jssshvirtual-machinegoogle-compute-engineclient-library

How to ssh to a gcloud virtual machine through node.js client library


I have a Google Cloud VM named cloudvm and a Node.js script to contact that virtual machine. I want to take a shell and execute

echo "this is fun" > a.txt

command using a ssh client in Node.js. I have tried node-ssh with userid,password and private key and following error occurs;

Message: All configured authentication methods failed

I have used

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

const ssh = new NodeSSH()

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

My final goal is to pass a value to a file inside the Google Cloud VM using Node.js environment. Any ideas?


Solution

  • After creating

    OS user and password

    in google compute engine you can connect it via node-ssh library in node JS .

    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);