Sample code for 30+ languages & platforms
DataFlex

SSH Tunnel for Database Connection (such as ADO, ODBC, JDBC, etc.)

See more SSH Tunnel Examples

Demonstrates how to create an SSH tunneling client in a background thread of your application. This makes it possible to SSH tunnel database connections without the need for separate software (such as PuTTY) to be running.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoTunnel
    String sSshHostname
    Integer iSshPort
    Integer iListenPort
    Boolean iWaitForThreadExit
    String sTemp1

    Move False To iSuccess

    // This example assumes the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    Move False To iSuccess

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

    Move "sftp.example.com" To sSshHostname
    Move 22 To iSshPort

    // Connect to an SSH server and establish the SSH tunnel:
    Get ComConnect Of hoTunnel sSshHostname iSshPort To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoTunnel To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // Authenticate with the SSH server via a login/password
    // or with a public key.  
    // This example demonstrates SSH password authentication.
    Get ComAuthenticatePw Of hoTunnel "mySshLogin" "mySshPassword" To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoTunnel To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // The destination host/port is the database server.
    // The DestHostname may be the domain name or 
    // IP address (in dotted decimal notation) of the database
    // server.
    Set ComDestPort Of hoTunnel To 1433
    Set ComDestHostname Of hoTunnel To "myDbServer.com"

    // Start accepting connections in a background thread.
    // The SSH tunnels are autonomously run in a background
    // thread.  There is one background thread for accepting
    // connections, and another for managing the tunnel pool.
    Move 3316 To iListenPort
    Get ComBeginAccepting Of hoTunnel iListenPort To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoTunnel To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // At this point the app may connect to the database server through
    // the SSH tunnel.  The database connection string would
    // use "localhost" for the hostname and 3316 for the port.
    // We're not going to show the database coding here,
    // because it can vary depending on the API you're using
    // (ADO, ODBC, OLE DB, etc. )

    // This is where the application's database code would go...

    // Stop the background listen/accept thread:
    Move True To iWaitForThreadExit
    Get ComStopAccepting Of hoTunnel iWaitForThreadExit To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoTunnel To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // Close the SSH tunnel (would also kick any remaining connected clients).
    Get ComCloseTunnel Of hoTunnel iWaitForThreadExit To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoTunnel To sTemp1
        Showln sTemp1
        Procedure_Return
    End



End_Procedure