A library that allows you to start a local sshd daemon that you can test against. It is based off of Patrick Debois' original test-sshd library.
Because there is nothing like the real thing
Way too many codebases that uses ssh/sftp aren't being tested because mocking and stubbing sshd servers is hard.
It starts an ssh server on the localhost such that:
- you can login as the current user (
process.env.USER) - using the test key provided (included in the package at
config/keys/id_rsa) - on a port you specify
- with one of three modes:
- (mode:
echo) if login succeeds it usesForceCommandto echo the command in$SSH_ORIGINAL_COMMAND - (mode:
exec) if login succeeds it executes the command - (mode:
transfer) if login succeeds it allows forsftptransfers
- (mode:
- it does NOT do password authentication. You must use a private key
import { TestSSHD } from 'test-sshdng';
const sshd = TestSSHD({port: 4000};
const connectParams = sshd.connectParams();
sshd.on('ready', () => {
console.log('ready to login');
});
sshd.on('error', (error) => {
// When sshd has an error
});
sshd.on('stdout', (data) => {
// receive sshd stdout
});
sshd.on('stderr', (data) => {
// receive sshd stderr
});
sshd.on('exit', () => {
// when daemon exit
});
sshd.start();
// Make sure to stop the ssh server when the process exits
// This prevents orphaned processes
process.on('exit', function() {
if(sshd.status === 'started') {
sshd.stop(); // this returns a promise if you want to wait for it to resolve
}
});
status: either 'started' or 'stopped'settings: contains params used to initialize the ssh server
port: integer defaults to4000mode: string default toecho(other options areexec,transfer)
ready: when the ssh server is successfully listeningexit: when the ssh server exitserror: when an error occurs
Returns an object with the following members:
host: string host to connect tousername: string username to connect withport: integer port it listens onprivateKey: string key that can be used to connect
See LICENSE.md.