Sample code for 30+ languages & platforms
DataFlex

SSH Through a SOCKS Proxy

See more SSH Examples

Demonstrates connecting to an SSH server through a SOCKS4 or SOCKS5 proxy. Set the SOCKS properties before calling Connect, and set SocksVersion to match the proxy server.

Background: SOCKS is a general-purpose TCP relay, so unlike an HTTP proxy it is designed from the start to carry arbitrary protocols — usually the smoother option for SSH. The version matters: SOCKS4 supports only a username with no password, while SOCKS5 supports full login/password authentication as well as IPv6 and remote DNS resolution. Note this is the inverse of dynamic port forwarding: here a SOCKS proxy is used to reach the SSH server, rather than SSH providing a SOCKS proxy.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoSsh
    String sHostname
    Integer iPort
    String sPassword
    String sTemp1

    Move False To iSuccess

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

    //  Demonstrates connecting to an SSH server through a SOCKS4 or SOCKS5 proxy.

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

    //  Set the SOCKS proxy properties before connecting.  The hostname may be a domain name or an
    //  IP address.
    Set ComSocksHostname Of hoSsh To "socks.example.com"
    Set ComSocksPort Of hoSsh To 1080
    Set ComSocksUsername Of hoSsh To "mySocksLogin"
    Set ComSocksPassword Of hoSsh To "mySocksPassword"

    //  Set the version to match the proxy server: 4 or 5.
    //  Note: SOCKS4 supports only a username, with no password.  SOCKS5 supports full
    //  login/password authentication.
    Set ComSocksVersion Of hoSsh To 5

    Move "ssh.example.com" To sHostname
    Move 22 To iPort
    Get ComConnect Of hoSsh sHostname iPort To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoSsh To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Showln "Connected to the SSH server through the SOCKS proxy."

    //  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 hoSsh "mySshLogin" sPassword To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoSsh To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  ... use the SSH connection normally ...

    Send ComDisconnect To hoSsh


End_Procedure