SQL Server
SQL Server
Use a PuTTY Key for SSH Authentication
See more SSH Examples
Demonstrates authenticating with an SSH server using a username and a PuTTY (.ppk) private key. The key is imported with FromPuttyPrivateKey — setting Password first if it is encrypted — and passed to AuthenticatePk.
Note: The file paths are relative to the application's current working directory. Supply the paths to your own files.
Background: A PuTTY key can be used directly for authentication with no conversion step, which is convenient when the key was generated by PuTTYgen on Windows. Conceptually the private key takes the place of a password while the username identifies the account, and the corresponding public key must already be installed on the server. Convert to OpenSSH format only if some other tool in your pipeline requires PEM.
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
-- This example requires the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.
-- Demonstrates authenticating with an SSH server using a username and a PuTTY (.ppk) private
-- key. The private key serves as the credential; the username identifies the account on the
-- server.
DECLARE @ssh int
EXEC @hr = sp_OACreate 'Chilkat.Ssh', @ssh OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
-- LoadText is a convenience method that reads any text file into a string. It does not itself
-- load the key.
DECLARE @puttyKey int
EXEC @hr = sp_OACreate 'Chilkat.SshKey', @puttyKey OUT
DECLARE @ppkText nvarchar(4000)
EXEC sp_OAMethod @puttyKey, 'LoadText', @ppkText OUT, 'qa_data/ppk/putty_private_secret.ppk'
EXEC sp_OAGetProperty @puttyKey, 'LastMethodSuccess', @iTmp0 OUT
IF @iTmp0 = 0
BEGIN
EXEC sp_OAGetProperty @puttyKey, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @ssh
EXEC @hr = sp_OADestroy @puttyKey
RETURN
END
-- Set the password before importing an encrypted .ppk. This should come from a secure source
-- rather than being hard-coded.
EXEC sp_OASetProperty @puttyKey, 'Password', 'myKeyPassword'
EXEC sp_OAMethod @puttyKey, 'FromPuttyPrivateKey', @success OUT, @ppkText
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @puttyKey, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @ssh
EXEC @hr = sp_OADestroy @puttyKey
RETURN
END
DECLARE @sshHostname nvarchar(4000)
SELECT @sshHostname = 'ssh.example.com'
DECLARE @sshPort int
SELECT @sshPort = 22
EXEC sp_OAMethod @ssh, 'Connect', @success OUT, @sshHostname, @sshPort
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @ssh, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @ssh
EXEC @hr = sp_OADestroy @puttyKey
RETURN
END
-- The corresponding public key must already be installed on the SSH server for the account.
EXEC sp_OAMethod @ssh, 'AuthenticatePk', @success OUT, 'mySshLogin', @puttyKey
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @ssh, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @ssh
EXEC @hr = sp_OADestroy @puttyKey
RETURN
END
PRINT 'Connection and authentication with the SSH server completed.'
EXEC sp_OAMethod @ssh, 'Disconnect', NULL
EXEC @hr = sp_OADestroy @ssh
EXEC @hr = sp_OADestroy @puttyKey
END
GO