Sample code for 30+ languages & platforms
Visual FoxPro

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 Visual FoxPro Downloads

Visual FoxPro
LOCAL lnSuccess
LOCAL loSshJump
LOCAL lnSshPort
LOCAL lcPassword
LOCAL loTunnel
LOCAL lnWaitForThreadExit

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

loSshJump = CreateObject('Chilkat.Ssh')

*  Connect and authenticate to the jump host first.
lnSshPort = 22
lnSuccess = loSshJump.Connect("jump.example.com",lnSshPort)
IF (lnSuccess = 0) THEN
    ? loSshJump.LastErrorText
    RELEASE loSshJump
    CANCEL
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.
lcPassword = "mySshPassword"

lnSuccess = loSshJump.AuthenticatePw("myJumpLogin",lcPassword)
IF (lnSuccess = 0) THEN
    ? loSshJump.LastErrorText
    RELEASE loSshJump
    CANCEL
ENDIF

*  Set up the tunnel and connect to the target SSH server through the jump host.
loTunnel = CreateObject('Chilkat.SshTunnel')
loTunnel.DestHostname = "db.internal.example.com"
loTunnel.DestPort = 5432

lnSuccess = loTunnel.ConnectThroughSsh(loSshJump,"ssh.internal.example.com",lnSshPort)
IF (lnSuccess = 0) THEN
    ? loTunnel.LastErrorText
    RELEASE loSshJump
    RELEASE loTunnel
    CANCEL
ENDIF

*  Authenticate to the target SSH server (its own credentials).
lnSuccess = loTunnel.AuthenticatePw("mySshLogin",lcPassword)
IF (lnSuccess = 0) THEN
    ? loTunnel.LastErrorText
    RELEASE loSshJump
    RELEASE loTunnel
    CANCEL
ENDIF

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

lnWaitForThreadExit = 1
lnSuccess = loTunnel.CloseTunnel(lnWaitForThreadExit)
IF (lnSuccess = 0) THEN
    ? loTunnel.LastErrorText
    RELEASE loSshJump
    RELEASE loTunnel
    CANCEL
ENDIF

loSshJump.Disconnect()

RELEASE loSshJump
RELEASE loTunnel