ssh2 icon indicating copy to clipboard operation
ssh2 copied to clipboard

SSH Tunnel Port Forwarding

Open gach87 opened this issue 9 years ago • 4 comments

Hello, i'm trying to do a version of this command with ssh2.client, tried with fordward out but it didn't work.

The command: ssh -fNg -L local-port:localhost:remote-port username@server;

The code i tried:

conn.on('ready', () => {
        conn.forwardOut(
            'localhost',
           localPort,
            server,
            remotePort, function (err, stream) {
                if (err) throw err;                
    }).connect({
        host: server
        port: 22
        username: username
        privateKey: sshKey
    });

When i check with lsof -i -P i dont see the local port.

Thanks in advance

gach87 avatar Jun 15 '17 14:06 gach87

That functionality isn't provided out of the box by this module. However there are other modules on npm that build on top of ssh2 that provide the local TCP port forwarding. All that's basically needed is net.createServer() and creating a new ssh connection per incoming socket, where you call conn.forwardOut() appropriately and then pipe between the local socket and the forwarded stream you get in your conn.forwardOut() callback.

mscdex avatar Jun 15 '17 14:06 mscdex

Thanks for the quick reply, could you point me out some of those modules?

Great software BTW.

gach87 avatar Jun 15 '17 15:06 gach87

Just search npm's site for 'ssh tunnel' and you will find some. I have not personally used any of them, but here are a few of examples found during a quick search: tunnel-ssh, open-ssh-tunnel, and adit.

mscdex avatar Jun 15 '17 16:06 mscdex

Hi @gach87, Did you manage to find a solution for the local port forward? I have a similar problem, I use plink for windows instead of ssh.

aberbegall avatar Sep 16 '17 18:09 aberbegall