Last active
November 25, 2021 20:32
-
-
Save matthewholliday/77860b72a345cd3b7c37dfc4c265870a to your computer and use it in GitHub Desktop.
Connecting to SSH server with ssh2 example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { readFileSync } = require('fs'); | |
const { Client } = require('ssh2'); | |
const conn = new Client(); | |
conn.on('ready', () => { | |
console.log('CONNECTED! \n\n'); | |
//Run the "ls -la" command. | |
conn.exec('ls -la', (err, stream) => { | |
if (err) throw err; | |
stream.on('close', (exitCode, signal) => { | |
console.log('STREAM HAS CLOSED:'); | |
console.log('EXIT CODE: ' + exitCode); | |
console.log('SIGNAL: ' + signal); | |
conn.end(); | |
}).on('data', (data) => { | |
console.log('TERMINAL OUTPUT: \n\n' + data); | |
}).stderr.on('data', (data) => { | |
console.log('ERROR: \n' + data); | |
}); | |
}); | |
}).connect({ | |
host: '54.237.141.196', | |
port: 22, | |
username: 'ec2-user', | |
privateKey: readFileSync('C:\\Path\\To\\Key\\MY_KEY_NAME.pem') //Must be a ".pem" key. | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment