a" /> a" /> a"/>

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

234 views Asked by At

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?

1

There are 1 answers

0
SHARON XAVIER On BEST ANSWER

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