Sample code for 30+ languages & platforms
Tcl

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

Tcl

load ./chilkat.dll

set success 0

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

set ssh [new_CkSsh]

set sshPort 22
set success [CkSsh_Connect $ssh "ssh.example.com" $sshPort]
if {$success == 0} then {
    puts [CkSsh_lastErrorText $ssh]
    delete_CkSsh $ssh
    exit
}

#  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.
set password "mySshPassword"

set success [CkSsh_AuthenticatePw $ssh "mySshLogin" $password]
if {$success == 0} then {
    puts [CkSsh_lastErrorText $ssh]
    delete_CkSsh $ssh
    exit
}

#  Start three commands without waiting for any of them to finish.
set channel1 [CkSsh_QuickCmdSend $ssh "sleep 2; echo first done"]
if {$channel1 < 0} then {
    puts [CkSsh_lastErrorText $ssh]
    delete_CkSsh $ssh
    exit
}

set channel2 [CkSsh_QuickCmdSend $ssh "echo second done"]
if {$channel2 < 0} then {
    puts [CkSsh_lastErrorText $ssh]
    delete_CkSsh $ssh
    exit
}

set channel3 [CkSsh_QuickCmdSend $ssh "sleep 1; echo third done"]
if {$channel3 < 0} then {
    puts [CkSsh_lastErrorText $ssh]
    delete_CkSsh $ssh
    exit
}

puts "Started three concurrent commands."

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

CkSsh_Disconnect $ssh

delete_CkSsh $ssh