SQL Server
SQL Server
Authenticate an SSH Tunnel with a Private Key
See more Socket/SSL/TLS Examples
Demonstrates Socket.SshAuthenticatePk, which authenticates an SSH tunnel session using a private key loaded into an SshKey object. Call SshOpenTunnel first.
The file path is relative to the application's current working directory. An absolute path may also be used. Supply the path appropriate to your own environment.
Background. The SSH server must have the corresponding public key authorized for the account. If the private key is encrypted, set the SshKey.Password property before loading it.
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
DECLARE @socket int
EXEC @hr = sp_OACreate 'Chilkat.Socket', @socket OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
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
-- Load the SSH private key. The file path is relative to the application's current working
-- directory. If the key is encrypted, set the SshKey.Password property before calling
-- FromOpenSshPrivateKey.
DECLARE @sshKey int
EXEC @hr = sp_OACreate 'Chilkat.SshKey', @sshKey OUT
DECLARE @keyText nvarchar(4000)
EXEC sp_OAMethod @sshKey, 'LoadText', @keyText OUT, 'qa_data/id_ed25519'
EXEC sp_OAGetProperty @sshKey, 'LastMethodSuccess', @iTmp0 OUT
IF @iTmp0 = 0
BEGIN
EXEC sp_OAGetProperty @sshKey, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @socket
EXEC @hr = sp_OADestroy @sshKey
RETURN
END
EXEC sp_OAMethod @sshKey, 'FromOpenSshPrivateKey', @success OUT, @keyText
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @sshKey, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @socket
EXEC @hr = sp_OADestroy @sshKey
RETURN
END
-- Authenticate the SSH tunnel session using public-key authentication. The SSH server must have the
-- corresponding public key authorized for the account.
EXEC sp_OAMethod @socket, 'SshAuthenticatePk', @success OUT, 'sshUser', @sshKey
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @socket, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @socket
EXEC @hr = sp_OADestroy @sshKey
RETURN
END
PRINT 'SSH public-key authentication succeeded.'
EXEC @hr = sp_OADestroy @socket
EXEC @hr = sp_OADestroy @sshKey
END
GO