Reputation: 14549
Using the ssh2
module, connecting to a server with a privateKey, I'm getting errors in the auth logs. The logs are shown below.
The code for setting up the connection:
const Client = require('ssh2').Client;
const fs = require('fs');
const util = require('util');
const key = fs.readFileSync('/path/to/rsa_key');
const conn = new Client();
await conn.connect({
host: '<REMOTE_IP>',
port: 22,
username: '<NAME>',
privateKey: key,
// @ts-ignore
debug: (d) => {
console.log(util.inspect(d));
},
});
await this.performTask(conn, 'uptime');
async performTask(conn, command) {
return new Promise((resolve, reject) => {
// @ts-ignore
conn.exec(command, (err, stream) => {
if (typeof err !== 'undefined') {
// On error, stream is undefined.
console.error(`ERROR: ${err}`);
reject(new Error(err));
} else {
// @ts-ignore
stream.on('close', (code, signal) => {
console.log(`Stream :: close :: code: ${code} , signal: ${signal}`);
conn.end();
resolve();
// @ts-ignore
}).on('data', (data) => {
console.log(`STDOUT: ${data}`);
// @ts-ignore
}).on('error', (errrrrr) => {
console.error(`B ${errrrrr}`);
// @ts-ignore
}).stderr.on('data', (data) => {
console.error(`STDERR: ${data}`);
});
}
});
});
}
Sep 2 22:04:27 <remote_host> sshd[9037]: Disconnected from <local_ip> port 38194 [preauth]
Turning on debug
prints different things. Either one of these 2:
'DEBUG: Local ident: \'SSH-2.0-ssh2js0.4.4\''
'DEBUG: Client: Trying <REMOTE_IP> on port 22 ...'
'DEBUG: Outgoing: Writing CHANNEL_OPEN (0, session)'
'DEBUG: Client: Connected'
'DEBUG: Parser: IN_INIT'
'DEBUG: Parser: IN_GREETING'
'DEBUG: Parser: IN_HEADER'
'DEBUG: Remote ident: \'SSH-2.0-OpenSSH_7.6p1 Ubuntu-4ubuntu0.3\''
'DEBUG: Outgoing: Writing KEXINIT'
'DEBUG: Parser: IN_PACKETBEFORE (expecting 8)'
'DEBUG: Parser: IN_PACKET'
'DEBUG: Parser: pktLen:1076,padLen:6,remainLen:1072'
'DEBUG: Parser: IN_PACKETDATA'
'DEBUG: Parser: IN_PACKETDATAAFTER, packet: KEXINIT'
'DEBUG: Comparing KEXINITs ...'
<alogirthm comparisons, no error message, just print of what is picked>
'DEBUG: Outgoing: Writing KEXECDH_INIT'
'DEBUG: Parser: IN_PACKETBEFORE (expecting 8)'
events.js:174
throw er; // Unhandled 'error' event
^
Error: write EPIPE
at WriteWrap.afterWrite [as oncomplete] (net.js:788:14)
Emitted 'error' event at:
at Socket.<anonymous> (....../node_modules/ssh2/lib/client.js:307:10)
at Socket.emit (events.js:203:15)
at errorOrDestroy (internal/streams/destroy.js:107:12)
at onwriteError (_stream_writable.js:436:5)
at onwrite (_stream_writable.js:461:5)
at _destroy (internal/streams/destroy.js:49:7)
at Socket._destroy (net.js:613:3)
at Socket.destroy (internal/streams/destroy.js:37:8)
at WriteWrap.afterWrite [as oncomplete] (net.js:790:10)
Or:
'DEBUG: Local ident: \'SSH-2.0-ssh2js0.4.4\''
'DEBUG: Client: Trying<REMOTE_IP> on port 22 ...'
'DEBUG: Outgoing: Writing CHANNEL_OPEN (0, session)'
'DEBUG: Client: Connected'
'DEBUG: Parser: IN_INIT'
'DEBUG: Parser: IN_GREETING'
'DEBUG: Parser: IN_HEADER'
'DEBUG: Remote ident: \'SSH-2.0-OpenSSH_7.6p1 Ubuntu-4ubuntu0.3\''
'DEBUG: Outgoing: Writing KEXINIT'
ERROR: Error: No response from server
Either of those happens without a predictable pattern.
Upvotes: 0
Views: 1771
Reputation: 106736
For now you have to wait for the 'ready'
event before issuing commands as there is currently no request/command queue used before the connection is completely ready.
Upvotes: 1