SQL Server
SQL Server
SSH Host Key Fingerprint
See more SSH Examples
Demonstrates getting the SSH server's host key fingerprint after connecting. The HostKeyFingerprint property returns the classic MD5 form, while GetHostKeyFP returns a fingerprint using a chosen hash algorithm, with optional inclusion of the key type and hash name.
Background: The host key fingerprint identifies the server, and comparing it against a known-good value is how a client implements host-key pinning — detecting a man-in-the-middle or a server whose key has changed. This is the same fingerprint an SSH client shows on first connection when it asks whether to trust the host. Prefer
SHA256, which is the modern standard; MD5 fingerprints still appear in older tooling but are cryptographically weak.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 getting the SSH server's host key fingerprint after connecting.
DECLARE @ssh int
EXEC @hr = sp_OACreate 'Chilkat.Ssh', @ssh OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
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
-- The classic MD5 fingerprint is available as a property.
DECLARE @md5_fingerprint nvarchar(4000)
EXEC sp_OAGetProperty @ssh, 'HostKeyFingerprint', @md5_fingerprint OUT
PRINT @md5_fingerprint
-- For example: ssh-rsa 3072 21:b0:d8:41:4e:ef:78:10:20:af:01:b7:71:5d:eb:94
-- GetHostKeyFP returns a fingerprint using the hash algorithm of your choice, such as SHA256,
-- SHA384, or SHA512. The 2nd and 3rd arguments control whether the key type and the hash name
-- are included in the returned string.
DECLARE @includeKeyType int
SELECT @includeKeyType = 1
DECLARE @includeHashName int
SELECT @includeHashName = 1
DECLARE @sha256_fingerprint nvarchar(4000)
EXEC sp_OAMethod @ssh, 'GetHostKeyFP', @sha256_fingerprint OUT, 'SHA256', @includeKeyType, @includeHashName
EXEC sp_OAGetProperty @ssh, 'LastMethodSuccess', @iTmp0 OUT
IF @iTmp0 = 0
BEGIN
EXEC sp_OAGetProperty @ssh, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @ssh
RETURN
END
PRINT @sha256_fingerprint
-- ssh-rsa SHA256:Ufgj480OsdsCZRjj9sSNM6fpgIcSJ61RsIG8usndUIY=
SELECT @includeKeyType = 0
SELECT @includeHashName = 1
EXEC sp_OAMethod @ssh, 'GetHostKeyFP', @sha256_fingerprint OUT, 'SHA256', @includeKeyType, @includeHashName
EXEC sp_OAGetProperty @ssh, 'LastMethodSuccess', @iTmp0 OUT
IF @iTmp0 = 0
BEGIN
EXEC sp_OAGetProperty @ssh, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @ssh
RETURN
END
PRINT @sha256_fingerprint
-- SHA256:Ufgj480OsdsCZRjj9sSNM6fpgIcSJ61RsIG8usndUIY=
SELECT @includeKeyType = 1
SELECT @includeHashName = 0
EXEC sp_OAMethod @ssh, 'GetHostKeyFP', @sha256_fingerprint OUT, 'SHA256', @includeKeyType, @includeHashName
EXEC sp_OAGetProperty @ssh, 'LastMethodSuccess', @iTmp0 OUT
IF @iTmp0 = 0
BEGIN
EXEC sp_OAGetProperty @ssh, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @ssh
RETURN
END
PRINT @sha256_fingerprint
-- ssh-rsa Ufgj480OsdsCZRjj9sSNM6fpgIcSJ61RsIG8usndUIY=
SELECT @includeKeyType = 0
SELECT @includeHashName = 0
EXEC sp_OAMethod @ssh, 'GetHostKeyFP', @sha256_fingerprint OUT, 'SHA256', @includeKeyType, @includeHashName
EXEC sp_OAGetProperty @ssh, 'LastMethodSuccess', @iTmp0 OUT
IF @iTmp0 = 0
BEGIN
EXEC sp_OAGetProperty @ssh, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @ssh
RETURN
END
PRINT @sha256_fingerprint
-- Ufgj480OsdsCZRjj9sSNM6fpgIcSJ61RsIG8usndUIY=
EXEC sp_OAMethod @ssh, 'Disconnect', NULL
EXEC @hr = sp_OADestroy @ssh
END
GO