A simple SSH2 wrapper to simplify SSH connections in TypeScript/JavaScript.
# Using pnpm
pnpm add @rnatsuki/myssh
# Using npm
npm install @rnatsuki/myssh
# Using yarn
yarn add @rnatsuki/myssh- Simple API for SSH operations
- Support for password and key-based authentication
- Automatic retry mechanism
- File upload/download via SFTP
- Interactive shell support
- TypeScript type definitions
- Support for SSH options (like
-o StrictHostKeyChecking=no)
// Equivalent to:
// ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no user@host
const ssh = new MySsh({
host: 'example.com',
username: 'user',
password: 'password',
// These two options disable host key checking
disableStrictHostKeyChecking: true,
ignoreKnownHosts: true
});
await ssh.connect();
const result = await ssh.exec('hostname');
console.log(result.stdout);
ssh.disconnect();import { MySsh } from 'myssh';
const ssh = new MySsh({
host: 'example.com',
port: 22,
username: 'user',
password: 'password'
});
// Connect to the server
await ssh.connect();
// Execute a command
const result = await ssh.exec('ls -la');
console.log(`Exit code: ${result.code}`);
console.log(`Output: ${result.stdout}`);
// Disconnect
ssh.disconnect();import { MySsh } from 'myssh';
import * as fs from 'fs';
const ssh = new MySsh({
host: 'example.com',
username: 'user',
privateKey: fs.readFileSync('/path/to/private/key')
});
await ssh.connect();
// ...const ssh = new MySsh({
host: 'example.com',
username: 'user',
useDefaultKey: true // Will use ~/.ssh/id_rsa automatically
});// Upload a file
await ssh.uploadFile('./local/file.txt', '/remote/path/file.txt');
// Download a file
await ssh.downloadFile('/remote/path/remote-file.txt', './local-file.txt');const stream = await ssh.shell();
// Handle data from the shell
stream.on('data', (data) => {
console.log('SHELL OUTPUT:', data.toString());
});
// Send commands to the shell
stream.write('ls -la\n');
// Close the shell when done
stream.close();const ssh = new MySsh({
host: 'example.com',
username: 'user',
password: 'password',
// Connection settings
timeout: 5000, // Connection timeout in ms (default: 10000)
retries: 3, // Number of retries on connection failure (default: 0)
retryDelay: 1000, // Delay between retries in ms (default: 2000)
// Logging
logger: console.log, // Custom logger function
// SSH options (similar to command-line -o options)
disableStrictHostKeyChecking: true, // Equivalent to -o StrictHostKeyChecking=no
ignoreKnownHosts: true, // Equivalent to -o UserKnownHostsFile=/dev/null
// Custom SSH options
sshOptions: {
"ServerAliveInterval": "60", // Keep connection alive
"ServerAliveCountMax": "3"
}
});The configuration above is equivalent to the following command-line SSH command:
ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o ServerAliveInterval=60 -o ServerAliveCountMax=3 user@example.comCreates a new SSH client instance.
| Option | Type | Description |
|---|---|---|
host |
string | Hostname or IP address (default: 'localhost') |
port |
number | Port number (default: 22) |
username |
string | SSH username |
password |
string | SSH password (optional) |
privateKey |
string | Buffer | Private key for authentication (optional) |
useDefaultKey |
boolean | Use ~/.ssh/id_rsa if available (default: false) |
timeout |
number | Connection timeout in ms (default: 10000) |
retries |
number | Number of connection retries (default: 0) |
retryDelay |
number | Delay between retries in ms (default: 2000) |
logger |
function | Custom logging function (optional) |
disableStrictHostKeyChecking |
boolean | Skip host key verification (equivalent to -o StrictHostKeyChecking=no) |
ignoreKnownHosts |
boolean | Don't use known_hosts file (equivalent to -o UserKnownHostsFile=/dev/null) |
sshOptions |
object | Custom SSH options as key-value pairs (equivalent to -o options) |
connect(): Connect to the SSH serverexec(command: string): Execute a command and return resultuploadFile(src: string, dest: string): Upload a file via SFTPdownloadFile(src: string, dest: string): Download a file via SFTPshell(): Get an interactive shelldisconnect(): Close the SSH connection
ISC