Sample code for 30+ languages & platforms
AutoIt

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 AutoIt Downloads

AutoIt
Local $bSuccess = False

;  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.

$oTunnel = ObjCreate("Chilkat.SshTunnel")

;  The tunnel forwards accepted local connections to this destination through the SSH server.
$oTunnel.DestHostname = "db.internal.example.com"
$oTunnel.DestPort = 5432

;  Connect to the SSH server.  This performs the TCP/proxy connection and SSH handshake, but
;  does not authenticate yet.
Local $iSshPort = 22
$bSuccess = $oTunnel.Connect("ssh.example.com",$iSshPort)
If ($bSuccess = False) Then
    ConsoleWrite($oTunnel.LastErrorText & @CRLF)
    Exit
EndIf

;  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.
Local $sPassword = "mySshPassword"

$bSuccess = $oTunnel.AuthenticatePw("mySshLogin",$sPassword)
If ($bSuccess = False) Then
    ConsoleWrite($oTunnel.LastErrorText & @CRLF)
    Exit
EndIf

;  Begin accepting so there is live tunnel state to inspect.
Local $iListenPort = 1080
$bSuccess = $oTunnel.BeginAccepting($iListenPort)
If ($bSuccess = False) Then
    ConsoleWrite($oTunnel.LastErrorText & @CRLF)
    Exit
EndIf

;  Get a diagnostic snapshot of the tunnel's current state as XML.
Local $stateXml = $oTunnel.GetCurrentState()
If ($oTunnel.LastMethodSuccess = False) Then
    ConsoleWrite($oTunnel.LastErrorText & @CRLF)
    Exit
EndIf

ConsoleWrite($stateXml & @CRLF)

Local $bWaitForThreadExit = True
$bSuccess = $oTunnel.CloseTunnel($bWaitForThreadExit)
If ($bSuccess = False) Then
    ConsoleWrite($oTunnel.LastErrorText & @CRLF)
    Exit
EndIf