Sample code for 30+ languages & platforms
PowerBuilder

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 PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_SshJump
integer li_SshPort
string ls_Password
oleobject loo_Tunnel
integer li_WaitForThreadExit

li_Success = 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.

loo_SshJump = create oleobject
li_rc = loo_SshJump.ConnectToNewObject("Chilkat.Ssh")
if li_rc < 0 then
    destroy loo_SshJump
    MessageBox("Error","Connecting to COM object failed")
    return
end if

//  Connect and authenticate to the jump host first.
li_SshPort = 22
li_Success = loo_SshJump.Connect("jump.example.com",li_SshPort)
if li_Success = 0 then
    Write-Debug loo_SshJump.LastErrorText
    destroy loo_SshJump
    return
end if

//  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.
ls_Password = "mySshPassword"

li_Success = loo_SshJump.AuthenticatePw("myJumpLogin",ls_Password)
if li_Success = 0 then
    Write-Debug loo_SshJump.LastErrorText
    destroy loo_SshJump
    return
end if

//  Set up the tunnel and connect to the target SSH server through the jump host.
loo_Tunnel = create oleobject
li_rc = loo_Tunnel.ConnectToNewObject("Chilkat.SshTunnel")

loo_Tunnel.DestHostname = "db.internal.example.com"
loo_Tunnel.DestPort = 5432

li_Success = loo_Tunnel.ConnectThroughSsh(loo_SshJump,"ssh.internal.example.com",li_SshPort)
if li_Success = 0 then
    Write-Debug loo_Tunnel.LastErrorText
    destroy loo_SshJump
    destroy loo_Tunnel
    return
end if

//  Authenticate to the target SSH server (its own credentials).
li_Success = loo_Tunnel.AuthenticatePw("mySshLogin",ls_Password)
if li_Success = 0 then
    Write-Debug loo_Tunnel.LastErrorText
    destroy loo_SshJump
    destroy loo_Tunnel
    return
end if

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

li_WaitForThreadExit = 1
li_Success = loo_Tunnel.CloseTunnel(li_WaitForThreadExit)
if li_Success = 0 then
    Write-Debug loo_Tunnel.LastErrorText
    destroy loo_SshJump
    destroy loo_Tunnel
    return
end if

loo_SshJump.Disconnect()


destroy loo_SshJump
destroy loo_Tunnel