SQL Server
SQL Server
Route Socket Connections through an SSH Object
See more Socket/SSL/TLS Examples
Demonstrates Socket.UseSsh, which configures a socket to route subsequent Connect calls through an already connected and authenticated Ssh object using SSH port forwarding.
Background. After UseSsh, the destination host and port passed to Connect are reached through the SSH tunnel rather than by a direct TCP connection. This is one of several SSH-tunneling approaches the Socket class supports.
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 @sTmp0 nvarchar(4000)
DECLARE @success int
SELECT @success = 0
DECLARE @ssh int
EXEC @hr = sp_OACreate 'Chilkat.Ssh', @ssh OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
-- Connect and authenticate a standalone SSH object.
EXEC sp_OAMethod @ssh, 'Connect', @success OUT, 'ssh.example.com', 22
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @ssh, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @ssh
RETURN
END
-- The SSH password should come from a secure source rather than being hard-coded.
DECLARE @sshPassword nvarchar(4000)
SELECT @sshPassword = 'mySshPassword'
EXEC sp_OAMethod @ssh, 'AuthenticatePw', @success OUT, 'sshUser', @sshPassword
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @ssh, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @ssh
RETURN
END
DECLARE @socket int
EXEC @hr = sp_OACreate 'Chilkat.Socket', @socket OUT
-- Route subsequent Connect calls through the already connected and authenticated SSH object. The
-- destination is reached by SSH port forwarding rather than by a direct TCP connection.
EXEC sp_OAMethod @socket, 'UseSsh', @success OUT, @ssh
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @socket, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @ssh
EXEC @hr = sp_OADestroy @socket
RETURN
END
-- Connect to the destination through the SSH tunnel.
DECLARE @bTls int
SELECT @bTls = 0
DECLARE @maxWaitMs int
SELECT @maxWaitMs = 5000
EXEC sp_OAMethod @socket, 'Connect', @success OUT, 'db.internal', 5432, @bTls, @maxWaitMs
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @socket, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @ssh
EXEC @hr = sp_OADestroy @socket
RETURN
END
PRINT 'Connected to the destination through the SSH tunnel.'
EXEC @hr = sp_OADestroy @ssh
EXEC @hr = sp_OADestroy @socket
END
GO