PowerBuilder
PowerBuilder
Start an SSH Tunnel Listener (BeginAccepting)
See more SSH Tunnel Examples
Demonstrates the Chilkat SshTunnel.BeginAccepting method, which creates and starts the local listener on the given port. The only argument is the listen port. Connect and authenticate first — clients accepted before authentication cannot be forwarded. The IsAccepting property reports whether the listener is running.
Background: This is the step that actually turns the object into a working tunnel: it spawns a background thread that accepts local TCP connections and forwards each through the authenticated SSH transport. Because the forwarding runs on its own thread, control returns to your program immediately and the tunnel operates while the rest of the application does other work — which is why a separate
CloseTunnel is needed to shut it down cleanly.Chilkat PowerBuilder Downloads
integer li_rc
integer li_Success
oleobject loo_Tunnel
integer li_SshPort
string ls_Password
integer li_ListenPort
integer li_WaitForThreadExit
li_Success = 0
// Demonstrates the SshTunnel.BeginAccepting method, which creates and starts the local listener
// on the given port. The only argument is the listen port. Connect and authenticate first;
// clients accepted before authentication cannot be forwarded.
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
loo_Tunnel.DestHostname = "db.internal.example.com"
loo_Tunnel.DestPort = 5432
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
// Start the local listener. Local TCP connections to this port are forwarded to DestHostname:DestPort
// through the SSH server.
li_ListenPort = 1080
li_Success = loo_Tunnel.BeginAccepting(li_ListenPort)
if li_Success = 0 then
Write-Debug loo_Tunnel.LastErrorText
destroy loo_Tunnel
return
end if
// IsAccepting reports whether the listener thread is running.
if loo_Tunnel.IsAccepting then
Write-Debug "The tunnel is accepting connections on port " + string(li_ListenPort)
end if
// ... the tunnel forwards traffic on a background thread while your application runs ...
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