Created
November 25, 2021 20:53
-
-
Save matthewholliday/ae627749ad8b39aace0467350a72eed4 to your computer and use it in GitHub Desktop.
Creating interactive shell session 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'); | |
conn.shell((err, stream) => { | |
if (err) throw err; | |
stream.on('close', () => { | |
console.log('STREAM HAS CLOSED'); | |
conn.end(); | |
}).on('data', (data) => { //Sever ==> Client | |
console.log('OUTPUT: ' + data); | |
}); | |
stream.write("ls -la\n"); //Run the "ls -la" command. | |
stream.write("pwd\n"); //Run the "pwd" command. | |
stream.write("cd ..\n"); //Run the "cd .." command. | |
conn.end(); //Close the connection. | |
}); | |
}).connect({ | |
host: '54.237.141.196', | |
port: 22, | |
username: 'ec2-user', | |
privateKey: readFileSync('path\\to\\key') //Must be a ".pem" key. | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment