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

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 192.168.70.254 > %officefile%
ping -n 10 192.168.71.254 > %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):
			pingfiles.append(file)

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
    openfile.close()

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.

Our Blog

Embracing AI in SMEs: 5 Key Steps for Successful Integration

Embracing AI in SMEs: 5 Key Steps for Successful Integration

Learn how SMEs can harness AI's power through leadership commitment, initial tool adoption, ethical…

How Schools Can Secure FCC’s $200 Million K-12 Cybersecurity Funding

How Schools Can Secure FCC’s $200 Million K-12 Cybersecurity Funding

Explore how educational institutions can effectively use the FCC's $200 million K-12 Cybersecurity Pilot…

CISOs: Why Investing in Security Tools Isn’t Enough for Effective Breach Detection

CISOs: Why Investing in Security Tools Isn’t Enough for Effective Breach Detection

Explore why CISOs' investments in security tools aren't translating to better breach detection. Learn…