PowerBuilder
PowerBuilder
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 PowerBuilder Downloads
integer li_rc
integer li_Success
oleobject loo_Ssh
integer li_SshPort
string ls_Password
string ls_TargetHostname
integer li_TargetPort
integer li_ChannelNum
li_Success = 0
// 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.
loo_Ssh = create oleobject
li_rc = loo_Ssh.ConnectToNewObject("Chilkat.Ssh")
if li_rc < 0 then
destroy loo_Ssh
MessageBox("Error","Connecting to COM object failed")
return
end if
li_SshPort = 22
li_Success = loo_Ssh.Connect("ssh.example.com",li_SshPort)
if li_Success = 0 then
Write-Debug loo_Ssh.LastErrorText
destroy loo_Ssh
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_Ssh.AuthenticatePw("mySshLogin",ls_Password)
if li_Success = 0 then
Write-Debug loo_Ssh.LastErrorText
destroy loo_Ssh
return
end if
// The target name is resolved by the SSH server, not by this computer, so it must be
// reachable from the server's network.
ls_TargetHostname = "db.internal.example.com"
li_TargetPort = 5432
li_ChannelNum = loo_Ssh.OpenDirectTcpIpChannel(ls_TargetHostname,li_TargetPort)
if li_ChannelNum < 0 then
Write-Debug loo_Ssh.LastErrorText
destroy loo_Ssh
return
end if
Write-Debug "Opened direct-tcpip channel: " + string(li_ChannelNum)
// The channel now carries ordinary TCP data to the target, tunneled over SSH.
loo_Ssh.Disconnect()
destroy loo_Ssh