Sample code for 30+ languages & platforms
VBScript

Collect Completed SSH Commands with QuickCmdCheck

See more SSH Examples

Demonstrates the Chilkat Ssh.QuickCmdCheck method, which waits for any command started by QuickCmdSend to complete. The only argument is the poll timeout in milliseconds; 0 performs a nonblocking check. It returns the channel number of a completed command, -1 when commands are still pending but none finished before the timeout, or -2 when no quick commands remain or an error occurred.

Background: This is the collection half of the concurrent pattern. Each completed channel is reported only once, so looping until -2 drains every outstanding command exactly once. Distinguish the two negative values carefully: -1 simply means "still working, ask again," while -2 means there is nothing left to wait for — or that the connection failed, so check LastErrorText when the distinction matters. Manually opened exec channels are not reported here.

Chilkat VBScript Downloads

VBScript
Dim fso, outFile
Set fso = CreateObject("Scripting.FileSystemObject")
'Create a Unicode (utf-16) output text file.
Set outFile = fso.CreateTextFile("output.txt", True, True)

success = 0

'  Demonstrates the Ssh.QuickCmdCheck method, which waits for any command started by
'  QuickCmdSend to complete.  The only argument is the poll timeout in milliseconds; 0 performs
'  a nonblocking check.

set ssh = CreateObject("Chilkat.Ssh")

sshPort = 22
success = ssh.Connect("ssh.example.com",sshPort)
If (success = 0) Then
    outFile.WriteLine(ssh.LastErrorText)
    WScript.Quit
End If

'  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.
password = "mySshPassword"

success = ssh.AuthenticatePw("mySshLogin",password)
If (success = 0) Then
    outFile.WriteLine(ssh.LastErrorText)
    WScript.Quit
End If

'  Start two commands that will finish at different times.
channel1 = ssh.QuickCmdSend("sleep 2; echo first done")
If (channel1 < 0) Then
    outFile.WriteLine(ssh.LastErrorText)
    WScript.Quit
End If

channel2 = ssh.QuickCmdSend("echo second done")
If (channel2 < 0) Then
    outFile.WriteLine(ssh.LastErrorText)
    WScript.Quit
End If

'  Collect the results as they complete.  The return value is the channel number of a completed
'  command, -1 if commands are still pending but none finished before the timeout, or -2 when
'  no quick commands remain (or an error occurred).
pollTimeoutMs = 5000
completedChannel = ssh.QuickCmdCheck(pollTimeoutMs)
Do While completedChannel <> -2
    If (completedChannel >= 0) Then
        output = ssh.GetReceivedText(completedChannel,"utf-8")
        If (ssh.LastMethodSuccess = 0) Then
            outFile.WriteLine(ssh.LastErrorText)
            WScript.Quit
        End If

        outFile.WriteLine("Channel " & completedChannel & " finished: " & output)
    End If

    If (completedChannel = -1) Then
        outFile.WriteLine("Still waiting for a command to finish...")
    End If

    completedChannel = ssh.QuickCmdCheck(pollTimeoutMs)
Loop

outFile.WriteLine("All quick commands have been collected.")

ssh.Disconnect 

outFile.Close