Sample code for 30+ languages & platforms
Lua

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

Lua
local chilkat = require("chilkat")

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

local ssh = chilkat.Ssh.new()

local sshPort = 22
local success = ssh:connect("ssh.example.com", sshPort)
if not success then
    print(ssh.last_error_text)
    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.
local password = "mySshPassword"

success = ssh:authenticate_pw("mySshLogin", password)
if not success then
    print(ssh.last_error_text)
    return
end

-- Start three commands without waiting for any of them to finish.
local channel1 = ssh:quick_cmd_send("sleep 2; echo first done")
if channel1 < 0 then
    print(ssh.last_error_text)
    return
end

local channel2 = ssh:quick_cmd_send("echo second done")
if channel2 < 0 then
    print(ssh.last_error_text)
    return
end

local channel3 = ssh:quick_cmd_send("sleep 1; echo third done")
if channel3 < 0 then
    print(ssh.last_error_text)
    return
end

print("Started three concurrent commands.")

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

ssh:disconnect()