Sample code for 30+ languages & platforms
PowerBuilder

Get an SSH Tunnel's Current State (Diagnostics)

See more SSH Tunnel Examples

Demonstrates the Chilkat SshTunnel.GetCurrentState method, which returns an XML snapshot describing the tunnel manager, SSH channels, and current tunnel clients. It takes no arguments and is intended for diagnostics and monitoring.

Background: Because the tunnel forwards traffic on a background thread, there is no natural place in your code to observe what it is doing at a given moment. GetCurrentState provides that visibility — byte counts, active channels, and connected clients — as structured XML suited to logging or a monitoring dashboard. It is a read-only snapshot and does not affect the tunnel.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Tunnel
integer li_SshPort
string ls_Password
integer li_ListenPort
string ls_StateXml
integer li_WaitForThreadExit

li_Success = 0

//  Demonstrates the SshTunnel.GetCurrentState method, which returns an XML snapshot describing the
//  tunnel manager, SSH channels, and current tunnel clients.  It takes no arguments and is
//  intended for diagnostics and monitoring.

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

//  Begin accepting so there is live tunnel state to inspect.
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

//  Get a diagnostic snapshot of the tunnel's current state as XML.
ls_StateXml = loo_Tunnel.GetCurrentState()
if loo_Tunnel.LastMethodSuccess = 0 then
    Write-Debug loo_Tunnel.LastErrorText
    destroy loo_Tunnel
    return
end if

Write-Debug ls_StateXml

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