Sign in to follow this  
Followers 0
ghetek

StderrRead reads nothing until application close

1 post in this topic

Hey guys, I am trying to automate the process of connecting to the Tor network. Currently they do use a nice GUI called Vidalia but I want to customize and simplify the process. Currently I am working with 2 executables, tor.exe and polipo.exe. Tor was super easy to tap into, just a stdoutread. Unfortunately Polipo would not give anything on the out stream and I had to use StdErr.

The problem

For some reason, while tor easily goes from stream to console, I only get output from Polipo after it has closed. The StdErr stream returns nothing while the application is open and then spits out everything as soon as it closes. has anybody else run into this problem when reading from a console? I have attached my current script but it is still in its infancy stages.

#include <File.au3>
#include <Constants.au3>
OnAutoItExitRegister("ToolCleanup")
Global $ConnectionState = 0
Global $Hnd_Tor, $Hnd_Polipo, _
  $TorConsole, $PolipoConsole, _
  $Dir_Tor = @ScriptDir & "\Tor", _
  $DirPolipo = @ScriptDir & "\Polipo", _
  $Exe_Tor = $Dir_Tor & "\tor.exe", _
  $Conf_Tor = $Dir_Tor & "\torrc", _
  $Exe_Polipo = $DirPolipo & "\polipo.exe", _
  $Conf_Polipo = $DirPolipo & "\polipo.conf"
TorConfig($Conf_Tor)
While 1
Switch $ConnectionState
  Case 0
   $Hnd_Tor = RunTor($Exe_Tor, $Conf_Tor)
   If $Hnd_Tor <> 0 Then
    $ConnectionState = 1
    ConsoleWrite("Connection State 1 (Tor PID:" & $Hnd_Tor & ")" & @CRLF)
   EndIf
  Case 1
   If StringInStr($TorConsole, "Bootstrapped 100%", 2) Then
    $Hnd_Polipo = RunPolipo($Exe_Polipo, $Conf_Polipo)
    If $Hnd_Polipo <> 0 Then
     $ConnectionState = 2
     ConsoleWrite("Connection State 2 (Polipo PID:" & $Hnd_Polipo & ")" & @CRLF)
    EndIf
   EndIf
EndSwitch
$PolipoConsole = StdErrRead($Hnd_Polipo)
$TorConsole = StdoutRead($Hnd_Tor)
ConsoleWrite($TorConsole)
ConsoleWrite($PolipoConsole)
WEnd
Func RunTor($Exe, $Conf)
Do
  ProcessClose("tor.exe")
Until ProcessExists("Tor.exe") = 0
$hTor = Run('"' & $Exe_Tor & '" -f "' & $Conf_Tor & '"', "", @SW_SHOW, $STDERR_CHILD + $STDOUT_CHILD)
Return $hTor
EndFunc   ;==>RunTor
Func TorConfig($Conf)
Local $aTorConf
_FileReadToArray($Conf, $aTorConf)
For $i = 1 To $aTorConf[0]
  If StringInStr($aTorConf[$i], "DataDirectory", 2) = 1 Then
   $aTorConf[$i] = "DataDirectory " & @ScriptDir & "\Tor\data"
  EndIf
Next
_FileWriteFromArray($Conf, $aTorConf, 1)
EndFunc   ;==>TorConfig
Func RunPolipo($Exe_Polipo, $Conf_Polipo)
Do
  ProcessClose("polipo.exe")
Until ProcessExists("polipo.exe") = 0
$hPolipo = Run('"' & $Exe_Polipo & '" -c "' & $Conf_Polipo & '" ', "", @SW_SHOW, $STDIN_CHILD + $STDERR_CHILD)
Return $hPolipo
EndFunc   ;==>RunPolipo
Func ToolCleanup()
Do
  ProcessClose("polipo.exe")
Until ProcessExists("polipo.exe") = 0
Do
  ProcessClose("tor.exe")
Until ProcessExists("Tor.exe") = 0
EndFunc   ;==>ToolCleanup

Here is a link to someone else with this issue (unresolved)

Share this post


Link to post
Share on other sites



Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0

  • Similar Content

    • Skysnake
      By Skysnake
      Perhaps someone would benefit off this.  I made heavy use of the Help file example.
      Only question I have here, is is there a better way to do the Regex for finding "error|ERROR|Error" in the source string?  Thx
      Example7zPwd() Func Example7zPwd() ;-- Local $iPID = Run(@ComSpec & " /c DIR Example.au3", @SystemDir, @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD) Local $iPID = Run(@ComSpec & " /c 7za t -pmasale myzip.zip ", "c:\files\testing", @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD) Local $sOutput = "" Local $myError = 0 ConsoleWrite("$myError:" & $myError & @CRLF) While 1 $sOutput = StdoutRead($iPID) If @error Then ; Exit the loop if the process closes or StdoutRead returns an error. ExitLoop EndIf MsgBox($MB_SYSTEMMODAL, "Stdout Read:", $sOutput, 5) If StringRegExp($sOutput, '\b(error|ERROR|[Ee]rror)\b', 0) Then $myError = $myError + 1 ;ConsoleWrite("$sOutput: " & $sOutput & @CRLF) ConsoleWrite("$myError: " & $myError & @CRLF) WEnd While 1 $sOutput = StderrRead($iPID) If @error Then ; Exit the loop if the process closes or StderrRead returns an error. ExitLoop EndIf MsgBox($MB_SYSTEMMODAL, "Stderr Read:", $sOutput, 15) WEnd ConsoleWrite("$myError: " & $myError & @CRLF) If $myError > 0 Then MsgBox(64, "An Error Occurred", "The upgrade may be incomplete. An error occurred") EndIf If StringRegExp($sOutput, '\b(error|ERROR|[Ee]rror)\b', 0) Then Is the Regex here optimized?
       
       
    • toasterking
      By toasterking
      Someone told me I should post this in the examples section.  I wrote this to make it easy for me to call a Windows console application and get its output using a single line of AutoIt code.  I hope it's helpful to someone else.
       
      #include <Constants.au3> ; Examples: MsgBox(0,"Windows Version",_RunWaitGet(@ComSpec & " /c ver",1,"",@SW_HIDE)) MsgBox(0,"System Info",_RunWaitGet(@SystemDir & "\systeminfo.exe",1)) ; #FUNCTION# ==================================================================================================================== ; Name ..........: _RunWaitGet ; Description ...: Runs the specified process, waits for it to exit, then returns the contents of its StdOut and/or StdErr streams. ; Handy for running command-line tools and getting their output. ; Syntax ........: _RunWaitGet($sProgram, $nOptions, $sWorkingDir, $nShowFlag) ; Parameters ....: $sProgram - The full path of the program (EXE, BAT, COM, or PIF) to run ; $nOptions - Add options together: ; 1 = Capture the StdOut stream. ; 2 = Capture the StdErr stream. ; 4 = Return when the stream(s) close(s), not when the process ends. ; $sWorkingDir - The working directory. Blank ("") uses the current working directory. ; This is not the path to the program. ; $nShowFlag - The "show" flag of the executed program: ; @SW_SHOW = Show window (default) ; @SW_HIDE = Hidden window (or Default keyword) ; @SW_MINIMIZE = Minimized window ; @SW_MAXIMIZE = Maximized window ; Return values .: String value containing the captured contents. ; If there was a problem running the process, @error is set to the @error value returned by Run(). ; Otherwise, @error is 0. ; Author ........: ToasterKing ; Modified ......: ; Remarks .......: ; Related .......: ; Link ..........: ; Example .......: MsgBox(0,"System Info",_RunWaitGet(@SystemDir & "\systeminfo.exe",1)) ; MsgBox(0,"Windows Version",_RunWaitGet(@ComSpec & " /c ver",1,"",@SW_HIDE)) ; =============================================================================================================================== Func _RunWaitGet($sProgram,$nOptions = 0,$sWorkingDir = @SystemDir,$nShowFlag = @SW_SHOW) Local $nRunOptFlags = 0,$sStreamOut = "" ; Initialize variables ; Determine flags for parent/child interaction If BitAND($nOptions,1) Then $nRunOptFlags += $STDOUT_CHILD If BitAND($nOptions,2) Then $nRunOptFlags += $STDERR_CHILD Local $hRunStream = Run($sProgram,$sWorkingDir,$nShowFlag,$nRunOptFlags) ; Run the process If @error Then Return SetError(@error,@extended,0) ; If there was an error code, return it. Otherwise... While 1 ; Loop until the end of the stream, which indicates that the process has closed it (which usually means the process ended) If BitAND($nOptions,1) Then ; If user specified to capture STDOUT stream... $sStreamOut &= StdoutRead($hRunStream) ; Append new stream contents to existing variable while removing those contents from the stream. If @error = 2 And BitAND($nOptions,4) Then ExitLoop ; If stream ended and user specified to return when the stream closes, stop looping. EndIf If BitAND($nOptions,2) Then ; If user specified to capture STDERR stream... $sStreamOut &= StderrRead($hRunStream) ; Append new stream contents to existing variable while removing those contents from the stream. If @error = 2 And BitAND($nOptions,4) Then ExitLoop ; If stream ended and user specified to return when the stream closes, stop looping. EndIf If Not BitAND($nOptions,4) And Not ProcessExists($hRunStream) Then ExitLoop ; If using the default setting and the process ended, stop looping. Sleep(100) ; To avoid overloading the CPU WEnd Return SetError(0,0,$sStreamOut) ; Return the captured contents and @error = 0 EndFunc  
    • therks
      By therks
      Is there any way to retrieve the exit code from a process while also being able to use StdoutRead/StderrRead?