SQL Server
SQL Server
SSH Public Key Authentication
See more SSH Examples
Demonstrates authenticating with an SSH server using public-key authentication. A private key is loaded into an SshKey object and passed to AuthenticatePk. The matching public key must already be installed on the server for the account.
Note: The private-key path is relative to the application's current working directory. Supply the path to your own key file.
Background: Public-key authentication is the standard for unattended and automated SSH: the private key never leaves the client, the server merely trusts the corresponding public key, and no reusable password crosses the wire.
SshKey imports several formats — OpenSSH PEM keys via FromOpenSshPrivateKey and PuTTY .ppk files via FromPuttyPrivateKey — encrypted or not. For an encrypted key, set the Password property before importing. Note that LoadText is only a convenience for reading a text file into a string; it does not itself load the key.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 public-key authentication. The matching
-- public key must already be installed on the SSH server for the account.
DECLARE @ssh int
EXEC @hr = sp_OACreate 'Chilkat.Ssh', @ssh OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
-- Timeouts, in milliseconds.
EXEC sp_OASetProperty @ssh, 'ConnectTimeoutMs', 5000
EXEC sp_OASetProperty @ssh, 'ReadTimeoutMs', 15000
DECLARE @hostname nvarchar(4000)
SELECT @hostname = 'ssh.example.com'
DECLARE @port int
SELECT @port = 22
EXEC sp_OAMethod @ssh, 'Connect', @success OUT, @hostname, @port
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @ssh, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @ssh
RETURN
END
-- LoadText is a convenience method that reads any text file into a string. It does not itself
-- load the key.
DECLARE @key int
EXEC @hr = sp_OACreate 'Chilkat.SshKey', @key OUT
DECLARE @privKeyText nvarchar(4000)
EXEC sp_OAMethod @key, 'LoadText', @privKeyText OUT, 'qa_data/my_private_key.pem'
EXEC sp_OAGetProperty @key, 'LastMethodSuccess', @iTmp0 OUT
IF @iTmp0 = 0
BEGIN
EXEC sp_OAGetProperty @key, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @ssh
EXEC @hr = sp_OADestroy @key
RETURN
END
-- Import the private key. Both OpenSSH and PuTTY formats are supported, encrypted or not.
-- This example loads an unencrypted OpenSSH-format key; for a PuTTY .ppk file, call
-- FromPuttyPrivateKey instead. For an encrypted key, set key.Password before importing.
EXEC sp_OAMethod @key, 'FromOpenSshPrivateKey', @success OUT, @privKeyText
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @key, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @ssh
EXEC @hr = sp_OADestroy @key
RETURN
END
EXEC sp_OAMethod @ssh, 'AuthenticatePk', @success OUT, 'mySshLogin', @key
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @ssh, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @ssh
EXEC @hr = sp_OADestroy @key
RETURN
END
PRINT 'Public-key authentication successful.'
EXEC sp_OAMethod @ssh, 'Disconnect', NULL
EXEC @hr = sp_OADestroy @ssh
EXEC @hr = sp_OADestroy @key
END
GO