Sample code for 30+ languages & platforms
SQL Server

Collect Completed SSH Commands with QuickCmdCheck

See more SSH Examples

Demonstrates the Chilkat Ssh.QuickCmdCheck method, which waits for any command started by QuickCmdSend to complete. The only argument is the poll timeout in milliseconds; 0 performs a nonblocking check. It returns the channel number of a completed command, -1 when commands are still pending but none finished before the timeout, or -2 when no quick commands remain or an error occurred.

Background: This is the collection half of the concurrent pattern. Each completed channel is reported only once, so looping until -2 drains every outstanding command exactly once. Distinguish the two negative values carefully: -1 simply means "still working, ask again," while -2 means there is nothing left to wait for — or that the connection failed, so check LastErrorText when the distinction matters. Manually opened exec channels are not reported here.

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
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

    --  Demonstrates the Ssh.QuickCmdCheck method, which waits for any command started by
    --  QuickCmdSend to complete.  The only argument is the poll timeout in milliseconds; 0 performs
    --  a nonblocking check.

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

    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

    --  Start two commands that will finish at different times.
    DECLARE @channel1 int
    EXEC sp_OAMethod @ssh, 'QuickCmdSend', @channel1 OUT, 'sleep 2; echo first done'
    IF @channel1 < 0
      BEGIN
        EXEC sp_OAGetProperty @ssh, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @ssh
        RETURN
      END

    DECLARE @channel2 int
    EXEC sp_OAMethod @ssh, 'QuickCmdSend', @channel2 OUT, 'echo second done'
    IF @channel2 < 0
      BEGIN
        EXEC sp_OAGetProperty @ssh, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @ssh
        RETURN
      END

    --  Collect the results as they complete.  The return value is the channel number of a completed
    --  command, -1 if commands are still pending but none finished before the timeout, or -2 when
    --  no quick commands remain (or an error occurred).
    DECLARE @pollTimeoutMs int
    SELECT @pollTimeoutMs = 5000
    DECLARE @completedChannel int
    EXEC sp_OAMethod @ssh, 'QuickCmdCheck', @completedChannel OUT, @pollTimeoutMs
    WHILE @completedChannel <> -2
      BEGIN
        IF @completedChannel >= 0
          BEGIN
            DECLARE @output nvarchar(4000)
            EXEC sp_OAMethod @ssh, 'GetReceivedText', @output OUT, @completedChannel, 'utf-8'
            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 'Channel ' + @completedChannel + ' finished: ' + @output
          END

        IF @completedChannel = -1
          BEGIN

            PRINT 'Still waiting for a command to finish...'
          END

        EXEC sp_OAMethod @ssh, 'QuickCmdCheck', @completedChannel OUT, @pollTimeoutMs
      END


    PRINT 'All quick commands have been collected.'

    EXEC sp_OAMethod @ssh, 'Disconnect', NULL

    EXEC @hr = sp_OADestroy @ssh


END
GO