No description
Find a file
2016-04-08 18:24:37 -06:00
src adds auto port generation 2016-04-08 14:28:58 -06:00
.babelrc initial commit 2016-04-07 19:22:13 -06:00
.gitignore adds readme 2016-04-07 19:56:14 -06:00
demo.js change username to root 2016-04-08 14:36:10 -06:00
index.js adds auto port generation 2016-04-08 14:28:58 -06:00
package.json updates to version 0.0.4 2016-04-08 14:29:19 -06:00
README.md more thorough docs 2016-04-08 18:24:37 -06:00

autossh

Persistent SSH tunnels

Install

Using npm

npm i -S autossh

Usage

To Start

const autossh = require('autossh');

autossh({
  host: '111.22.333.444',
  username: 'root',
  localPort: 64444,
  remotePort: 5432
});

...is equivalent to...

ssh -NL 64444:localhost:5432 -o "ExitOnForwardFailure yes" root@111.22.333.444

Event Listeners

Autossh inherits from node.js's EventEmitter, and implements two events: error, connect

error

The error event will fire anytime there is an error throughout the life of the autossh process.

connect

The connect event will fire only once when the initial ssh connection is made

autossh({
  host: '111.22.333.444',
  username: 'root',
  localPort: 64444,
  remotePort: 5432
})
.on('error', err => {
  console.error('ERROR: ', err);
})
.on('connect', connection => {
  console.log('Tunnel established on port ' + connection.localPort);
  console.log('pid: ' + connection.pid);
});

Generate Dynamic Local Port

If you want to dynamically/randomly generate a port number, provide a string auto for the localPort.

Port conflicts will automatically be avoided and the generated port will not be in use.

See demo.js for an example.

Killing the Autossh Process

The autossh process will automatically die if the node process is closed, but you can manually kill the process using kill.

If you try to kill the ssh process from the command line while the node process is active, a new ssh tunnel will be established (which is the point of autossh). You will need to kill the node process first or call the kill method on the instance.

Example 1

const myAutossh = autossh({
  host: '111.22.333.444',
  username: 'root',
  localPort: 64444,
  remotePort: 5432
})
.on('connect', connection => {
  console.log('connected: ', connection);
});

myAutossh.kill();

Example 2

const myAutossh = autossh({
  host: '111.22.333.444',
  username: 'root',
  localPort: 64444,
  remotePort: 5432
})
.on('connect', connection => {
  console.log('connected: ', connection);
  connection.kill();
});