Sample code for 30+ languages & platforms
SQL Server

SSH Through an HTTP Proxy

See more SSH Examples

Demonstrates connecting to an SSH server through an HTTP proxy, using the HTTP/1.1 CONNECT method. Set HttpProxyHostname and HttpProxyPort before calling Connect.

Background: CONNECT asks the proxy to open a raw TCP tunnel rather than to fetch a page, which is how non-HTTP protocols traverse an HTTP proxy. The important caveat is that the proxy must be configured to permit it: many proxies allow CONNECT only to port 443, in which case an SSH connection on port 22 is refused no matter how the client is configured. Typical proxy ports are 3128 and 8080.

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 an HTTP proxy, using the HTTP/1.1 CONNECT
    --  method.

    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 proxy hostname (or IP address) and port before connecting.  Typical HTTP proxy ports
    --  are 3128 and 8080.
    EXEC sp_OASetProperty @ssh, 'HttpProxyHostname', 'proxy.example.com'
    EXEC sp_OASetProperty @ssh, 'HttpProxyPort', 3128

    --  Important: the HTTP proxy must allow non-HTTP traffic to pass through the CONNECT tunnel,
    --  otherwise this cannot work.

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