![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| Guest | StdOut and StdErr reading... 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 |
My System Specs![]() |
| | #2 (permalink) |
| Guest | Re: StdOut and StdErr reading... lucadentella@xxxxxx wrote: Quote: > 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. > I can't say exactly what is going wrong with your script, but I can say that stdin/out can get tricky. In some cases a careful use of "sleep" can help, that is, by allowing time for things to develop. For example, if you run you app "manually", and see some delays as it is working, then when you script it, that would be the place (or places) to insert your delay(s). If nothing else, without any "sleep" calls (analagous to vb's DoEvents) you may be "hogging-the-cpu", and not allowing your other app (which is doing the "real work") to have a chance at getting the cpu so it can get going... The following snippet of code did not work for me until after I inserted the "sleep". --- <code> --- Set oExec = WshShell.Exec(sScriptFolder & "testCase.bat") sInput = "" Do While True If Not oExec.StdOut.AtEndOfStream Then ' sInput = sInput & oExec.StdOut.Read(1) ' read one character sInput = sInput & oExec.StdOut.ReadLine & vbCrLf ' WScript.Echo sInput If InStr(sInput, "Press any key") <> 0 Then Exit Do End If WScript.Sleep 10 Loop oExec.StdIn.Write vbCrLf --- </code> --- cheers, jw ____________________________________________________________ You got questions? WE GOT ANSWERS!!! ..(but, no guarantee the answers will be applicable to the questions) |
My System Specs![]() |
| | #3 (permalink) |
| Guest | Re: StdOut and StdErr reading... <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 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 |
My System Specs![]() |
| | #4 (permalink) |
| Guest | Re: StdOut and StdErr reading... Hello! Thanks for your answers... the problem I'm facing is about StdErr: I need a way to read exaclty all the characters in that moment are in that buffer, but ReadAll, AtEndOfStream are blocking. Is there a way to count the chars so I can use Read(n)? Quote: > 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 |
My System Specs![]() |
| | #5 (permalink) |
| Guest | Re: StdOut and StdErr reading... Il giorno Thu, 7 Aug 2008 01:50:50 -0700 (PDT), lucadentella@xxxxxx ha scritto: Quote: >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? I dropped a project I was working on (reading e-mails using vbs as an interface for netcat) because I had some problems as yours. Can you confirm I can read a stream before it is closed and while the output is still being writing? Thank you. Giovanni. -- Giovanni Cenati (Bergamo, Italy) Write to "Reventlov" at katamail com http://digilander.libero.it/Cenati (Esempi e programmi in VbScript) -- |
My System Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| Deadlock when logging to stdout and stderr | PowerShell | |||
| redirect powershell stdout to objShell.Exec.Stdout.ReadAll()( | PowerShell | |||
| Output to STDERR | PowerShell | |||
| Catching stdout AND stderr | PowerShell | |||
| Help with stderr from native console application | PowerShell | |||