mirror of
https://github.com/vale981/autossh
synced 2025-03-04 17:01:41 -05:00
add ability to set 'sshPort' to use a different port thn 22
This commit is contained in:
parent
11741caf5a
commit
e94af46fd8
3 changed files with 88 additions and 21 deletions
16
README.md
16
README.md
|
@ -209,4 +209,18 @@ autossh({
|
|||
});
|
||||
```
|
||||
|
||||
**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`).
|
||||
**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`).
|
||||
|
||||
#### Specifying a Different SSH Port
|
||||
|
||||
The designated port for SSH according to the Transmission Control Protocol (TCP) is port 22, but you can specify a different port if you are using a different port. Set the `sshPort` property in the object you pass to `autossh`.
|
||||
|
||||
```javascript
|
||||
autossh({
|
||||
host: '111.22.333.444',
|
||||
username: 'root',
|
||||
localPort: 'auto',
|
||||
remotePort: 5432,
|
||||
sshPort: 9999
|
||||
});
|
||||
```
|
53
index.js
53
index.js
|
@ -49,6 +49,8 @@ var AutoSSH = function (_EventEmitter) {
|
|||
|
||||
_this.serverAliveCountMax = typeof conf.serverAliveCountMax === 'number' ? conf.serverAliveCountMax : 1;
|
||||
|
||||
_this.sshPort = typeof conf.sshPort === 'number' ? conf.sshPort : null;
|
||||
|
||||
_this.privateKey = conf.privateKey || null;
|
||||
|
||||
setImmediate(function () {
|
||||
|
@ -178,22 +180,50 @@ var AutoSSH = function (_EventEmitter) {
|
|||
/*
|
||||
*/
|
||||
|
||||
}, {
|
||||
key: 'generateDefaultOptions',
|
||||
value: function generateDefaultOptions() {
|
||||
var exitOnFailure = '-o ExitOnForwardFailure=yes';
|
||||
var strictHostCheck = '-o StrictHostKeyChecking=no';
|
||||
return exitOnFailure + ' ' + strictHostCheck;
|
||||
}
|
||||
|
||||
/*
|
||||
*/
|
||||
|
||||
}, {
|
||||
key: 'generateServerAliveOptions',
|
||||
value: function generateServerAliveOptions() {
|
||||
var serverAliveInterval = '-o ServerAliveInterval=' + this.serverAliveInterval;
|
||||
var serverAliveCountMax = '-o ServerAliveCountMax=' + this.serverAliveCountMax;
|
||||
return serverAliveInterval + ' ' + serverAliveCountMax;
|
||||
}
|
||||
|
||||
/*
|
||||
*/
|
||||
|
||||
}, {
|
||||
key: 'generateExecOptions',
|
||||
value: function generateExecOptions() {
|
||||
var serverAliveOpts = this.generateServerAliveOptions();
|
||||
var defaultOpts = this.generateDefaultOptions();
|
||||
var privateKey = this.privateKey ? '-i ' + this.privateKey : '';
|
||||
var sshPort = this.sshPort ? '-p ' + this.sshPort : '';
|
||||
|
||||
return defaultOpts + ' ' + serverAliveOpts + ' ' + privateKey + ' ' + sshPort;
|
||||
}
|
||||
|
||||
/*
|
||||
*/
|
||||
|
||||
}, {
|
||||
key: 'generateExecString',
|
||||
value: function generateExecString() {
|
||||
var bindAddress = this.localPort + ':localhost:' + this.remotePort;
|
||||
var exitOnFailure = '-o ExitOnForwardFailure=yes';
|
||||
var serverAliveInterval = '-o ServerAliveInterval=' + this.serverAliveInterval;
|
||||
var serverAliveCountMax = '-o ServerAliveCountMax=' + this.serverAliveCountMax;
|
||||
var serverAliveOpts = serverAliveInterval + ' ' + serverAliveCountMax;
|
||||
var strictHostCheck = '-o StrictHostKeyChecking=no';
|
||||
var options = exitOnFailure + ' ' + serverAliveOpts + ' ' + strictHostCheck;
|
||||
var privateKey = this.privateKey ? '-i ' + this.privateKey : '';
|
||||
var options = this.generateExecOptions();
|
||||
var userAtHost = this.username + '@' + this.host;
|
||||
|
||||
this.execString = 'ssh -NL ' + bindAddress + ' ' + options + ' ' + privateKey + ' ' + userAtHost;
|
||||
|
||||
return this.execString;
|
||||
return 'ssh -NL ' + bindAddress + ' ' + options + ' ' + userAtHost;
|
||||
}
|
||||
|
||||
/*
|
||||
|
@ -204,7 +234,8 @@ var AutoSSH = function (_EventEmitter) {
|
|||
value: function execTunnel(execTunnelCb) {
|
||||
var _this5 = this;
|
||||
|
||||
this.currentProcess = (0, _child_process.exec)(this.generateExecString(), function (execErr, stdout, stderr) {
|
||||
this.execString = this.generateExecString();
|
||||
this.currentProcess = (0, _child_process.exec)(this.execString, function (execErr, stdout, stderr) {
|
||||
if (/Address already in use/i.test(stderr)) {
|
||||
_this5.kill();
|
||||
_this5.emit('error', stderr);
|
||||
|
|
40
src/index.js
40
src/index.js
|
@ -25,6 +25,8 @@ class AutoSSH extends EventEmitter {
|
|||
this.serverAliveCountMax = typeof conf.serverAliveCountMax === 'number' ?
|
||||
conf.serverAliveCountMax : 1;
|
||||
|
||||
this.sshPort = typeof conf.sshPort === 'number' ? conf.sshPort : null;
|
||||
|
||||
this.privateKey = conf.privateKey || null;
|
||||
|
||||
setImmediate(() => {
|
||||
|
@ -148,26 +150,46 @@ class AutoSSH extends EventEmitter {
|
|||
|
||||
/*
|
||||
*/
|
||||
generateExecString() {
|
||||
const bindAddress = `${this.localPort}:localhost:${this.remotePort}`;
|
||||
generateDefaultOptions() {
|
||||
const exitOnFailure = '-o ExitOnForwardFailure=yes';
|
||||
const strictHostCheck = `-o StrictHostKeyChecking=no`;
|
||||
return `${exitOnFailure} ${strictHostCheck}`;
|
||||
}
|
||||
|
||||
/*
|
||||
*/
|
||||
generateServerAliveOptions() {
|
||||
const serverAliveInterval = `-o ServerAliveInterval=${this.serverAliveInterval}`;
|
||||
const serverAliveCountMax = `-o ServerAliveCountMax=${this.serverAliveCountMax}`;
|
||||
const serverAliveOpts = `${serverAliveInterval} ${serverAliveCountMax}`;
|
||||
const strictHostCheck = `-o StrictHostKeyChecking=no`;
|
||||
const options = `${exitOnFailure} ${serverAliveOpts} ${strictHostCheck}`;
|
||||
return `${serverAliveInterval} ${serverAliveCountMax}`;
|
||||
}
|
||||
|
||||
/*
|
||||
*/
|
||||
generateExecOptions() {
|
||||
const serverAliveOpts = this.generateServerAliveOptions();
|
||||
const defaultOpts = this.generateDefaultOptions();
|
||||
const privateKey = this.privateKey ? `-i ${this.privateKey}` : '';
|
||||
const sshPort = this.sshPort ? `-p ${this.sshPort}` : '';
|
||||
|
||||
return `${defaultOpts} ${serverAliveOpts} ${privateKey} ${sshPort}`;
|
||||
}
|
||||
|
||||
/*
|
||||
*/
|
||||
generateExecString() {
|
||||
const bindAddress = `${this.localPort}:localhost:${this.remotePort}`;
|
||||
const options = this.generateExecOptions();
|
||||
const userAtHost = `${this.username}@${this.host}`;
|
||||
|
||||
this.execString = `ssh -NL ${bindAddress} ${options} ${privateKey} ${userAtHost}`;
|
||||
|
||||
return this.execString;
|
||||
return `ssh -NL ${bindAddress} ${options} ${userAtHost}`;
|
||||
}
|
||||
|
||||
/*
|
||||
*/
|
||||
execTunnel(execTunnelCb) {
|
||||
this.currentProcess = exec(this.generateExecString(), (execErr, stdout, stderr) => {
|
||||
this.execString = this.generateExecString();
|
||||
this.currentProcess = exec(this.execString, (execErr, stdout, stderr) => {
|
||||
if (/Address already in use/i.test(stderr)) {
|
||||
this.kill();
|
||||
this.emit('error', stderr);
|
||||
|
|
Loading…
Add table
Reference in a new issue