DataFlex
DataFlex
SSH Parallel Remote Commands on a Single Server
See more SSH Examples
Demonstrates running several commands in parallel on one SSH server and collecting each command's output as it finishes. QuickCmdSend starts each command and returns immediately; QuickCmdCheck reports them as they complete.
Background: Because SSH multiplexes channels, several commands can run at once over one authenticated connection — far faster than running them one after another when each involves waiting. Results arrive in completion order rather than the order the commands were started, so the loop keys off the returned channel number. Distinguish the two negative returns carefully:
-1 means "still working, ask again," while -2 means nothing remains to collect or the connection failed.Chilkat DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoSsh
Integer iPort
String sPassword
Integer iChannel1
Integer iChannel2
Integer iChannel3
Integer iPollTimeoutMs
Integer iNumFinished
Integer iChannel
String sCmdOutput
String sTemp1
Boolean bTemp1
Move False To iSuccess
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// Demonstrates running several commands in parallel on a single SSH server and collecting each
// command's output as it finishes.
Get Create (RefClass(cComChilkatSsh)) To hoSsh
If (Not(IsComObjectCreated(hoSsh))) Begin
Send CreateComObject of hoSsh
End
Move 22 To iPort
Get ComConnect Of hoSsh "ssh.example.com" iPort 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
// QuickCmdSend starts a command and returns immediately, so all three run concurrently, each on
// its own session channel.
Get ComQuickCmdSend Of hoSsh "df" To iChannel1
If (iChannel1 < 0) Begin
Get ComLastErrorText Of hoSsh To sTemp1
Showln sTemp1
Procedure_Return
End
Get ComQuickCmdSend Of hoSsh "date" To iChannel2
If (iChannel2 < 0) Begin
Get ComLastErrorText Of hoSsh To sTemp1
Showln sTemp1
Procedure_Return
End
Get ComQuickCmdSend Of hoSsh "echo hello world" To iChannel3
If (iChannel3 < 0) Begin
Get ComLastErrorText Of hoSsh To sTemp1
Showln sTemp1
Procedure_Return
End
// Collect the results. QuickCmdCheck returns the channel number of a completed command,
// -1 when commands are still pending but none finished within the timeout, or -2 when nothing
// remains to be checked (or an error occurred).
Move 50 To iPollTimeoutMs
Move 0 To iNumFinished
While (iNumFinished < 3)
Get ComQuickCmdCheck Of hoSsh iPollTimeoutMs To iChannel
If (iChannel = -2) Begin
Get ComLastErrorText Of hoSsh To sTemp1
Showln sTemp1
Procedure_Return
End
If (iChannel >= 0) Begin
Showln "---- channel " iChannel " finished ----"
Get ComGetReceivedText Of hoSsh iChannel "utf-8" To sCmdOutput
Get ComLastMethodSuccess Of hoSsh To bTemp1
If (bTemp1 = False) Begin
Get ComLastErrorText Of hoSsh To sTemp1
Showln sTemp1
Procedure_Return
End
Showln sCmdOutput
Move (iNumFinished + 1) To iNumFinished
End
Loop
Send ComDisconnect To hoSsh
End_Procedure