Sample code for 30+ languages & platforms
DataFlex

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 DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoSsh
    Integer iSshPort
    String sPassword
    Integer iChannel1
    Integer iChannel2
    Integer iPollTimeoutMs
    Integer iCompletedChannel
    String sOutput
    String sTemp1
    Boolean bTemp1

    Move False To iSuccess

    //  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.

    Get Create (RefClass(cComChilkatSsh)) To hoSsh
    If (Not(IsComObjectCreated(hoSsh))) Begin
        Send CreateComObject of hoSsh
    End

    Move 22 To iSshPort
    Get ComConnect Of hoSsh "ssh.example.com" iSshPort To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoSsh To sTemp1
        Showln sTemp1
        Procedure_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.
    Move "mySshPassword" To sPassword

    Get ComAuthenticatePw Of hoSsh "mySshLogin" sPassword To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoSsh To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  Start two commands that will finish at different times.
    Get ComQuickCmdSend Of hoSsh "sleep 2; echo first done" To iChannel1
    If (iChannel1 < 0) Begin
        Get ComLastErrorText Of hoSsh To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComQuickCmdSend Of hoSsh "echo second done" To iChannel2
    If (iChannel2 < 0) Begin
        Get ComLastErrorText Of hoSsh To sTemp1
        Showln sTemp1
        Procedure_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).
    Move 5000 To iPollTimeoutMs
    Get ComQuickCmdCheck Of hoSsh iPollTimeoutMs To iCompletedChannel
    While (iCompletedChannel <> -2)
        If (iCompletedChannel >= 0) Begin
            Get ComGetReceivedText Of hoSsh iCompletedChannel "utf-8" To sOutput
            Get ComLastMethodSuccess Of hoSsh To bTemp1
            If (bTemp1 = False) Begin
                Get ComLastErrorText Of hoSsh To sTemp1
                Showln sTemp1
                Procedure_Return
            End

            Showln "Channel " iCompletedChannel " finished: " sOutput
        End

        If (iCompletedChannel = -1) Begin
            Showln "Still waiting for a command to finish..."
        End

        Get ComQuickCmdCheck Of hoSsh iPollTimeoutMs To iCompletedChannel
    Loop

    Showln "All quick commands have been collected."

    Send ComDisconnect To hoSsh


End_Procedure