+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',
privateKTop issues
#551Feature Request: support certificate authentication. 17 comments · 2017-04-30#927Timed out while waiting for handshake stuck on Outgoing: Writing KEXINIT15 comments · 2020-09-08#1184SFTP Missing finish event11 comments · 2022-05-24#1380SSH Client fails handshake when using publickey method10 comments · 2024-03-21#1206Support parsing "ecdsa-sk" and "ed25519-sk" SSH keys10 comments · 2022-08-20#1443[SFTP Server] Client hangs on exit9 comments · 2025-02-08#333Allow GSSAPI authentication methods8 comments · 2015-10-20#1175Missing callback when SFTP server disconnects during sftp operation7 comments · 2022-05-02