autossh/README.md

106 lines
2.1 KiB
Markdown
Raw Normal View History

2016-04-07 19:56:14 -06:00
# autossh
Persistent SSH tunnels
### 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`.
Port conflicts will automatically be avoided and the generated port will not be in use.
2016-04-08 14:34:39 -06:00
See `demo.js` for an example.
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
const myAutossh = autossh({
host: '111.22.333.444',
username: 'root',
localPort: 64444,
remotePort: 5432
})
.on('connect', connection => {
console.log('connected: ', connection);
connection.kill();
});
```