DataFlex
DataFlex
Open a direct-tcpip Channel (SSH Port Forwarding)
See more SSH Examples
Demonstrates the Chilkat Ssh.OpenDirectTcpIpChannel method, which opens a direct-tcpip channel through the SSH server to a target host and port. The first argument is the target hostname and the second is the target port. It returns the channel number, or -1 on failure.
Background: This is SSH local port forwarding at the API level: the channel carries ordinary TCP traffic to a service that may be reachable only from the SSH server's network — a database, an internal web app, a message broker. The key detail is that the server resolves the target name, not your machine, so it must resolve and be reachable there. Supplying a numeric IPv4 address avoids remote DNS resolution entirely.
Chilkat DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoSsh
Integer iSshPort
String sPassword
String sTargetHostname
Integer iTargetPort
Integer iChannelNum
String sTemp1
Move False To iSuccess
// Demonstrates the Ssh.OpenDirectTcpIpChannel method, which opens a direct-tcpip channel
// through the SSH server to a target host and port. The 1st argument is the target hostname
// and the 2nd is the target port. It returns the channel number, or -1 on failure.
Get Create (RefClass(cComChilkatSsh)) To hoSsh
If (Not(IsComObjectCreated(hoSsh))) Begin
Send CreateComObject of hoSsh
End
Move 22 To iSshPort
Get ComConnect Of hoSsh "ssh.example.com" iSshPort To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoSsh 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 hoSsh "mySshLogin" sPassword To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoSsh To sTemp1
Showln sTemp1
Procedure_Return
End
// The target name is resolved by the SSH server, not by this computer, so it must be
// reachable from the server's network.
Move "db.internal.example.com" To sTargetHostname
Move 5432 To iTargetPort
Get ComOpenDirectTcpIpChannel Of hoSsh sTargetHostname iTargetPort To iChannelNum
If (iChannelNum < 0) Begin
Get ComLastErrorText Of hoSsh To sTemp1
Showln sTemp1
Procedure_Return
End
Showln "Opened direct-tcpip channel: " iChannelNum
// The channel now carries ordinary TCP data to the target, tunneled over SSH.
Send ComDisconnect To hoSsh
End_Procedure