Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: ping an address, log errors to text file with timestamp  (Read 187222 times)

0 Members and 2 Guests are viewing this topic.

foxidrive



    Specialist
  • Thanked: 268
  • Experience: Experienced
  • OS: Windows 8
Re: ping an address, log errors to text file with timestamp
« Reply #15 on: August 29, 2012, 02:00:40 AM »
Do you want every line or just "request timed out" or what exactly?

denywinarto



    Newbie

    • Experience: Beginner
    • OS: Unknown
    Re: ping an address, log errors to text file with timestamp
    « Reply #16 on: August 29, 2012, 05:30:55 AM »
    Do you want every line or just "request timed out" or what exactly?

    Just "request timed out"
    It would be great if there's a total amount of RTO's at the end, but if not it's ok

    foxidrive



      Specialist
    • Thanked: 268
    • Experience: Experienced
    • OS: Windows 8
    Re: ping an address, log errors to text file with timestamp
    « Reply #17 on: August 29, 2012, 06:04:03 AM »
    Try this modified VBS code from above.

    Save it as pinglog.vbs

    Code: [Select]
    hostIp      = wscript.arguments(0)
    logfilename = wscript.arguments(1)
    Set fso     = CreateObject("Scripting.FileSystemObject")
    Set Shell   = CreateObject("Wscript.Shell")
    ' OpenTextFile Method requires a Const value
    ' (Over)Write = 2  Append = 8   
    Set logfile = fso.OpenTextFile(logfilename, 8, True)
    shellstring = "%comspec% /c ping -t " & hostIP
    Set oExec   = Shell.Exec(shellstring)
    wscript.echo "Ping Error log With Timestamp - Ctrl + C to halt"
    Do While oExec.StdOut.AtEndOfStream <> True
          pingline = Date & " " & Time & " " & oExec.StdOut.ReadLine
          If InStr(pingline, "timed out") > 0 Then
             logfile.WriteLine(pingline)
          End If
    Loop


    And save this as pinglog.bat

    Code: [Select]
    @echo off
    del pinglog.txt 2>nul
    cscript /nologo pinglog.vbs "%~1" pinglog.txt
    echo found this many timed out responses
    find /c /i "timed out" <pinglog.txt
    pause


    and execute it like this:  pinglog.bat www.telstra.com


    Remove this line from the bat file if you want a permanent log, but the count will be of the entire file.

    del pinglog.txt 2>nul

    songa



      Newbie

      • Experience: Experienced
      • OS: Windows 7
      Re: ping an address, log errors to text file with timestamp
      « Reply #18 on: January 05, 2013, 07:56:10 AM »

      hostIp      = wscript.arguments(0)
      logfilename = wscript.arguments(1)
      Set fso     = CreateObject("Scripting.FileSystemObject")
      Set Shell   = CreateObject("Wscript.Shell")
      ' OpenTextFile Method requires a Const value
      ' (Over)Write = 2  Append = 8   
      Set logfile = fso.OpenTextFile(logfilename, 8, True)
      shellstring = "%comspec% /c ping -t " & hostIP
      Set oExec   = Shell.Exec(shellstring)
      wscript.echo "Ping Error log With Timestamp - Ctrl + C to halt"
      Do While oExec.StdOut.AtEndOfStream <> True
            pingline = Date & " " & Time & " " & oExec.StdOut.ReadLine
            If InStr(pingline, "TTL=") = 0 Then
               logfile.WriteLine(pingline)
            End If
      Loop


      Hello Salmon Trout, its a nice script!!

      Could you increase a code to add every one (or N) hour(s) in a log-file, a successful ping with address.
      example:

      05/01/2013 10:28:07 Reply from 74.125.234.39: bytes=32 time=41ms TTL=55
      05/01/2013 11:28:07 Reply from 74.125.234.39: bytes=32 time=39ms TTL=55
      etc...

      I have a doubt:

      I have used www.google.com as a reference...

        lping.vbs www.google.com lping.log

      If the Google IP change, your script will think that it is off-line?
      I'm not sure if in your code, gets the Google IP at the start and always keeps the same!
      It always checks the Google IP inside the loop or not?
      Sorry-me about this, but Im not very familiar with VBScript!

      and thank you for the script!!  :)

      foxidrive



        Specialist
      • Thanked: 268
      • Experience: Experienced
      • OS: Windows 8
      Re: ping an address, log errors to text file with timestamp
      « Reply #19 on: January 05, 2013, 09:32:48 AM »
      This will use ping for each invocation and it will do a fresh DNS lookup for each ping,
      and should only write the failed lines,
      and will write a line to the log like ' DATE TIME - pinging "www.google.com" ' every so often.

      The frequency of the above log update (when no failure events are present) is determined by the 3600 in the for /L line.
      On my machine with Windows 8 then the 3600 equates to around 4 minutes.

      It just occurs to me that although this will flush the local DNS cache, your broadband modem also maintains a DNS cache and it will provide the stale DNS info for the URL.

      This doesn't work out of the box for XP (you will require skip=3 and the log will double space), and I didn't check vista or Win 7.


      Code: [Select]
      @echo off
      if "%~1"=="" (
      echo Enter an IP address or domain name
      echo Example Syntax: %0 www.google.com
      pause
      goto :EOF
      )
      setlocal enabledelayedexpansion
      :loop
      >> pinglog.txt echo !date! !time! - pinging "%~1"
      for /L %%a in (1,1,3600) do (
      ipconfig /flushdns >nul
      for /F "skip=2 delims=" %%b in ('ping -n 1 "%~1"') do (
      if not defined var (
      echo "%%b"|find "TTL=">nul ||(>> pinglog.txt echo !date! !time! - %%b)
      )
      set var=1
      )
      set "var="
      )
      goto :loop
      « Last Edit: January 05, 2013, 09:45:19 AM by foxidrive »

      mikiedba



        Newbie

        • Experience: Experienced
        • OS: Windows 7
        Re: ping an address, log errors to text file with timestamp
        « Reply #20 on: November 20, 2014, 06:39:02 PM »
        Thank you for posts. Here's a variation of just the ping test.

        pingtest.vbs
        Code: [Select]
        ' Ping once every 5 minutes
        ' Timeout after 1 second
        ' Write TTL ping line with timestamp
        ' Write request timeout with timestamp

        hostIp      = wscript.arguments(0)
        logfilename = wscript.arguments(1)

        Set fso     = CreateObject("Scripting.FileSystemObject")
        Set Shell   = CreateObject("Wscript.Shell")

        ' OpenTextFile Method requires a Const value
        ' (Over)Write = 2  Append = 8   

        Set logfile = fso.OpenTextFile(logfilename, 8, True)
        shellstring = "%comspec% /c ping -n 1 -w 1000 " & hostIP

        wscript.echo "pingtest to logfile - Ctrl + C to halt"

        Do While True
            Set oExec   = Shell.Exec(shellstring)
            Do
        pingline = Date & " " & Time & " " & oExec.StdOut.ReadLine
        If InStr(pingline, "TTL=") <> 0 Then
        logfile.WriteLine(pingline)
        End If
        If InStr(pingline, "Request timed out.") <> 0 Then
        logfile.WriteLine(pingline)
        End If
        Loop While Not oExec.Stdout.atEndOfStream
        ' WScript.Sleep(1000)
        WScript.Sleep(1000 * 60 * 5)
        Loop


        Example run:
        cscript pingtest.vbs 10.11.12.13  pingtest.log

        Example pingtest.log: (1 second sleep)
        21/11/2014 9:31:30 AM Reply from 10.11.12.13: bytes=32 time=6ms TTL=121
        21/11/2014 9:31:31 AM Reply from 10.11.12.13: bytes=32 time=6ms TTL=121
        21/11/2014 9:31:32 AM Reply from 10.11.12.13: bytes=32 time=6ms TTL=121
        21/11/2014 9:31:33 AM Reply from 10.11.12.13: bytes=32 time=7ms TTL=121
        21/11/2014 9:31:35 AM Reply from 10.11.12.13: bytes=32 time=10ms TTL=121
        21/11/2014 9:31:54 AM Request timed out.
        21/11/2014 9:31:56 AM Request timed out.
        21/11/2014 9:31:58 AM Request timed out.
        21/11/2014 9:32:00 AM Request timed out.
        21/11/2014 9:32:02 AM Request timed out.
        21/11/2014 9:32:04 AM Request timed out.

        thanks again, mikie

        Squashman



          Specialist
        • Thanked: 134
        • Experience: Experienced
        • OS: Other
        Re: ping an address, log errors to text file with timestamp
        « Reply #21 on: November 20, 2014, 07:31:01 PM »
        That is a big variation.

        Dzzope



          Starter

          • Experience: Experienced
          • OS: Windows 7
          Re: ping an address, log errors to text file with timestamp
          « Reply #22 on: December 03, 2014, 10:58:59 AM »
          Hi guys,

          I'm a total noob when it comes to windows and scripts,
          Trying to sort a connection issue and would love to simply log changes in the status (either "reply from" or "request timed out") with a timestamp.

          Can anyone help? (also with what to run / save it as) as I said, widows script noob

          Thanks in advance.

          foxidrive



            Specialist
          • Thanked: 268
          • Experience: Experienced
          • OS: Windows 8
          Re: ping an address, log errors to text file with timestamp
          « Reply #23 on: December 03, 2014, 07:13:28 PM »
          Trying to sort a connection issue and would love to simply log changes in the status (either "reply from" or "request timed out") with a timestamp.

          It would help for you to show the sort of thing you need to get in the log file.

          Quote
          Can anyone help? (also with what to run / save it as) as I said, widows script noob

          Have you ever used batch files in the past?

          Dzzope



            Starter

            • Experience: Experienced
            • OS: Windows 7
            Re: ping an address, log errors to text file with timestamp
            « Reply #24 on: December 04, 2014, 05:10:20 PM »
            Just timestamp with ping reply/timeout but only log the first instance of each as it changes. That way I can see and show the connection drops and for how long without a huge log.
            If it's not easy to do, then I can do it manually. Some advice on the steps to take to save & run whichever script above will simply log with timestamps would be great though.

            Nope. Nada on scripts in windows ever.

            Squashman



              Specialist
            • Thanked: 134
            • Experience: Experienced
            • OS: Other
            Re: ping an address, log errors to text file with timestamp
            « Reply #25 on: December 04, 2014, 06:03:12 PM »
            I think Foxidrive was asking for a concrete example of the output you want to see in your log file.

            Dzzope



              Starter

              • Experience: Experienced
              • OS: Windows 7
              Re: ping an address, log errors to text file with timestamp
              « Reply #26 on: December 05, 2014, 12:41:31 PM »
              Reply from 192.168.208.1: bytes = 32 time1ms TTL = 255
              Request timed out.
              Reply from 192.168.208.1: bytes = 32 time1ms TTL = 255
              Request timed out.
              Etc, with timestamps..

              ie the changes in status, I don't need every reply, just the first as connection drops and is regained to easily show when each connection issue starts and stops.

              Squashman



                Specialist
              • Thanked: 134
              • Experience: Experienced
              • OS: Other
              Re: ping an address, log errors to text file with timestamp
              « Reply #27 on: December 05, 2014, 12:55:29 PM »
              I think you would be better off with a dedicated program to do this.
              When I was a Network admin this is just one of the simple tools I would use.
              http://www.snapfiles.com/get/mhostalive.html
              http://www.snapfiles.com/get/angryip.html