Sample code for 30+ languages & platforms
PureBasic

Connect an SSH Tunnel Through a Jump Host

See more SSH Tunnel Examples

Demonstrates the Chilkat SshTunnel.ConnectThroughSsh method, which connects to an SSH server through an already connected and authenticated Ssh object. The first argument is the jump-host Ssh object, and the second and third are the destination SSH hostname and port.

Background: This chains the tunnel through a "jump host" (bastion): the application reaches a gateway server, then tunnels onward to an SSH server that is only accessible from inside the network. Each hop authenticates separately with its own credentials. The result is a normal SshTunnel — still requiring its own authentication and BeginAccepting — whose transport is routed through the first connection.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkSshTunnel.pb"
IncludeFile "CkSsh.pb"

Procedure ChilkatExample()

    success.i = 0

    ;  Demonstrates the SshTunnel.ConnectThroughSsh method, which connects to an SSH server through
    ;  an already connected and authenticated Ssh object (a jump host).  The 1st argument is the Ssh
    ;  object, and the 2nd and 3rd are the destination SSH hostname and port.

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

    ;  Connect and authenticate to the jump host first.
    sshPort.i = 22
    success = CkSsh::ckConnect(sshJump,"jump.example.com",sshPort)
    If success = 0
        Debug CkSsh::ckLastErrorText(sshJump)
        CkSsh::ckDispose(sshJump)
        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 = CkSsh::ckAuthenticatePw(sshJump,"myJumpLogin",password)
    If success = 0
        Debug CkSsh::ckLastErrorText(sshJump)
        CkSsh::ckDispose(sshJump)
        ProcedureReturn
    EndIf

    ;  Set up the tunnel and connect to the target SSH server through the jump host.
    tunnel.i = CkSshTunnel::ckCreate()
    If tunnel.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkSshTunnel::setCkDestHostname(tunnel, "db.internal.example.com")
    CkSshTunnel::setCkDestPort(tunnel, 5432)

    success = CkSshTunnel::ckConnectThroughSsh(tunnel,sshJump,"ssh.internal.example.com",sshPort)
    If success = 0
        Debug CkSshTunnel::ckLastErrorText(tunnel)
        CkSsh::ckDispose(sshJump)
        CkSshTunnel::ckDispose(tunnel)
        ProcedureReturn
    EndIf

    ;  Authenticate to the target SSH server (its own credentials).
    success = CkSshTunnel::ckAuthenticatePw(tunnel,"mySshLogin",password)
    If success = 0
        Debug CkSshTunnel::ckLastErrorText(tunnel)
        CkSsh::ckDispose(sshJump)
        CkSshTunnel::ckDispose(tunnel)
        ProcedureReturn
    EndIf

    ;  After authenticating, call BeginAccepting to start listening for local connections to forward.

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

    CkSsh::ckDisconnect(sshJump)


    CkSsh::ckDispose(sshJump)
    CkSshTunnel::ckDispose(tunnel)


    ProcedureReturn
EndProcedure