Sample code for 30+ languages & platforms
SQL Server

SSH Keyboard Authentication

See more SSH Examples

Demonstrates how to implement keyboard authentication with an SSH server.

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 @iTmp0 int
    -- Important: Do not use nvarchar(max).  See the warning about using nvarchar(max).
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

    -- This example assumes the Chilkat API to have been previously unlocked.
    -- See Global Unlock Sample for sample code.

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

    -- Set some timeouts, in milliseconds:
    EXEC sp_OASetProperty @ssh, 'ConnectTimeoutMs', 5000
    EXEC sp_OASetProperty @ssh, 'IdleTimeoutMs', 15000

    -- Connect to the SSH server.  
    -- The standard SSH port = 22
    -- The hostname may be a hostname or IP address.
    DECLARE @hostname nvarchar(4000)
    SELECT @hostname = 'sftp.example.com'
    DECLARE @port int
    SELECT @port = 22
    EXEC sp_OAMethod @ssh, 'Connect', @success OUT, @hostname, @port
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @ssh, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @ssh
        RETURN
      END

    -- Begin keyboard authentication..
    DECLARE @xmlResponse nvarchar(4000)
    EXEC sp_OAMethod @ssh, 'StartKeyboardAuth', @xmlResponse OUT, 'myLogin'
    EXEC sp_OAGetProperty @ssh, 'LastMethodSuccess', @iTmp0 OUT
    IF @iTmp0 <> 1
      BEGIN
        EXEC sp_OAGetProperty @ssh, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @ssh
        RETURN
      END

    -- If a user authentication banner was received, then your app
    -- may display it prior to prompting for the password.

    EXEC sp_OAGetProperty @ssh, 'UserAuthBanner', @sTmp0 OUT
    PRINT 'UserAuthBanner: ' + @sTmp0

    DECLARE @xml int
    EXEC @hr = sp_OACreate 'Chilkat.Xml', @xml OUT

    EXEC sp_OAMethod @xml, 'LoadXml', @success OUT, @xmlResponse
    -- Assume LoadXml succeeds for the example..
    EXEC sp_OAMethod @xml, 'HasChildWithTag', @iTmp0 OUT, 'success'
    IF @iTmp0 = 1
      BEGIN

        PRINT 'No password required, already authenticated.'
        EXEC @hr = sp_OADestroy @ssh
        EXEC @hr = sp_OADestroy @xml
        RETURN
      END
    EXEC sp_OAMethod @xml, 'HasChildWithTag', @iTmp0 OUT, 'error'
    IF @iTmp0 = 1
      BEGIN

        PRINT 'Authentication already failed.'
        EXEC @hr = sp_OADestroy @ssh
        EXEC @hr = sp_OADestroy @xml
        RETURN
      END

    -- See the online reference documentation for Chilkat SSH.
    -- The XML returned by StartKeyboardAuth will contain an infoRequest
    -- with one or more prompts that your application may choose to display.

    -- Call ContinueKeyboardAuth, passing in the whatever information is requires (such as the password).
    -- Typically, keyboard authentication requires one call to ContinueKeyboardAuth 
    -- using the password.  Theoretically, the SSH server could prompt for additional pieces
    -- of information.  The authentication is completed when the XML returned contains
    -- either a "success" or "error" child node.

    -- This example asumes only one call to ContinueKeyboardAuth is required.
    EXEC sp_OAMethod @ssh, 'ContinueKeyboardAuth', @xmlResponse OUT, 'myPassword'
    EXEC sp_OAGetProperty @ssh, 'LastMethodSuccess', @iTmp0 OUT
    IF @iTmp0 <> 1
      BEGIN
        EXEC sp_OAGetProperty @ssh, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @ssh
        EXEC @hr = sp_OADestroy @xml
        RETURN
      END

    EXEC sp_OAMethod @xml, 'LoadXml', @success OUT, @xmlResponse
    -- Assume LoadXml succeeds for the example..
    EXEC sp_OAMethod @xml, 'HasChildWithTag', @iTmp0 OUT, 'success'
    IF @iTmp0 = 1
      BEGIN

        PRINT 'SSH Keyboard Authentication Successful!'
        EXEC @hr = sp_OADestroy @ssh
        EXEC @hr = sp_OADestroy @xml
        RETURN
      END
    EXEC sp_OAMethod @xml, 'HasChildWithTag', @iTmp0 OUT, 'error'
    IF @iTmp0 = 1
      BEGIN

        PRINT 'Authentication failed.'
        EXEC @hr = sp_OADestroy @ssh
        EXEC @hr = sp_OADestroy @xml
        RETURN
      END

    EXEC @hr = sp_OADestroy @ssh
    EXEC @hr = sp_OADestroy @xml


END
GO