Sample code for 30+ languages & platforms
DataFlex

Open a Forwarded Channel through an SSH Tunnel

See more Socket/SSL/TLS Examples

Demonstrates Socket.SshNewChannel, which opens a port-forwarded channel through an authenticated SSH tunnel to a destination host and port, populating a Socket with the connected channel.

Background. Set the ssl argument to true to establish TLS to the destination inside the SSH tunnel. The returned channel socket is then used to send and receive with the destination.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoSocket
    String sSshPassword
    Boolean iBTls
    Integer iMaxWaitMs
    Variant vChannel
    Handle hoChannel
    String sTemp1

    Move False To iSuccess

    Get Create (RefClass(cComChilkatSocket)) To hoSocket
    If (Not(IsComObjectCreated(hoSocket))) Begin
        Send CreateComObject of hoSocket
    End

    //  Connect to the SSH server and initialize an SSH tunneling session.  Port 22 is conventional but
    //  not required.
    Get ComSshOpenTunnel Of hoSocket "ssh.example.com" 22 To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoSocket To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  Authenticate the SSH session before opening forwarded channels.
    //  The SSH password should come from a secure source rather than being hard-coded.
    Move "mySshPassword" To sSshPassword
    Get ComSshAuthenticatePw Of hoSocket "sshUser" sSshPassword To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoSocket To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  Open a port-forwarded channel through the authenticated SSH tunnel to the destination host and
    //  port, populating a Socket with the connected channel.  Set the 3rd argument to True to establish
    //  TLS to the destination inside the tunnel.
    Move False To iBTls
    Move 20000 To iMaxWaitMs
    Get Create (RefClass(cComChilkatSocket)) To hoChannel
    If (Not(IsComObjectCreated(hoChannel))) Begin
        Send CreateComObject of hoChannel
    End
    Get pvComObject of hoChannel to vChannel
    Get ComSshNewChannel Of hoSocket "db.internal" 5432 iBTls iMaxWaitMs vChannel To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoSocket To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  Use the channel socket to send and receive with the destination through the tunnel.
    Showln "Forwarded channel opened to the destination."


End_Procedure