<lucadentella@xxxxxx> wrote in message
news:80ae145e-64b5-4a22-babb-4da21a080ccf@xxxxxx
Quote:
> Hello!
>
> I'm working on a script to automate SFTP operations (using a text-
> based sftp client).
> My idea is to use the Exec() method to run the client, send commands
> via StdIn and receive output via StdOut and StdErr.
>
> The problem is that when I try to read buffers using ReadAll or
> AtEndOfStream, those methods are blocking and never ends... for StdOut
> I was able to find a condiction (when I read "sftp>" from the buffer)
> to end reading, but for StdErr? Is there a way to know how many chars
> are in the buffer to use the Read(n) method which is not blocking?
>
> Thanks!
>
> ---
> Dim objFSO, objShell, objCmd
>
> Set objFSO = CreateObject("Scripting.FileSystemObject")
> Set objShell = CreateObject("WScript.Shell")
> Set objCmd = objShell.Exec("sftp -F /home/sshuser/.ssh/ssh_config
> sshuser@xxxxxx")
>
> WScript.Echo ReadOutput(objCmd.StdOut)
>
> objCmd.StdIn.WriteLine("pwd")
> WScript.Echo ReadOutput(objCmd.StdOut)
>
> objCmd.StdIn.WriteLine("ls")
> WScript.Echo ReadOutput(objCmd.StdOut)
>
> objCmd.StdIn.WriteLine("ls download.ok")
> WScript.Echo ReadOutput(objCmd.StdOut)
> WScript.Echo objCmd.StdErr.ReadAll ' <-- blocking!!!
>
> WScript.Echo "close"
> objCmd.StdIn.WriteLine("close")
>
>
> Function ReadOutput(stream)
>
> Dim strOut
>
> Do
> If Not stream.AtEndOfStream Then
> strOut = strOut & stream.Read(1)
> If Right(strOut, 5) = "sftp>" Then
> strOut = Left(strOut, Len(strOut) - 5)
> Exit Do
> End If
> End If
> Loop
>
> ReadOutput = strOut
>
> End Function
Your ReadOutput function is flawed: It will loop forever unless it
finds the string "sftp" in the stream. Try this instead:
Function ReadOutput(stream)
Do
If Not stream.AtEndOfStream Then
strOut = strOut & stream.Read(1)
If Right(strOut, 5) = "sftp>" Then
strOut = Left(strOut, Len(strOut) - 5)
Exit Do
End If
Else
Exit Do
End If
Loop
ReadOutput = strOut
End Function