Sample code for 30+ languages & platforms
Lianja

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

Lianja
llSuccess = .F.

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

loSsh = createobject("CkSsh")

lnSshPort = 22
llSuccess = loSsh.Connect("ssh.example.com",lnSshPort)
if (llSuccess = .F.) then
    ? loSsh.LastErrorText
    release loSsh
    return
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"

llSuccess = loSsh.AuthenticatePw("mySshLogin",lcPassword)
if (llSuccess = .F.) then
    ? loSsh.LastErrorText
    release loSsh
    return
endif

//  Start three commands without waiting for any of them to finish.
lnChannel1 = loSsh.QuickCmdSend("sleep 2; echo first done")
if (lnChannel1 < 0) then
    ? loSsh.LastErrorText
    release loSsh
    return
endif

lnChannel2 = loSsh.QuickCmdSend("echo second done")
if (lnChannel2 < 0) then
    ? loSsh.LastErrorText
    release loSsh
    return
endif

lnChannel3 = loSsh.QuickCmdSend("sleep 1; echo third done")
if (lnChannel3 < 0) then
    ? loSsh.LastErrorText
    release loSsh
    return
endif

? "Started three concurrent commands."

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

loSsh.Disconnect()


release loSsh