SQL Server
SQL Server
Close an SSH Tunnel
See more Socket/SSL/TLS Examples
Demonstrates Socket.SshCloseTunnel, which closes the SSH tunnel opened by SshOpenTunnel and terminates its forwarded channels.
Background. Closing the tunnel does not affect unrelated SSH objects or connections.
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
-- Authenticate the SSH session 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
-- Close the SSH tunnel and terminate its forwarded channels. This does not affect unrelated SSH
-- objects or connections.
EXEC sp_OAMethod @socket, 'SshCloseTunnel', @success OUT
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @socket, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @socket
RETURN
END
PRINT 'SSH tunnel closed.'
EXEC @hr = sp_OADestroy @socket
END
GO