Sample code for 30+ languages & platforms
DataFlex

SSH Tunnel Through an Outbound SOCKS Proxy

See more SSH Tunnel Examples

Demonstrates reaching the SSH server through an outbound SOCKS proxy using the SocksVersion, SocksHostname, SocksPort, SocksUsername, and SocksPassword properties.

Background: This is the reverse direction from dynamic forwarding: here a SOCKS proxy is used to reach the SSH server, for networks where outbound connections must pass through one. SocksVersion is the switch — 0 means no SOCKS, while 4 or 5 selects the protocol. SOCKS5 supports username/password authentication and remote DNS; SOCKS4 supports only a username.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

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

    Move False To iSuccess

    //  Demonstrates reaching the SSH server through an outbound SOCKS proxy using the SshTunnel
    //  properties SocksVersion, SocksHostname, SocksPort, SocksUsername, and SocksPassword.

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

    //  Set SocksVersion to 4 or 5 to route the outbound connection to the SSH server through a SOCKS
    //  proxy.  A value of 0 (the default) means no SOCKS proxy.
    Set ComSocksVersion Of hoTunnel To 5
    Set ComSocksHostname Of hoTunnel To "socks.example.com"
    Set ComSocksPort Of hoTunnel To 1080

    //  SOCKS4 supports only a username; SOCKS5 supports username and password.
    Set ComSocksUsername Of hoTunnel To "myProxyLogin"
    Set ComSocksPassword Of hoTunnel To "myProxyPassword"

    Set ComDestHostname Of hoTunnel To "db.internal.example.com"
    Set ComDestPort Of hoTunnel To 5432

    //  Connect to the SSH server through the SOCKS proxy configured above.
    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

    Move 1080 To iListenPort
    Get ComBeginAccepting Of hoTunnel iListenPort To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoTunnel To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Showln "Tunnel established through the SOCKS proxy."

    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