PowerBuilder
PowerBuilder
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 PowerBuilder Downloads
integer li_rc
integer li_Success
oleobject loo_Tunnel
integer li_SshPort
string ls_Password
integer li_WaitForThreadExit
li_Success = 0
// 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.
loo_Tunnel = create oleobject
li_rc = loo_Tunnel.ConnectToNewObject("Chilkat.SshTunnel")
if li_rc < 0 then
destroy loo_Tunnel
MessageBox("Error","Connecting to COM object failed")
return
end if
// The fixed destination that accepted connections are forwarded to.
loo_Tunnel.DestHostname = "db.internal.example.com"
loo_Tunnel.DestPort = 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).
loo_Tunnel.ListenBindIpAddress = "127.0.0.1"
li_SshPort = 22
li_Success = loo_Tunnel.Connect("ssh.example.com",li_SshPort)
if li_Success = 0 then
Write-Debug loo_Tunnel.LastErrorText
destroy loo_Tunnel
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_Tunnel.AuthenticatePw("mySshLogin",ls_Password)
if li_Success = 0 then
Write-Debug loo_Tunnel.LastErrorText
destroy loo_Tunnel
return
end if
// Pass 0 to let the operating system choose an available port. After BeginAccepting succeeds,
// ListenPort contains the actual port that was bound.
li_Success = loo_Tunnel.BeginAccepting(0)
if li_Success = 0 then
Write-Debug loo_Tunnel.LastErrorText
destroy loo_Tunnel
return
end if
Write-Debug "Listening on 127.0.0.1 port " + string(loo_Tunnel.ListenPort)
// Applications now connect to 127.0.0.1:ListenPort as if it were the database server.
li_WaitForThreadExit = 1
li_Success = loo_Tunnel.CloseTunnel(li_WaitForThreadExit)
if li_Success = 0 then
Write-Debug loo_Tunnel.LastErrorText
destroy loo_Tunnel
return
end if
destroy loo_Tunnel