steelbrain / node-ssh

SSH2 with Promises
MIT License
940 stars 95 forks source link

Issue with Error: All configured authentication methods failed #258

Open shinkhouse opened 4 years ago

shinkhouse commented 4 years ago

Hi there,

I'm trying to use your library to connect to a server via ssh but having some trouble. I'm stumbling on the Error: All configured authentication methods failed error message. However, when I try to connect with using ssh2 library by itself, it has no issue.

This example is how I connect using ssh2 library...

const connectionOptions = {
            host: hostname,
            port: port,
            username: username,
            privateKey: require('fs').readFileSync(path.resolve(__dirname, privateKeyLocation))
        };

        var conn = new Client();
        conn.on('ready', function () {
            conn.shell(function (err, stream) {
                if (err) {
                    return err;
                } else {
                    stream.on('close', function () {
                        console.log('Stream :: close');
                        conn.end();
                    }).on('data', function (data) {
                        console.log('OUTPUT: ' + data);
                    });
                    stream.end(commands);
                }

            });

        }).connect(connectionOptions);

This example is how I'm trying to use node-ssh. I also had an issue with the privateKey not being a string but it connects fine in first example without toString() method.

const connectionOptions = {
            host: hostname,
            port: port,
            username: username,
            privateKey: require('fs').readFileSync(path.resolve(__dirname, privateKeyLocation)).toString()
        };

        await ssh.connect(connectionOptions)
        const defaultDirectory = await ssh.exec('pwd', [], {
            stream: 'stdout'
        })
        const shellStream = await ssh.requestShell()

        const stdin = process.openStdin();
        stdin.addListener('data', (data) => {
            console.log('current path is', defaultDirectory)
            // Trim input to prevent the endings of lines in Windows ('\r\n\')
            shellStream.write(data.toString().trim() + '\n')
        })
        shellStream.on('data', (data) => {
            process.stdout.write(data)
        })
        shellStream.stderr.on('data', (data) => {
            process.stdout.write(data)
        })

I'm aware this error is common in Linux, but seems to only happen after I use this library. Could you point me in the right direction?

steelbrain commented 4 years ago

Thanks for the issue @shinkhouse

I do not see an obvious bug here, it seems to be in order. One nit tho is that node-ssh supports file paths to private keys, so instead of reading contents, you can just feed it a path and it'll read the contents itself