Sample code for 30+ languages & platforms
DataFlex

Route Socket Connections through an SSH Object

See more Socket/SSL/TLS Examples

Demonstrates Socket.UseSsh, which configures a socket to route subsequent Connect calls through an already connected and authenticated Ssh object using SSH port forwarding.

Background. After UseSsh, the destination host and port passed to Connect are reached through the SSH tunnel rather than by a direct TCP connection. This is one of several SSH-tunneling approaches the Socket class supports.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Variant vSsh
    Handle hoSsh
    String sSshPassword
    Handle hoSocket
    Boolean iBTls
    Integer iMaxWaitMs
    String sTemp1

    Move False To iSuccess

    Get Create (RefClass(cComChilkatSsh)) To hoSsh
    If (Not(IsComObjectCreated(hoSsh))) Begin
        Send CreateComObject of hoSsh
    End

    //  Connect and authenticate a standalone SSH object.
    Get ComConnect Of hoSsh "ssh.example.com" 22 To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoSsh To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  The SSH password should come from a secure source rather than being hard-coded.
    Move "mySshPassword" To sSshPassword
    Get ComAuthenticatePw Of hoSsh "sshUser" sSshPassword To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoSsh To sTemp1
        Showln sTemp1
        Procedure_Return
    End

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

    //  Route subsequent Connect calls through the already connected and authenticated SSH object.  The
    //  destination is reached by SSH port forwarding rather than by a direct TCP connection.
    Get pvComObject of hoSsh to vSsh
    Get ComUseSsh Of hoSocket vSsh To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoSocket To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  Connect to the destination through the SSH tunnel.
    Move False To iBTls
    Move 5000 To iMaxWaitMs
    Get ComConnect Of hoSocket "db.internal" 5432 iBTls iMaxWaitMs To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoSocket To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Showln "Connected to the destination through the SSH tunnel."


End_Procedure