5

I am getting below error when connecting ssh server with identity file in node js

Error: Unable to read the Private Key File

my code:

    driver = require('node-ssh');

    ssh = new driver({
              host: '192.168.*.*',
              username: 'user',
              password: 'password',
              privateKey : require('fs').readFileSync('/tmp/my_key')
            });

    ssh.connect().then(function() {
           /*
       some code
        */

            },function(error) {
                console.log(error);

            });

But if i connect from my linux machine, it is connected.

ssh -i /tmp/my_key  [email protected].*.*

Please give any solution for this.

2 Answers 2

8

node-ssh expects the name of the private key file, instead of the contents (example from the node-ssh npm page):

ssh = new node_ssh({
  host: 'localhost',
  username: 'steel',
  privateKey: '/home/steel/.ssh/id_rsa'
});
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Timwolla. Now its working, I got another error. if i execute command like "uptime" , it throws error from server. usage: scp [-1246BCpqrv] [-c cipher] [-F ssh_config] [-i identity_file] [-l limit] [-o ssh_option] [-P port] [-S program] [[user@]host1:]file1 ... [[user@]host2:]file2
0

Updated in the latest npm package, https://www.npmjs.com/package/node-ssh/v/13.1.0 Here, the privateKeyPath parameter is introduced if you give the path of your privateKey.

Example:

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

// or with inline privateKey

ssh.connect({
  host: 'localhost',
  username: 'steel',
  privateKey: Buffer.from('...')
})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.