PowerBuilder
PowerBuilder
Check if the SSH Tunnel's Transport is Connected
See more SSH Tunnel Examples
Demonstrates the Chilkat SshTunnel.IsSshConnected method, which returns whether the SSH transport used by the tunnel is connected. It takes no arguments.
Background: A tunnel has two independent parts: the SSH transport to the server and the local listener that accepts connections to forward. This method reports only the first. The distinction matters because the listener can keep accepting local connections even after the SSH transport has dropped — those connections would then fail to forward — so a health check on a long-running tunnel should verify the SSH side explicitly.
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 the SshTunnel.IsSshConnected method, which returns whether the SSH transport used
// by the tunnel is connected. It takes no arguments.
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 tunnel forwards accepted local connections to this destination through the SSH server.
loo_Tunnel.DestHostname = "db.internal.example.com"
loo_Tunnel.DestPort = 5432
// Connect to the SSH server. This performs the TCP/proxy connection and SSH handshake, but
// does not authenticate yet.
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
// Check whether the SSH transport is connected. This is independent of the local listener
// state -- the listener can still be accepting local connections even when this is 0,
// though those connections would fail to forward.
if loo_Tunnel.IsSshConnected() then
Write-Debug "The SSH transport is connected."
else
Write-Debug "The SSH transport is not connected."
end if
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