What is @types/ssh2?
@types/ssh2 provides TypeScript definitions for the ssh2 library, which is a client and server implementation of the SSH2 protocol. This package allows developers to use the ssh2 library with TypeScript, providing type safety and autocompletion features.
What are @types/ssh2's main functionalities?
Establishing an SSH Connection
This code sample demonstrates how to establish an SSH connection to a remote server and execute a command ('uptime') on that server. The connection is established using the ssh2 library, and the TypeScript definitions provided by @types/ssh2 ensure type safety.
const Client = require('ssh2').Client;
const conn = new Client();
conn.on('ready', () => {
console.log('Client :: ready');
conn.exec('uptime', (err, stream) => {
if (err) throw err;
stream.on('close', (code, signal) => {
console.log('Stream :: close :: code: ' + code + ', signal: ' + signal);
conn.end();
}).on('data', (data) => {
console.log('STDOUT: ' + data);
}).stderr.on('data', (data) => {
console.log('STDERR: ' + data);
});
});
}).connect({
host: '127.0.0.1',
port: 22,
username: 'frylock',
privateKey: require('fs').readFileSync('/here/is/my/key')
});
Setting up an SSH Server
This code sample demonstrates how to set up an SSH server that listens for incoming connections. The server authenticates clients using a username and password, and allows them to execute commands. The TypeScript definitions provided by @types/ssh2 ensure type safety for the ssh2 library's server functionality.
const { Server } = require('ssh2');
const fs = require('fs');
const server = new Server({
hostKeys: [fs.readFileSync('host.key')]
}, (client) => {
console.log('Client connected!');
client.on('authentication', (ctx) => {
if (ctx.method === 'password' && ctx.username === 'foo' && ctx.password === 'bar')
ctx.accept();
else
ctx.reject();
}).on('ready', () => {
console.log('Client authenticated!');
client.on('session', (accept, reject) => {
const session = accept();
session.on('exec', (accept, reject, info) => {
console.log('Client wants to execute: ' + info.command);
const stream = accept();
stream.stderr.write('Oh no, the dreaded errors!
');
stream.write('Just kidding about the errors!
');
stream.exit(0);
stream.end();
});
});
}).on('end', () => {
console.log('Client disconnected');
});
}).listen(22, '127.0.0.1', () => {
console.log('Listening on port 22');
});
Other packages similar to @types/ssh2
node-ssh
node-ssh is a simpler, higher-level library for SSH connections in Node.js. It provides a more user-friendly API compared to ssh2, but may lack some of the advanced features and flexibility of ssh2.
ssh2-promise
ssh2-promise is a wrapper around ssh2 that provides a promise-based API. This can make it easier to work with asynchronous operations compared to the callback-based API of ssh2.
simple-ssh
simple-ssh is another high-level library for SSH connections in Node.js. It focuses on simplicity and ease of use, but may not offer the same level of control and customization as ssh2.