Sample code for 30+ languages & platforms
SQL Server

Initiate an SSH Key Re-Exchange

See more SSH Examples

Demonstrates the Chilkat Ssh.ReKey method, which initiates an SSH key re-exchange and waits for it to complete. It takes no arguments. Either the client or the server may trigger rekeying at any time.

Background: Rekeying periodically replaces the session keys to limit how much data is protected by any single key. RFC 4253 recommends it after roughly one gigabyte of transfer or one hour, whichever comes first. In practice servers initiate this automatically and Chilkat handles it transparently, so calling ReKey explicitly is rarely necessary.

Chilkat SQL Server Downloads

SQL Server
-- 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

    --  Demonstrates the Ssh.ReKey method, which initiates an SSH key re-exchange and waits for it to
    --  complete.  It takes no arguments.  In normal use the server initiates rekeying as needed and
    --  Chilkat handles it transparently.

    DECLARE @ssh int
    EXEC @hr = sp_OACreate 'Chilkat.Ssh', @ssh OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    --  Connect to the SSH server.  Port 22 is the usual SSH port.
    DECLARE @sshPort int
    SELECT @sshPort = 22
    EXEC sp_OAMethod @ssh, 'Connect', @success OUT, 'ssh.example.com', @sshPort
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @ssh, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @ssh
        RETURN
      END

    --  Normally you would not hard-code the password in source.  You should instead obtain it
    --  from an interactive prompt, environment variable, or a secrets vault.
    DECLARE @password nvarchar(4000)
    SELECT @password = 'mySshPassword'

    EXEC sp_OAMethod @ssh, 'AuthenticatePw', @success OUT, 'mySshLogin', @password
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @ssh, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @ssh
        RETURN
      END

    --  Explicitly initiate a key re-exchange on the live connection.
    EXEC sp_OAMethod @ssh, 'ReKey', @success OUT
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @ssh, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @ssh
        RETURN
      END

    PRINT 'Key re-exchange complete.'

    EXEC sp_OAMethod @ssh, 'Disconnect', NULL

    EXEC @hr = sp_OADestroy @ssh


END
GO