DataFlex
DataFlex
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 DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoTunnel
Integer iSshPort
String sPassword
Boolean iWaitForThreadExit
String sTemp1
Integer iTemp1
Move False To iSuccess
// 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.
Get Create (RefClass(cComChilkatSshTunnel)) To hoTunnel
If (Not(IsComObjectCreated(hoTunnel))) Begin
Send CreateComObject of hoTunnel
End
// The fixed destination that accepted connections are forwarded to.
Set ComDestHostname Of hoTunnel To "db.internal.example.com"
Set ComDestPort Of hoTunnel To 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).
Set ComListenBindIpAddress Of hoTunnel To "127.0.0.1"
Move 22 To iSshPort
Get ComConnect Of hoTunnel "ssh.example.com" iSshPort To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoTunnel 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 hoTunnel "mySshLogin" sPassword To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoTunnel To sTemp1
Showln sTemp1
Procedure_Return
End
// Pass 0 to let the operating system choose an available port. After BeginAccepting succeeds,
// ListenPort contains the actual port that was bound.
Get ComBeginAccepting Of hoTunnel 0 To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoTunnel To sTemp1
Showln sTemp1
Procedure_Return
End
Get ComListenPort Of hoTunnel To iTemp1
Showln "Listening on 127.0.0.1 port " iTemp1
// Applications now connect to 127.0.0.1:ListenPort as if it were the database server.
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
End_Procedure