Skip to content
This repository was archived by the owner on Aug 26, 2025. It is now read-only.

Commit 55c9368

Browse files
authored
Merge pull request #13 from Stocard/customPorts
Custom ports
2 parents 1a21bd6 + 0161c20 commit 55c9368

File tree

4 files changed

+19
-6
lines changed

4 files changed

+19
-6
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ Options are an object with following properties:
8686
* `privateKey` (optional): Can be a `string` or `Buffer` that contains a private key. If not set, it fallbacks to `~/.ssh/id_rsa`
8787
* `agentForward` (optional): Is a `boolean` which uses the `ssh-agent` for connection (defaults to `false`.
8888
* `endHost` (required): The host you want to end up on (connect to)
89+
* `endPort` (optional): Port number of the server. Needed in case the server runs on a custom port (defaults to `22`)
8990
* `bastionHost` (optional): You can specify a bastion host if you want
9091
* `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.
9192

src/Connection.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,11 @@ import * as readline from 'readline'
2424

2525
interface Options {
2626
username?: string
27-
privateKey?: string | Buffer,
28-
agentForward? : boolean,
29-
bastionHost?: string,
27+
privateKey?: string | Buffer
28+
agentForward? : boolean
29+
bastionHost?: string
3030
passphrase?: string
31+
endPort?: number
3132
endHost: string
3233
}
3334

@@ -46,6 +47,9 @@ class SSHConnection {
4647
if (!options.username) {
4748
this.options.username = process.env['SSH_USERNAME'] || process.env['USER']
4849
}
50+
if (!options.endPort) {
51+
this.options.endPort = 22
52+
}
4953
if (!options.privateKey) {
5054
this.options.privateKey = fs.readFileSync(`${os.homedir()}${path.sep}.ssh${path.sep}id_rsa`)
5155
}
@@ -114,7 +118,7 @@ class SSHConnection {
114118
private async connectViaBastion(bastionHost: string) {
115119
const connectionToBastion = await this.connect(bastionHost)
116120
return new Promise<Client>((resolve, reject) => {
117-
connectionToBastion.exec(`nc ${this.options.endHost} 22`, async (err, stream) => {
121+
connectionToBastion.exec(`nc ${this.options.endHost} ${this.options.endPort}`, async (err, stream) => {
118122
if (err) {
119123
return reject(err)
120124
}

test/server/Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@ COPY ./server/sshd_config /etc/ssh/sshd_config
1212
COPY ./keys/authorized_keys /root/.ssh/authorized_keys
1313

1414

15-
EXPOSE 22
16-
CMD /usr/sbin/sshd -D
15+
EXPOSE 22 23
16+
CMD /usr/sbin/sshd -D -p 22 -p 23

test/test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,13 @@ describe('node-ssh-forward', async () => {
3535
})
3636
await ssh.executeCommand('uptime')
3737
})
38+
it('with a custom end port', async () => {
39+
const ssh = new SSHConnection({
40+
username: 'root',
41+
endHost: 'server',
42+
endPort: 23
43+
})
44+
await ssh.executeCommand('uptime')
45+
})
3846
})
3947
})

0 commit comments

Comments
 (0)