Sample code for 30+ languages & platforms
SQL Server

Connect an SSH Tunnel Through a Jump Host

See more SSH Tunnel Examples

Demonstrates the Chilkat SshTunnel.ConnectThroughSsh method, which connects to an SSH server through an already connected and authenticated Ssh object. The first argument is the jump-host Ssh object, and the second and third are the destination SSH hostname and port.

Background: This chains the tunnel through a "jump host" (bastion): the application reaches a gateway server, then tunnels onward to an SSH server that is only accessible from inside the network. Each hop authenticates separately with its own credentials. The result is a normal SshTunnel — still requiring its own authentication and BeginAccepting — whose transport is routed through the first connection.

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

    --  Demonstrates the SshTunnel.ConnectThroughSsh method, which connects to an SSH server through
    --  an already connected and authenticated Ssh object (a jump host).  The 1st argument is the Ssh
    --  object, and the 2nd and 3rd are the destination SSH hostname and port.

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

    --  Connect and authenticate to the jump host first.
    DECLARE @sshPort int
    SELECT @sshPort = 22
    EXEC sp_OAMethod @sshJump, 'Connect', @success OUT, 'jump.example.com', @sshPort
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @sshJump, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @sshJump
        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 @sshJump, 'AuthenticatePw', @success OUT, 'myJumpLogin', @password
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @sshJump, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @sshJump
        RETURN
      END

    --  Set up the tunnel and connect to the target SSH server through the jump host.
    DECLARE @tunnel int
    EXEC @hr = sp_OACreate 'Chilkat.SshTunnel', @tunnel OUT

    EXEC sp_OASetProperty @tunnel, 'DestHostname', 'db.internal.example.com'
    EXEC sp_OASetProperty @tunnel, 'DestPort', 5432

    EXEC sp_OAMethod @tunnel, 'ConnectThroughSsh', @success OUT, @sshJump, 'ssh.internal.example.com', @sshPort
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @tunnel, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @sshJump
        EXEC @hr = sp_OADestroy @tunnel
        RETURN
      END

    --  Authenticate to the target SSH server (its own credentials).
    EXEC sp_OAMethod @tunnel, 'AuthenticatePw', @success OUT, 'mySshLogin', @password
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @tunnel, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @sshJump
        EXEC @hr = sp_OADestroy @tunnel
        RETURN
      END

    --  After authenticating, call BeginAccepting to start listening for local connections to forward.

    DECLARE @waitForThreadExit int
    SELECT @waitForThreadExit = 1
    EXEC sp_OAMethod @tunnel, 'CloseTunnel', @success OUT, @waitForThreadExit
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @tunnel, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @sshJump
        EXEC @hr = sp_OADestroy @tunnel
        RETURN
      END

    EXEC sp_OAMethod @sshJump, 'Disconnect', NULL

    EXEC @hr = sp_OADestroy @sshJump
    EXEC @hr = sp_OADestroy @tunnel


END
GO