Sample code for 30+ languages & platforms
DataFlex

Connect an SSH Tunnel to the SSH Server

See more SSH Tunnel Examples

Demonstrates the Chilkat SshTunnel.Connect method, which establishes the SSH connection that carries the tunnel's traffic. The first argument is the SSH server hostname and the second is the port. It performs the connection, SSH handshake, and host-key negotiation, but does not authenticate the SSH user.

Background: Connecting establishes only the SSH transport — the encrypted channel and host-key exchange. It is the point at which the server's identity becomes known, so it is where a security-conscious client checks HostKeyFingerprint against a trusted value before handing over credentials. Authentication and BeginAccepting follow; nothing is forwarded until they do.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

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

    Move False To iSuccess

    //  Demonstrates the SshTunnel.Connect method, which establishes the SSH connection that carries
    //  tunneled traffic.  The 1st argument is the SSH server hostname and the 2nd is the port.  This
    //  performs the connection, SSH handshake, and host-key negotiation, but does not authenticate.

    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

    //  Optionally verify the server's host key before authenticating.
    Get ComHostKeyFingerprint Of hoTunnel To sTemp1
    Showln "Host key fingerprint: " sTemp1

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

    //  After authenticating, call BeginAccepting to start listening for local connections to forward.

    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