Sample code for 30+ languages & platforms
PureBasic

SSH Tunnel Static (Fixed-Destination) Port Forwarding

See more SSH Tunnel Examples

Demonstrates fixed-destination port forwarding with the DestHostname, DestPort, ListenBindIpAddress, and ListenPort properties. Every local connection accepted by the listener is forwarded to the same fixed destination through the SSH server.

Background: This is the classic ssh -L scenario: expose a single remote service — a database, an internal web app — at a local port. Binding the listener to 127.0.0.1 keeps the tunnel private to the local machine, whereas 0.0.0.0 would let other hosts route through it. Passing 0 to BeginAccepting lets the OS pick a free port, which ListenPort then reports — handy when a fixed port might already be in use.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkSshTunnel.pb"

Procedure ChilkatExample()

    success.i = 0

    ;  Demonstrates fixed-destination (static) port forwarding with the SshTunnel properties
    ;  DestHostname, DestPort, ListenBindIpAddress, and ListenPort.
    ;  
    ;  Every local connection accepted by the listener is forwarded to the same fixed destination
    ;  through the SSH server.

    tunnel.i = CkSshTunnel::ckCreate()
    If tunnel.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    ;  The fixed destination that accepted connections are forwarded to.
    CkSshTunnel::setCkDestHostname(tunnel, "db.internal.example.com")
    CkSshTunnel::setCkDestPort(tunnel, 5432)

    ;  Bind the listener to loopback so only local processes can use the tunnel.  Use "0.0.0.0" to
    ;  accept connections from other machines (less secure).
    CkSshTunnel::setCkListenBindIpAddress(tunnel, "127.0.0.1")

    sshPort.i = 22
    success = CkSshTunnel::ckConnect(tunnel,"ssh.example.com",sshPort)
    If success = 0
        Debug CkSshTunnel::ckLastErrorText(tunnel)
        CkSshTunnel::ckDispose(tunnel)
        ProcedureReturn
    EndIf

    ;  Normally you would not hard-code the password in source.  You should instead obtain it
    ;  from an interactive prompt, environment variable, or a secrets vault.
    password.s = "mySshPassword"

    success = CkSshTunnel::ckAuthenticatePw(tunnel,"mySshLogin",password)
    If success = 0
        Debug CkSshTunnel::ckLastErrorText(tunnel)
        CkSshTunnel::ckDispose(tunnel)
        ProcedureReturn
    EndIf

    ;  Pass 0 to let the operating system choose an available port.  After BeginAccepting succeeds,
    ;  ListenPort contains the actual port that was bound.
    success = CkSshTunnel::ckBeginAccepting(tunnel,0)
    If success = 0
        Debug CkSshTunnel::ckLastErrorText(tunnel)
        CkSshTunnel::ckDispose(tunnel)
        ProcedureReturn
    EndIf

    Debug "Listening on 127.0.0.1 port " + Str(CkSshTunnel::ckListenPort(tunnel))

    ;  Applications now connect to 127.0.0.1:ListenPort as if it were the database server.

    waitForThreadExit.i = 1
    success = CkSshTunnel::ckCloseTunnel(tunnel,waitForThreadExit)
    If success = 0
        Debug CkSshTunnel::ckLastErrorText(tunnel)
        CkSshTunnel::ckDispose(tunnel)
        ProcedureReturn
    EndIf



    CkSshTunnel::ckDispose(tunnel)


    ProcedureReturn
EndProcedure