Sample code for 30+ languages & platforms
Visual FoxPro

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 Visual FoxPro Downloads

Visual FoxPro
LOCAL lnSuccess
LOCAL loSsh
LOCAL lnPort
LOCAL lcPassword
LOCAL lnChannel1
LOCAL lnChannel2
LOCAL lnChannel3
LOCAL lnPollTimeoutMs
LOCAL lnNumFinished
LOCAL lnChannel
LOCAL lcCmdOutput

lnSuccess = 0

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

loSsh = CreateObject('Chilkat.Ssh')

lnPort = 22
lnSuccess = loSsh.Connect("ssh.example.com",lnPort)
IF (lnSuccess = 0) THEN
    ? loSsh.LastErrorText
    RELEASE loSsh
    CANCEL
ENDIF

*  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.
lcPassword = "mySshPassword"

lnSuccess = loSsh.AuthenticatePw("mySshLogin",lcPassword)
IF (lnSuccess = 0) THEN
    ? loSsh.LastErrorText
    RELEASE loSsh
    CANCEL
ENDIF

*  QuickCmdSend starts a command and returns immediately, so all three run concurrently, each on
*  its own session channel.
lnChannel1 = loSsh.QuickCmdSend("df")
IF (lnChannel1 < 0) THEN
    ? loSsh.LastErrorText
    RELEASE loSsh
    CANCEL
ENDIF

lnChannel2 = loSsh.QuickCmdSend("date")
IF (lnChannel2 < 0) THEN
    ? loSsh.LastErrorText
    RELEASE loSsh
    CANCEL
ENDIF

lnChannel3 = loSsh.QuickCmdSend("echo hello world")
IF (lnChannel3 < 0) THEN
    ? loSsh.LastErrorText
    RELEASE loSsh
    CANCEL
ENDIF

*  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).
lnPollTimeoutMs = 50
lnNumFinished = 0
DO WHILE lnNumFinished < 3
    lnChannel = loSsh.QuickCmdCheck(lnPollTimeoutMs)
    IF (lnChannel = -2) THEN
        ? loSsh.LastErrorText
        RELEASE loSsh
        CANCEL
    ENDIF

    IF (lnChannel >= 0) THEN
        ? "---- channel " + STR(lnChannel) + " finished ----"
        lcCmdOutput = loSsh.GetReceivedText(lnChannel,"utf-8")
        IF (loSsh.LastMethodSuccess = 0) THEN
            ? loSsh.LastErrorText
            RELEASE loSsh
            CANCEL
        ENDIF

        ? lcCmdOutput
        lnNumFinished = lnNumFinished + 1
    ENDIF

ENDDO

loSsh.Disconnect()

RELEASE loSsh