autossh/README.md

158 lines
3.5 KiB
Markdown
Raw Normal View History

2016-04-07 19:56:14 -06:00
# autossh
2016-04-09 02:30:10 -06:00
Persistent SSH tunnels for Node.js
2016-04-07 19:56:14 -06:00
### Install
Using npm
```
npm i -S autossh
```
### Usage
#### To Start
``` javascript
const autossh = require('autossh');
autossh({
host: '111.22.333.444',
username: 'root',
localPort: 64444,
remotePort: 5432
});
```
...is equivalent to...
``` bash
2016-04-08 14:33:16 -06:00
ssh -NL 64444:localhost:5432 -o "ExitOnForwardFailure yes" root@111.22.333.444
2016-04-07 19:56:14 -06:00
```
2016-04-08 18:24:37 -06:00
#### 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
``` javascript
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);
});
```
2016-04-08 14:28:58 -06:00
#### Generate Dynamic Local Port
If you want to dynamically/randomly generate a port number, provide a string `auto` for the `localPort`.
2016-04-28 13:39:14 -06:00
The major benefit is that port conflicts will automatically be avoided--the generated port will not have been in use.
2016-04-08 14:28:58 -06:00
2016-04-28 13:39:14 -06:00
The generated `localPort` can be accessed from the connection object as `localPort`.
``` javascript
const myAutossh = autossh({
host: '111.22.333.444',
username: 'root',
localPort: 64444,
remotePort: 5432
})
.on('connect', connection => {
console.log('connected: ', connection);
console.log('localPort: ', connection.localPort);
});
```
2016-04-08 14:28:58 -06:00
#### Killing the Autossh Process
2016-04-07 19:56:14 -06:00
The autossh process will automatically die if the node process is closed, but you can manually kill the process using `kill`.
2016-04-08 14:28:58 -06:00
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**
2016-04-07 19:56:14 -06:00
``` javascript
const myAutossh = autossh({
host: '111.22.333.444',
username: 'root',
localPort: 64444,
remotePort: 5432
})
2016-04-08 14:28:58 -06:00
.on('connect', connection => {
console.log('connected: ', connection);
2016-04-07 19:56:14 -06:00
});
myAutossh.kill();
```
2016-04-08 14:28:58 -06:00
**Example 2**
``` javascript
2016-04-09 02:30:10 -06:00
autossh({
2016-04-08 14:28:58 -06:00
host: '111.22.333.444',
username: 'root',
localPort: 64444,
remotePort: 5432
})
.on('connect', connection => {
console.log('connected: ', connection);
connection.kill();
});
```
2016-04-28 13:39:14 -06:00
#### Adjusting/Disabling Max Poll Count
When first trying to establish the ssh tunnel, `autoshh` will poll the local port until the connection has been established. The default max poll count is `30`.
**Adjusting the max poll count**
Set the `maxPollCount` property in the object passed to `autossh`:
```javascript
const myAutossh = autossh({
host: '111.22.333.444',
username: 'root',
localPort: 'auto',
remotePort: 5432,
maxPollCount: 50
})
.on('connect', connection => {
console.log('connected: ', connection);
});
```
**Disabling the max poll count**
Set the `maxPollCount` property to `0` or `false` in the object passed to `autossh`:
```javascript
const myAutossh = autossh({
host: '111.22.333.444',
username: 'root',
localPort: 'auto',
remotePort: 5432,
maxPollCount: false
})
.on('connect', connection => {
console.log('connected: ', connection);
});
```
**Warning:** The max poll count is there to prevent `autossh` from infinitely polling the local port. Rather than disabling it, it may be wise to set it to a high number (e.g. `500`).