mscdex /ssh2

SSH2 client and server modules written in pure JavaScript for node.js

★ 5,827⑂ 741👁 118⑃ 175● 1,347▤ 0
JavaScriptMITCreated 2012-08-03
+0
日增长
5,827
stars
741
forks
118
watchers
175
prs
1,347
issues
0
releases

README

Description

SSH2 client and server modules written in pure JavaScript for node.js.

Development/testing is done against OpenSSH (8.7 currently).

 Table of Contents

 Requirements
 Installation
 Client Examples
   Execute 'uptime' on a server
   Start an interactive shell session
   Send a raw HTTP request to port 80 on the server
   Forward local connections to port 8000 on the server to us
   Get a directory listing via SFTP
   Connection hopping
   Forward remote X11 connections
   Dynamic (1:1) port forwarding using a SOCKSv5 proxy (using socksv5)
   Make HTTP(S) connections easily using a custom http(s).Agent
   Invoke an arbitrary subsystem (e.g. netconf)
 Server Examples
   Password and public key authentication and non-interactive (exec) command execution
   SFTP-only server
 Other Examples
   Generate an SSH key
 API
   Client
       Client events
       Client methods
   Server
       Server events
       Server methods
       Connection events
       Connection methods
       Session events
   Channel
   Pseudo-TTY settings
   Terminal modes
   HTTPAgent
       HTTPAgent methods
   HTTPSAgent
       HTTPSAgent methods
   Utilities

 Requirements

 node.js -- v16.0.0 or newer
 (Optional) cpu-features is set as an optional package dependency (you do not need to install it explicitly/separately from ssh2) that will be automatically built and used if possible. See the project's documentation for its own requirements.
   This addon is currently used to help generate an optimal default cipher list

 Installation

    npm install ssh2

 Client Examples

 Execute 'uptime' on a server

js
const { readFileSync } = require('fs');

const { Client } = require('ssh2');

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: '192.168.100.100',
  port: 22,
  username: 'frylock',
  privateKey: readFileSync('/path/to/my/key')
});

// example output:
// Client :: ready
// STDOUT:  17:41:15 up 22 days, 18:09,  1 user,  load average: 0.00, 0.01, 0.05
//
// Stream :: exit :: code: 0, signal: undefined
// Stream :: close


 Start an interactive shell session

js
const { readFileSync } = require('fs');

const { Client } = require('ssh2');

const conn = new Client();
conn.on('ready', () = {
  console.log('Client :: ready');
  conn.shell((err, stream) = {
    if (err) throw err;
    stream.on('close', () = {
      console.log('Stream :: close');
      conn.end();
    }).on('data', (data) = {
      console.log('OUTPUT: ' + data);
    });
    stream.end('ls -l\nexit\n');
  });
}).connect({
  host: '192.168.100.100',
  port: 22,
  username: 'frylock',
  privateK

Top issues

Snapshot: 2026-11-16T09:23:14Z · Data license Apache-2.0 · JSON