Tuesday, December 2, 2014

Windows Batch File High Ping Datalogger

There comes a time in every internet user's life where they realize that their internet connection isn't all it is cracked up to be. For me, living out in rural mid-west USA, this is a constant struggle. I only have access to a singular phone and internet provider, and because I live more than three miles out from their one and only server center, I have to pay considerably more, and accept a far lower standard of internet service. It costs me roughly $90/mo for a 15mbit down / 1.5mbit up DSL line. bandwidth isn't usually the issue though, unless I need to upload a file, where if I exceed 125kbit bandwidth it swamps the line and terminates any downstream data streams. That can't be helped, it is a product of the hardware systems in place. The issue that plagues me most... is latency.

What do you do to check latency statistics? You run the ubiquitous Ping (ICMP Echo Request) diagnostic. Here, you select a target IP address, and your system will send a packet off to that address, tell you if it was delivered, and how much time it took to get there and back. You can go to just about any windows machine, open the command prompt, enter "ping" followed by a standard IP address, or even a domain name, and hit enter, to see the results of four packets being sent off. You can also run the Tracert or Trace Route ping utility, which sets the Time To Live (TTL - a limit of how many hops a single packet is allowed to make) to 1, which means you get a ping response from every single hop along the route consecutively.

Both tools are indispensable in network troubleshooting, but by the very nature of tracert it can only be used as a spot-check tool. You run it when you think there is a problem, or when you know there isn't one, and then compare the results. You can run a very light weight single ping anytime you want, and you can even make a batch file to run it continuously, once every set amount of time. This helps to keep connections that are prone to failure or timeout alive. It is simple to do, simply use the option "-n #" after the ip address, remember to remove the quotations, and replace the # with a number. Running continuous pings can be done with the "-t" option, but this can easily contribute to network congestion, both in your LAN and at the destination server. Some servers will even consider this a form of attack and block off communications with your IP address, not good, and thus not advised. You can get around this by adding a delay and automating the ping process in a batch file. A simple GOTO command and a TIMEOUT delay command are all that are needed.

Here's where the fun starts....

What if you want to record the results of the ping tests to catch intermittent latency issues. You probably want to run the ping commands relatively often, perhaps once every second or two. It's trivial to output the results from Ping.exe to a text file using the append operator ">>". Wait though, this is going to make a HUGE file that no IT professional is going to want to look over. Well, you could write up a text file parsing script to extract only the unusual ping results and place them in a new file. That would solve it. but now you're still eating system resources by continuously writing to a text file on your hard drive. Also, you now need to periodically run the parsing script to trim down the text file. Not very user friendly, and certainly not elegant.

The Programming Considerations.

You have a few choices.

1) You fire up your favorite language's IDE and start writing a from scratch program that will execute the functions that Ping uses and processes the resultant data and logs it accordingly. This will be tens of hours of coding, at the very least. If you take a look at Ping.c or Ping.cpp for example, they're rather huge works of programming.
2) You fire up your favorite language's IDE and write a from scratch program that will call on Ping.exe to do its thing and then grab the results. You'll have to deal with advanced systems like windows sockets, pipes, and all kinds of nasties.
3) You throw in the towel and find some end user program that probably isn't free that will datalog ping results, and hope it is good enough. Yeah, I'm not the kind to do that either.
4) You start learning a scripting language like Perl of Python to do what options 1 and 2 would but far easier. Yeah, still not going to do that.
5) Jump into the deep end of windows batch scripting and try to trudge through the muck of a poorly explained, foreign looking language with rather inaccessible documentation because you KNOW it can be done, and the results are likely to be the easiest to implement. Guess that's what we have to do.

The Windows Batch (.bat) File

Basically, batch files are a script of commands to be executed autonomously by the windows command line interface once the batch file is executed. Any command that can go into a CLI can go in a batch file, though you will usually need to modify the syntax and structure some.

Thankfully there is a website called SS64.com which is run by Simon Sheppard of the UK. This is a truly valuable resource when it comes to windows CLI and Batch files. If you have any experience with programming, especially in C/C++, this site will get you going writing useful and efficient batch files as long as you carefully read the information presented and apply some elbow grease.

The hardest part will be recognising the required syntax and wrapping your head around the fact that you don't have the structure to fall back on that most languages have. You don't declare/define variables at the top, and there are many hidden limitations when working with variables and data input/output. Getting things right in terms of structure and syntax was the most difficult part by far. Notepad makes a pretty piss poor programming IDE, haha. I know at least 90 minutes were swallowed up by forgetting to wrap a variable in % signs and having a line break after a DO command that it didn't like.

My Batch File

The heart of this 'program' is fourfold;
1) Call and save the date and time in an appropriate format to variables.
2) Ping the supplied IP address.
3) Find the relevant data in the Ping.exe output, save it to a variable,
4) Process the variables' text strings into usable chunks and then perform an IF THEN conditional check to discard all of the useless data points. Export the saved data to a log file.

Written within comments (lines preceeded by double colons) is the syntax and command structure basics, as well as links to their pages on SS64.com At the top of the file is my generic header that explains what the program is and what it does, as well as how to contact me. It goes on to explain what to change in what lines to repurpose this program for your own needs. I've gone ahead and generalized the output file names and filepaths, as well as the target IP address so that this program will just simply run on just about any modern windows machine. Feel free to use it as you like. To use, simply rename/SaveAs the file as a .bat instead of a .txt file in any text editor. If you have file extensions showing enabled you can just right click and rename as well. Note that with the default settings for the output file (C:\ directory) you might need to run the batch file as an administrator (right click, run as admin), depending on your operating system and account privileges. Simply change the directory to another place where you can easily write files to if this is an issue.


P.S. for what it is worth; my internet connection is far less stable than I thought it was. I am getting many spikes above 500ms roughly every hour, throughout all hours of the day and night. Less frequently there are spikes above 1 second. There are spikes between 200ms and 400ms roughly every couple of minutes. The "normal" ping time is 58ms for reference.