Skip to content

Commit

Permalink
Merge pull request #13 from Stocard/customPorts
Browse files Browse the repository at this point in the history
Custom ports
  • Loading branch information
Junkern authored Mar 1, 2019
2 parents 1a21bd6 + 0161c20 commit 55c9368
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 6 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ Options are an object with following properties:
* `privateKey` (optional): Can be a `string` or `Buffer` that contains a private key. If not set, it fallbacks to `~/.ssh/id_rsa`
* `agentForward` (optional): Is a `boolean` which uses the `ssh-agent` for connection (defaults to `false`.
* `endHost` (required): The host you want to end up on (connect to)
* `endPort` (optional): Port number of the server. Needed in case the server runs on a custom port (defaults to `22`)
* `bastionHost` (optional): You can specify a bastion host if you want
* `passphrase` (optional): You can specify the passphrase when you have an encrypted private key. If you don't specify the passphrase and you use an encrypted private key, you get prompted in the command line.

Expand Down
12 changes: 8 additions & 4 deletions src/Connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ import * as readline from 'readline'

interface Options {
username?: string
privateKey?: string | Buffer,
agentForward? : boolean,
bastionHost?: string,
privateKey?: string | Buffer
agentForward? : boolean
bastionHost?: string
passphrase?: string
endPort?: number
endHost: string
}

Expand All @@ -46,6 +47,9 @@ class SSHConnection {
if (!options.username) {
this.options.username = process.env['SSH_USERNAME'] || process.env['USER']
}
if (!options.endPort) {
this.options.endPort = 22
}
if (!options.privateKey) {
this.options.privateKey = fs.readFileSync(`${os.homedir()}${path.sep}.ssh${path.sep}id_rsa`)
}
Expand Down Expand Up @@ -114,7 +118,7 @@ class SSHConnection {
private async connectViaBastion(bastionHost: string) {
const connectionToBastion = await this.connect(bastionHost)
return new Promise<Client>((resolve, reject) => {
connectionToBastion.exec(`nc ${this.options.endHost} 22`, async (err, stream) => {
connectionToBastion.exec(`nc ${this.options.endHost} ${this.options.endPort}`, async (err, stream) => {
if (err) {
return reject(err)
}
Expand Down
4 changes: 2 additions & 2 deletions test/server/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ COPY ./server/sshd_config /etc/ssh/sshd_config
COPY ./keys/authorized_keys /root/.ssh/authorized_keys


EXPOSE 22
CMD /usr/sbin/sshd -D
EXPOSE 22 23
CMD /usr/sbin/sshd -D -p 22 -p 23
8 changes: 8 additions & 0 deletions test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,13 @@ describe('node-ssh-forward', async () => {
})
await ssh.executeCommand('uptime')
})
it('with a custom end port', async () => {
const ssh = new SSHConnection({
username: 'root',
endHost: 'server',
endPort: 23
})
await ssh.executeCommand('uptime')
})
})
})

0 comments on commit 55c9368

Please sign in to comment.