A simple, ESM-first SSH client for Node.js with private key authentication and sequential command execution.
- 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
- Node.js 26 or newer
- An SSH server and private-key authentication
npm install @eliware/ssh-clientimport{sshExec}from'@eliware/ssh-client';constresults=awaitsshExec({host: 'your.ssh.server',username: 'youruser',// optional if same as local usercommands: ['echo Hello, SSH!','uname -a',],});for(const[i,{ result, code }]ofresults.entries()){console.log(`Command #${i+1} exit code: ${code}`);console.log(result);}Executes one or more commands on a remote SSH server using private key authentication.
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)
Promise<Array<{ result: string, code: number }>>: Resolves to an array of results for each command, with merged stdout/stderr and exit code.
- If connection or authentication fails, or if no private key is found in
~/.ssh/.
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.
npm test
npm run test:gaps
npm run lint
npm run typecheck
npm run packTreat 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.
Type definitions are included:
exportinterfaceSshExecOptions{host: string;port?: number;username?: string;commands: string[];}exportinterfaceSshExecResult{result: string;code: number;}exportdeclarefunctionsshExec(options: SshExecOptions): Promise<SshExecResult[]>;For help, questions, or to chat with the author and community, visit:


