SQL Server
SQL Server
Open an SSH Tunnel
See more Socket/SSL/TLS Examples
Demonstrates Socket.SshOpenTunnel, which connects to an SSH server and initializes an SSH tunneling session, then authenticates it.
Background. An SSH tunnel lets a socket reach a destination host through an SSH server using port forwarding. The sequence is: open the tunnel, authenticate, then open forwarded channels to destinations.
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 @socket int
EXEC @hr = sp_OACreate 'Chilkat.Socket', @socket OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
-- Connect to the SSH server and initialize an SSH tunneling session. Port 22 is conventional but
-- not required.
EXEC sp_OAMethod @socket, 'SshOpenTunnel', @success OUT, 'ssh.example.com', 22
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @socket, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @socket
RETURN
END
-- After the tunnel is open, authenticate before opening forwarded channels.
-- The SSH password should come from a secure source rather than being hard-coded.
DECLARE @sshPassword nvarchar(4000)
SELECT @sshPassword = 'mySshPassword'
EXEC sp_OAMethod @socket, 'SshAuthenticatePw', @success OUT, 'sshUser', @sshPassword
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @socket, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @socket
RETURN
END
PRINT 'SSH tunnel established and authenticated.'
EXEC @hr = sp_OADestroy @socket
END
GO