Sample code for 30+ languages & platforms
DataFlex

Start Concurrent SSH Commands with QuickCmdSend

See more SSH Examples

Demonstrates the Chilkat Ssh.QuickCmdSend method, which starts a remote command and immediately returns its channel number. The only argument is the command line. Internally it opens a session channel and sends an exec request.

Background: Because it returns without waiting, several commands can run concurrently over one authenticated connection — far faster than running them one after another when each has latency or slow work to do. Pair it with QuickCmdCheck, which reports commands as they finish, so results can be collected in completion order rather than the order they were started.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoSsh
    Integer iSshPort
    String sPassword
    Integer iChannel1
    Integer iChannel2
    Integer iChannel3
    String sTemp1

    Move False To iSuccess

    //  Demonstrates the Ssh.QuickCmdSend method, which starts a remote command and immediately
    //  returns its channel number.  The only argument is the command line.  Because it returns
    //  right away, several commands can run concurrently on the same SSH connection.

    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 three commands without waiting for any of them to finish.
    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

    Get ComQuickCmdSend Of hoSsh "sleep 1; echo third done" To iChannel3
    If (iChannel3 < 0) Begin
        Get ComLastErrorText Of hoSsh To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Showln "Started three concurrent commands."

    //  Use QuickCmdCheck to collect the results as each command completes.

    Send ComDisconnect To hoSsh


End_Procedure