Sample code for 30+ languages & platforms
DataFlex

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

DataFlex
Use ChilkatAx-win32.pkg

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

    Move False To iSuccess

    //  Demonstrates the SshTunnel.IsSshConnected method, which returns whether the SSH transport used
    //  by the tunnel is connected.  It takes no arguments.

    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

    //  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 False,
    //  though those connections would fail to forward.
    Get ComIsSshConnected Of hoTunnel To bTemp1
    If (bTemp1) Begin
        Showln "The SSH transport is connected."
    End
    Else Begin
        Showln "The SSH transport is not connected."
    End

    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