Sign in to follow this  
Followers 0
ALanclos

Capturing tracert output to log file

4 posts in this topic

#1 ·  Posted (edited)

I have been tearing my hair out trying to get this script to write the output of tracert and pausing before it continues. I am sure there is an easier way to do this but I'm at my wit's end.  Here is what I have so far:

Func RunTraceRt ()
    Local $eMsg, $stdout, $stdoutAll
    Local $pID = Run(@ComSpec & " /c " & "tracert 8.8.8.8", @SW_HIDE, 6)
    While ProcessExists($pID)
        $eMsg = StderrRead($pID, 0, 0)
        If @error Then ExitLoop
        $stdOut = StdoutRead($pid, 0, 0)
        If Not @error Then
            $stdoutAll &= $stdOut
        EndIf
    WEnd
    Local $hOpenFile = FileOpen($fileName, $FO_Append)
    FileWrite($hOpenFile,$StdOutALL)
EndFunc

Thanks in advance.

I've also tried this but nothing gets written to the file.

Func RunTraceRt ()
    Local $hOpenFile = FileOpen($fileName, 1)
    FileWrite($hOpenFile, _GetDOSOutput("tracert 8.8.8.8"))
    FileClose($hOpenFile)
EndFunc
Func _GetDOSOutput($sCommand)
    Local $iPID, $sOutput = ""

    $iPID = Run('"' & @ComSpec & '" /c ' & $sCommand, "", @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)
    While 1
        $sOutput &= StdoutRead($iPID, False, False)
        If @error Then
            ExitLoop
        EndIf
        Sleep(10)
    WEnd
    Return $sOutput
EndFunc   ;==>_GetDOSOutput
Edited by ALanclos

Share this post


Link to post
Share on other sites

#2 ·  Posted (edited)

hello ALanclos, welcome to AutoIt and to the forum!

not that i don't appreciate good stdout work, but console programs usually support output redirection, which may be much simpler:

$sCommand='tracert 8.8.8.8'
$sOutputFile=@TempDir&'\tracert-output.txt'

RunWait(@ComSpec&' /c '&$sCommand&' > "'&$sOutputFile&'"','',@SW_HIDE)

ShellExecute($sOutputFile)
Edited by orbs

Share this post


Link to post
Share on other sites

This is what i tried initially, but I still had the problem of the program completing before the cmd.exe process was completed.  This morning I tried sleep between functions and that seemed to deal with the problem!  here is my code if you're interested

Global Const $fileName = "C:\filepath\" & @ComputerName & "_tracert.log"
Global Const $nerds = @ComputerName & "_tracert.log"
;Main Run Section
RunTraceRt()
sleep(120000)
SendFile()
Exit 0
Func RunTraceRt ()
    Local $time =   _Date_Time_GetLocalTime()
    Local $hOpenFile = FileOpen($fileName,$FO_CREATEPATH + $FO_OVERWRITE )
    FileWrite($hOpenFile, "### tracert log for " & @Computername & " " & _Date_Time_SystemTimeToDateTimeStr($time) & @CRLF)
    FileClose($hOpenFile)
    RunWait(@ComSpec&' /c '& 'tracert 8.8.8.8' &' > "'&$fileName&'"','',@SW_HIDE)
EndFunc
Func SendFile()
    Local $server = "**************"
    Local $port = "**"
    Local $userName = "********"
    Local $userPass = "********"
    Local $hOpen = _FTP_Open("My FTP Hole")
    Local $hConn = _FTP_Connect($hOpen, $server, $userName, $userPass, 1, $port)
    ;ballsack here
    $ftpPut = _FTP_FilePut($hConn, $filename, "/" & $nerds, $FTP_TRANSFER_TYPE_ASCII)
    Local $ftpC = _FTP_Close($hConn)
    Local $ftpO = _FTP_Close($hOpen)
EndFunc

Share this post


Link to post
Share on other sites

$PID = Run(@ComSpec&' /c '& 'tracert 8.8.8.8', @WorkingDir, @SW_HIDE, 8) ;8 = $STDERR_MERGED
While ProcessExists($PID)
    Sleep(10)
WEnd

$DATA = StdoutRead($PID)

;FileWrite("file.log", $DATA)
ConsoleWrite($DATA & @LF)


AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

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