Here’s a post I put out on my python blog. I figured it would be helpful for sysadmins and the like, so I figured I’d post it here as well.

A client of mine is looking to give access to their ERP application to their office and plant in Shanghai. They are going to do this via Citrix, so I wanted to see what latency was like. To do this, I just setup a batch file that runs pings to both locations and outputs the results to a text file. I scheduled this to run every 4 hours. Here’s the batch file.

@echo off
set officefile="C:TempShanghaiPingsping_office_%date:~4,2%-%date:~7,2%-%date:~10,4%_%time:~0,2%-%time:~3,2%.txt"
set plantfile="C:TempShanghaiPingsping_plant_%date:~4,2%-%date:~7,2%-%date:~10,4%_%time:~0,2%-%time:~3,2%.txt"
ping -n 10 > %officefile%
ping -n 10 > %plantfile%

After running for about a week, I wrote a small python script to grab all the ping times out of the text files and give me the maximum, minimum, and average response times. You are prompted for the location of the text files and the beginning pattern, so you can get the results for each site.

import os
import re

pingfiles = []
filepath = input('Please enter where files are stored:(Ex: C:files)  :   ')
filepath = filepath + ''
beginning = input('Please enter what each file starts with: (Ex: ping_plant)  :  ')
for root, dirs, files in os.walk(filepath):
	for file in files:
		if file.startswith(beginning):

pingtimes = []

for file in pingfiles:
    filename = filepath + file
    openfile = open(filename, 'r')
    lines = openfile.readlines()
    responsetimes = re.findall(r'bytes=32 time=(d+)ms', str(lines))
    pingtimes = pingtimes + responsetimes

pingtimes = [int(i) for i in pingtimes]
print("Maximum ping time is", max(pingtimes))
print("Minimum ping time is", min(pingtimes))
print("Average ping time is", round(sum(pingtimes) / len(pingtimes)),)

Here’s the output for one location:

Maximum ping time is 307
Minimum ping time is 237
Average ping time is 254

I know this is nothing special, but I figured I’d throw it out there in case any other newbies or sys admins need to get this information quickly without software.

via Down and dirty way to determine average latency using Python, text files, and a batch file | Playing with Python.