Skip to content

Latest commit

 

History

History
139 lines (95 loc) · 3.89 KB

File metadata and controls

139 lines (95 loc) · 3.89 KB

eliware.org

@eliware/ssh-client npm versionlicensebuild status

A simple, ESM-first SSH client for Node.js with private key authentication and sequential command execution.


Table of Contents

Features

  • Simple SSH command execution for Node.js
  • Private key authentication only (no password support)
  • Sequential execution of multiple commands in a single SSH session
  • Returns merged stdout/stderr and exit code for each command
  • TypeScript type definitions included
  • Fully ESM compatible
  • Easily testable/mocked via dependency injection

Requirements

  • Node.js 26 or newer
  • An SSH server and private-key authentication

Installation

npm install @eliware/ssh-client

Usage

ESM Example

import { sshExec } from '@eliware/ssh-client';

const results = await sshExec({
  host: 'your.ssh.server',
  username: 'youruser', // optional if same as local user
  commands: [
    'echo Hello, SSH!',
    'uname -a',
  ],
});

for (const [i, { result, code }] of results.entries()) {
  console.log(`Command #${i + 1} exit code: ${code}`);
  console.log(result);
}

API

sshExec(options)

Executes one or more commands on a remote SSH server using private key authentication.

Parameters

  • host (string): Hostname or IP address (required)
  • port (number): SSH port (default: 22)
  • username (string): SSH username (default: current user)
  • commands (string[]): List of commands to execute (required)

Returns

  • Promise<Array<{ result: string, code: number }>>: Resolves to an array of results for each command, with merged stdout/stderr and exit code.

Throws

  • If connection or authentication fails, or if no private key is found in ~/.ssh/.

Errors / Troubleshooting

sshExec validates the host, command list, and port before connecting. It throws SshError with codes for invalid options, missing keys, authentication failures, connection failures, connection timeouts, command failures, and command timeouts. Configure host verification with hostVerifier or knownHosts; do not weaken verification defaults in production.

Development

npm test
npm run test:gaps
npm run lint
npm run typecheck
npm run pack

Security

Treat private keys, passphrases, agents, host credentials, and command content as sensitive. Never commit keys or credentials. Prefer knownHosts/hostVerifier, limit command scope, and avoid logging command output containing secrets.

TypeScript

Type definitions are included:

export interface SshExecOptions {
  host: string;
  port?: number;
  username?: string;
  commands: string[];
}

export interface SshExecResult {
  result: string;
  code: number;
}

export declare function sshExec(options: SshExecOptions): Promise<SshExecResult[]>;

Support

For help, questions, or to chat with the author and community, visit:

Discordeliware.org

eliware.org on Discord

License

MIT © 2025 Eli Sterling, eliware.org

Links