Jump to content

Capturing tracert output to log file


ALanclos
 Share

Recommended Posts

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
Link to comment
Share on other sites

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

Signature - my forum contributions:

Spoiler

UDF:

LFN - support for long file names (over 260 characters)

InputImpose - impose valid characters in an input control

TimeConvert - convert UTC to/from local time and/or reformat the string representation

AMF - accept multiple files from Windows Explorer context menu

DateDuration -  literal description of the difference between given dates

Apps:

Touch - set the "modified" timestamp of a file to current time

Show For Files - tray menu to show/hide files extensions, hidden & system files, and selection checkboxes

SPDiff - Single-Pane Text Diff

 

Link to comment
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
Link to comment
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.

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...