Kroko Just another WordPress weblog

February 8, 2009

Script to Monitor Your Internet Connection

Filed under: fuck windows — admin @ 5:00 am

this text is from: http://blog.akinstech.com/script-to-monitor-your-internet-connection

Have you ever had a situation where your Internet connection drops for a few seconds, then comes back, then drops again, then comes back, and repeats randomly with no rhyme or reason? Getting your ISP to see the problem can be a real hassle as Murphy’s Law dictates that the problem will ALWAYS go away once you finally get them on the phone. You’ll usually get the standard “looks good on our end” with no actual resolution of the problem. Well, this script will allow you to monitor the line for hours, days, even weeks, and provide you with a timestamped activity log of exactly what was happening over that period. Armed with this type of information, you can usually get your ISP to take notice.

I actually wrote this script a few years ago when one of my clients was having these very problems. Using the log it generates, I was able to provide the ISP with meaningful numbers and exact times, and eventually they were able to correct the problem. Since then, I’ve used it many times to check the stability of a connection. Anyway, today on one of the tech forums, somebody asked how to monitor a flakey line. It got me thinking about this script, so I dusted it off and posted it to the forum. I figured while I was at it, I’d post it here as well.

 

I wrote it in VBScript, and it utilizes the WMI Ping object. Basically it sends a single ping to a host you specify, logs the outcome with a time-stamp, then waits a predetermined delay time and repeats itself indefinitely. If you let this script run for a full day or more, pinging every few seconds, it will give you a really good idea what might be happening with your Internet connection.

The script contains four values that you’ll need to edit:

strHost = “someserver.samplehost.com” – This is the host you want to ping. It can be a web server, a router, a DNS server, etc. You can specify a DNS name or an IP address.
nDelay = 15 – This is how many seconds the script will wait between pings. I find that pinging every 15 seconds is usually sufficient, although you may want to ping as often as every second in some cases.
strLogFile = “C:\Comcastpinglog.txt” – This is the full path to the log you want to create.
nThreshold = 75 – This is the response threshold (in milliseconds). Any ping response which is greater than this will be flagged as “SLOW” and marked as such in the log. This makes it easy to filter these log entries later. 75ms is rather high for most broadband connections at idle. What you’ll want to do is to let the script run for a bit and establish a baseline for what “normal” is. For instance, my Comcast cable connection (3-6Mbit speed) usually pings at about 19-32ms.

What should I ping?
If you’re having trouble with your Internet connection, a good host to ping will be the default gateway provided by your ISP. This would represent the first physical device on the “other side” of your connection. You can get this by checking your router’s “Connection Status” or “WAN Status” screen and noting the IP address for default gateway. Another good one would be one of your ISP’s DNS servers. This is also available in your router. With either of these, you’re pinging something fairly local to your ISP, eliminating additional hops.

If you’re having trouble with your hosted web site or server, you’ll want to ping that server or the IP assigned to you by your provider. Just keep in mind that you will be traversing many hops to get there, so it isn’t uncommon for your provider to pass the blame. If you notice periods of high latency, try to back them up with a tracert output so you can show your provider where slowdown is.

* Note: Please be advised that some ISP’s or network admins may not appreciate you performing continuous pings to their servers. I am not responsible for any nasty emails you might receive. Make sure you know what you’re doing before you go pinging somebody’s else’s servers.

Logfile Output
The script continuously appends to a log file with the following output:

================================================================================
1/26/2008 1:42:16 PM: PINGING HOST: [www.yahoo.com]
================================================================================
1/26/2008 1:42:16 PM: Reply from 209.131.36.158: bytes=32 time=19ms TTL=51
1/26/2008 1:42:21 PM: Reply from 209.131.36.158: bytes=32 time=20ms TTL=52
1/26/2008 1:42:26 PM: Ping Failed
1/26/2008 1:42:31 PM: Ping Failed
1/26/2008 1:42:36 PM: Ping Failed
1/26/2008 1:42:41 PM: Reply from 209.131.36.158: bytes=32 time=39ms TTL=51 SLOW
1/26/2008 1:42:46 PM: Reply from 209.131.36.158: bytes=32 time=23ms TTL=51
1/26/2008 1:42:51 PM: Reply from 209.131.36.158: bytes=32 time=31ms TTL=51 SLOW

The log is appended infinitely, so you’ll eventually want to purge the log or copy it off to avoid it growing to an unmanageable size.

You may later filter specific lines from the log using the following DOS commands:

type logfile.txt | find /I "Failed" > failed.txt
type logfile.txt | find /I "SLOW" > slow.txt

Running the Script
Copy the VBScript code below and paste into a VBS file in your scripts directory. Call it PingTest.vbs. To run the script, launch it with the command “cscript PingTest.vbs”. To kill the script, just press CTRL-C. Note: Do NOT just double-click the script to run it! That will cause it to launch with the wscript engine. Since this script outputs to the console it needs to run with the cscript engine.

To make things easier, and ensure you always launch it with cscript, just write a simple 2-line batch file to launch the script. (Put it in the same directory as the vbs script):

PingTest.bat

@echo off
cscript %~dp0PingTest.vbs

PingTest.vbs

Dim strHost, nDelay, strLogFile, nThreshold

' EDIT THESE VARIABLES TO SUIT YOUR APPLICATION
strHost = "someserver.samplehost.com" ' What do you want to ping
nDelay = 15  ' How many seconds to pause between pings
strLogFile = "C:\Comcastpinglog.txt" ' Full Name and Path of logfile
nThreshold = 75 ' Anything response over this (in ms) will be flagged as "Slow"

Dim txtLogLine, objPing, objRetStatus, rc

rc = LogLine(strLogFile,"================================================================================")
rc = LogLine(strLogFile,FormatDateTime(Now(),0) & ": PINGING HOST: [" & strHost & "]")
rc = LogLine(strLogFile,"================================================================================")

Do While True
   txtLogLine = ""

   txtLogLine = txtLogLine & FormatDateTime(Now(),0) & ": "
   Set objPing = GetObject("winmgmts:{impersonationLevel=impersonate}").ExecQuery _
       ("select * from Win32_PingStatus where address = '" & strHost & "'")

   For each objRetStatus in objPing
       If IsNull(objRetStatus.StatusCode) or objRetStatus.StatusCode<>0 then
          Ping = False
          txtLogLine = txtLogLine & "Ping Failed"
       Else
          Ping = True
	  txtLogLine = txtLogLine & "Reply from " & objRetStatus.ProtocolAddress & ": "
	  txtLogLine = txtLogLine & "bytes=" & objRetStatus.BufferSize & " "
          txtLogLine = txtLogLine & "time=" & objRetStatus.ResponseTime & "ms "
          txtLogLine = txtLogLine & "TTL=" & objRetStatus.ResponseTimeToLive
	  If objRetStatus.ResponseTime >= nThreshold Then
	     txtLogLine = txtLogLine & " SLOW"
	  End If
       End If
   Next
   WScript.Echo txtLogLine
   rc = LogLine(strLogFile, txtLogLine)
   WScript.Sleep nDelay * 1000
Loop

Function LogLine(strFile, strLine)
   Dim objFSO, objFile
   Set objFSO = CreateObject("Scripting.FileSystemObject")
   Set objFile = objFSO.OpenTextFile(strFile,8,True)
   objFile.WriteLine(strLine)
   objFile.Close
End Function

While this isn’t necessarily the most advanced method of detecting a slow connection, it’s surprisingly accurate in exposing periods of high latency or dropped connection. Try it out and let me know what you think.

No Comments »

No comments yet.

RSS feed for comments on this post.

Leave a comment

Powered by WordPress