Sample code for 30+ languages & platforms
DataFlex

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

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoTunnel
    Integer iSshPort
    String sPassword
    Integer iListenPort
    String sStateXml
    Boolean iWaitForThreadExit
    String sTemp1
    Boolean bTemp1

    Move False To iSuccess

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

    Get Create (RefClass(cComChilkatSshTunnel)) To hoTunnel
    If (Not(IsComObjectCreated(hoTunnel))) Begin
        Send CreateComObject of hoTunnel
    End

    //  The tunnel forwards accepted local connections to this destination through the SSH server.
    Set ComDestHostname Of hoTunnel To "db.internal.example.com"
    Set ComDestPort Of hoTunnel To 5432

    //  Connect to the SSH server.  This performs the TCP/proxy connection and SSH handshake, but
    //  does not authenticate yet.
    Move 22 To iSshPort
    Get ComConnect Of hoTunnel "ssh.example.com" iSshPort To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoTunnel To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  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.
    Move "mySshPassword" To sPassword

    Get ComAuthenticatePw Of hoTunnel "mySshLogin" sPassword To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoTunnel To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  Begin accepting so there is live tunnel state to inspect.
    Move 1080 To iListenPort
    Get ComBeginAccepting Of hoTunnel iListenPort To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoTunnel To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  Get a diagnostic snapshot of the tunnel's current state as XML.
    Get ComGetCurrentState Of hoTunnel To sStateXml
    Get ComLastMethodSuccess Of hoTunnel To bTemp1
    If (bTemp1 = False) Begin
        Get ComLastErrorText Of hoTunnel To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Showln sStateXml

    Move True To iWaitForThreadExit
    Get ComCloseTunnel Of hoTunnel iWaitForThreadExit To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoTunnel To sTemp1
        Showln sTemp1
        Procedure_Return
    End



End_Procedure