SQL Server
SQL Server
SSH Tunnel Static (Fixed-Destination) Port Forwarding
See more SSH Tunnel Examples
Demonstrates fixed-destination port forwarding with the DestHostname, DestPort, ListenBindIpAddress, and ListenPort properties. Every local connection accepted by the listener is forwarded to the same fixed destination through the SSH server.
Background: This is the classic
ssh -L scenario: expose a single remote service — a database, an internal web app — at a local port. Binding the listener to 127.0.0.1 keeps the tunnel private to the local machine, whereas 0.0.0.0 would let other hosts route through it. Passing 0 to BeginAccepting lets the OS pick a free port, which ListenPort then reports — handy when a fixed port might already be in use.Chilkat SQL Server Downloads
-- 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
-- Demonstrates fixed-destination (static) port forwarding with the SshTunnel properties
-- DestHostname, DestPort, ListenBindIpAddress, and ListenPort.
--
-- Every local connection accepted by the listener is forwarded to the same fixed destination
-- through the SSH server.
DECLARE @tunnel int
EXEC @hr = sp_OACreate 'Chilkat.SshTunnel', @tunnel OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
-- The fixed destination that accepted connections are forwarded to.
EXEC sp_OASetProperty @tunnel, 'DestHostname', 'db.internal.example.com'
EXEC sp_OASetProperty @tunnel, 'DestPort', 5432
-- Bind the listener to loopback so only local processes can use the tunnel. Use "0.0.0.0" to
-- accept connections from other machines (less secure).
EXEC sp_OASetProperty @tunnel, 'ListenBindIpAddress', '127.0.0.1'
DECLARE @sshPort int
SELECT @sshPort = 22
EXEC sp_OAMethod @tunnel, 'Connect', @success OUT, 'ssh.example.com', @sshPort
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @tunnel, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @tunnel
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 @tunnel, 'AuthenticatePw', @success OUT, 'mySshLogin', @password
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @tunnel, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @tunnel
RETURN
END
-- Pass 0 to let the operating system choose an available port. After BeginAccepting succeeds,
-- ListenPort contains the actual port that was bound.
EXEC sp_OAMethod @tunnel, 'BeginAccepting', @success OUT, 0
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @tunnel, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @tunnel
RETURN
END
EXEC sp_OAGetProperty @tunnel, 'ListenPort', @iTmp0 OUT
PRINT 'Listening on 127.0.0.1 port ' + @iTmp0
-- Applications now connect to 127.0.0.1:ListenPort as if it were the database server.
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 @tunnel
RETURN
END
EXEC @hr = sp_OADestroy @tunnel
END
GO