DataFlex
DataFlex
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 DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Variant vSshJump
Handle hoSshJump
Integer iSshPort
String sPassword
Handle hoTunnel
Boolean iWaitForThreadExit
String sTemp1
Move False To iSuccess
// 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.
Get Create (RefClass(cComChilkatSsh)) To hoSshJump
If (Not(IsComObjectCreated(hoSshJump))) Begin
Send CreateComObject of hoSshJump
End
// Connect and authenticate to the jump host first.
Move 22 To iSshPort
Get ComConnect Of hoSshJump "jump.example.com" iSshPort To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoSshJump To sTemp1
Showln sTemp1
Procedure_Return
End
// 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.
Move "mySshPassword" To sPassword
Get ComAuthenticatePw Of hoSshJump "myJumpLogin" sPassword To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoSshJump To sTemp1
Showln sTemp1
Procedure_Return
End
// Set up the tunnel and connect to the target SSH server through the jump host.
Get Create (RefClass(cComChilkatSshTunnel)) To hoTunnel
If (Not(IsComObjectCreated(hoTunnel))) Begin
Send CreateComObject of hoTunnel
End
Set ComDestHostname Of hoTunnel To "db.internal.example.com"
Set ComDestPort Of hoTunnel To 5432
Get pvComObject of hoSshJump to vSshJump
Get ComConnectThroughSsh Of hoTunnel vSshJump "ssh.internal.example.com" iSshPort To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoTunnel To sTemp1
Showln sTemp1
Procedure_Return
End
// Authenticate to the target SSH server (its own credentials).
Get ComAuthenticatePw Of hoTunnel "mySshLogin" sPassword To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoTunnel To sTemp1
Showln sTemp1
Procedure_Return
End
// After authenticating, call BeginAccepting to start listening for local connections to forward.
Move True To iWaitForThreadExit
Get ComCloseTunnel Of hoTunnel iWaitForThreadExit To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoTunnel To sTemp1
Showln sTemp1
Procedure_Return
End
Send ComDisconnect To hoSshJump
End_Procedure