Sample code for 30+ languages & platforms
SQL Server

SSH Tunnel (Port Forwarding via direct-tcpip Channel)

See more SSH Examples

Demonstrates creating an SSH tunnel to a remote host:port using a direct-tcpip channel. Data written to the channel is forwarded by the SSH server to the target, and data read back is whatever the target sent. This example tunnels a simple HTTP GET request to a web server.

Background: This is SSH local port forwarding at the API level, and the target need not be a web server — it can be a database, a message broker, or any TCP service. The key detail is that the SSH server resolves and connects to the target, not your machine, so the destination only has to be reachable from the server's network. That is what makes tunneling useful for reaching services behind a firewall. Note the receive pattern here: read until the double CRLF that ends an HTTP header, extract exactly that with GetReceivedTextS, then poll briefly for the body, which may already have arrived.

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 @iTmp0 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 creating an SSH tunnel to a remote host:port using a direct-tcpip channel.
    --  Data written to the channel is forwarded by the SSH server to the target host:port, and data
    --  read from the channel is whatever the target sent back.

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

    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

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

    --  Ask the SSH server to connect onward to this host:port.  The target is resolved by the SSH
    --  server, not by this computer, so it must be reachable from the server's network.  The target
    --  need not be a web server -- it can be any TCP service.
    DECLARE @targetHost nvarchar(4000)
    SELECT @targetHost = 'www.chilkatsoft.com'
    DECLARE @targetPort int
    SELECT @targetPort = 80
    DECLARE @channelNum int
    EXEC sp_OAMethod @ssh, 'OpenDirectTcpIpChannel', @channelNum OUT, @targetHost, @targetPort
    IF @channelNum < 0
      BEGIN
        EXEC sp_OAGetProperty @ssh, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @ssh
        RETURN
      END

    --  Send a simple HTTP GET request through the tunnel.
    DECLARE @httpReq nvarchar(4000)
    SELECT @httpReq = 'GET /xyz123.html HTTP/1.1' + CHAR(13) + CHAR(10) + 'Host: www.chilkatsoft.com' + CHAR(13) + CHAR(10) + CHAR(13) + CHAR(10)
    EXEC sp_OAMethod @ssh, 'ChannelSendString', @success OUT, @channelNum, @httpReq, 'utf-8'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @ssh, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @ssh
        RETURN
      END

    --  IMPORTANT: Set a read timeout before receiving until a match.  ReadTimeoutMs defaults to 0,
    --  which means no limit -- without it, this call waits forever if the received data never
    --  contains a match.
    EXEC sp_OASetProperty @ssh, 'ReadTimeoutMs', 15000

    --  An HTTP response header ends with a blank line, so receive until a double CRLF is seen.
    --  The method may read beyond the match, but it stops waiting as soon as the match appears.
    DECLARE @caseSensitive int
    SELECT @caseSensitive = 0
    DECLARE @matchStr nvarchar(4000)
    SELECT @matchStr = CHAR(13) + CHAR(10) + CHAR(13) + CHAR(10)
    EXEC sp_OAMethod @ssh, 'ChannelReceiveUntilMatch', @success OUT, @channelNum, @matchStr, 'utf-8', @caseSensitive
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @ssh, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @ssh
        RETURN
      END

    --  GetReceivedTextS removes everything through and including the match, which is exactly the
    --  response header.
    DECLARE @responseHeader nvarchar(4000)
    EXEC sp_OAMethod @ssh, 'GetReceivedTextS', @responseHeader OUT, @channelNum, @matchStr, 'utf-8'
    EXEC sp_OAGetProperty @ssh, 'LastMethodSuccess', @iTmp0 OUT
    IF @iTmp0 = 0
      BEGIN
        EXEC sp_OAGetProperty @ssh, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @ssh
        RETURN
      END

    PRINT '---- HTTP Response Header ----'

    PRINT @responseHeader

    --  The body may already have arrived along with the header.  Poll briefly for anything more.
    --  A -2 return simply means nothing further arrived, which is not an error here.
    DECLARE @pollTimeoutMs int
    SELECT @pollTimeoutMs = 200
    DECLARE @numBytesRead int
    EXEC sp_OAMethod @ssh, 'ChannelPoll', @numBytesRead OUT, @channelNum, @pollTimeoutMs
    IF @numBytesRead = -1
      BEGIN
        EXEC sp_OAGetProperty @ssh, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @ssh
        RETURN
      END

    DECLARE @htmlBody nvarchar(4000)
    EXEC sp_OAMethod @ssh, 'GetReceivedText', @htmlBody OUT, @channelNum, 'utf-8'
    EXEC sp_OAGetProperty @ssh, 'LastMethodSuccess', @iTmp0 OUT
    IF @iTmp0 = 0
      BEGIN
        EXEC sp_OAGetProperty @ssh, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @ssh
        RETURN
      END

    PRINT '---- HTML BODY ----'

    PRINT @htmlBody

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

    EXEC sp_OAMethod @ssh, 'Disconnect', NULL

    EXEC @hr = sp_OADestroy @ssh


END
GO