Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
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.

Go Back   Vista Forums > Misc Newsgroups > VB Script

Vista - StdOut and StdErr reading...

Reply
 
Old 08-07-2008   #1 (permalink)
lucadentella


 
 

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 SpecsSystem Spec
Old 08-07-2008   #2 (permalink)
mr_unreliable


 
 

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.
>
hi Lucadentella,

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 SpecsSystem Spec
Old 08-07-2008   #3 (permalink)
Pegasus \(MVP\)


 
 

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
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 SpecsSystem Spec
Old 08-08-2008   #4 (permalink)
lucadentella


 
 

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 SpecsSystem Spec
Old 08-08-2008   #5 (permalink)
Reventlov


 
 

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?
Hello,
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 SpecsSystem Spec
Reply

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


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46