Sample code for 30+ languages & platforms
SQL Server

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 SQL Server Downloads

SQL Server
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

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

    DECLARE @ssh int
    EXEC @hr = sp_OACreate 'Chilkat.Ssh', @ssh OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    --  Set the SOCKS proxy properties before connecting.  The hostname may be a domain name or an
    --  IP address.
    EXEC sp_OASetProperty @ssh, 'SocksHostname', 'socks.example.com'
    EXEC sp_OASetProperty @ssh, 'SocksPort', 1080
    EXEC sp_OASetProperty @ssh, 'SocksUsername', 'mySocksLogin'
    EXEC sp_OASetProperty @ssh, 'SocksPassword', '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.
    EXEC sp_OASetProperty @ssh, 'SocksVersion', 5

    DECLARE @hostname nvarchar(4000)
    SELECT @hostname = 'ssh.example.com'
    DECLARE @port int
    SELECT @port = 22
    EXEC sp_OAMethod @ssh, 'Connect', @success OUT, @hostname, @port
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @ssh, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @ssh
        RETURN
      END


    PRINT '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.
    DECLARE @password nvarchar(4000)
    SELECT @password = 'mySshPassword'

    EXEC sp_OAMethod @ssh, 'AuthenticatePw', @success OUT, 'mySshLogin', @password
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @ssh, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @ssh
        RETURN
      END

    --  ... use the SSH connection normally ...

    EXEC sp_OAMethod @ssh, 'Disconnect', NULL

    EXEC @hr = sp_OADestroy @ssh


END
GO