Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import subprocess
- import datetime
- import time
- import re
- import sys
- toolbar_width = 40
- while True:
- try:
- output = subprocess.check_output(["ping", "-c 50", "-i 0.05", "google.ca"])
- except subprocess.CalledProcessError as e:
- if e.returncode == 2:
- # no responses received
- output = e.output
- else:
- print "error code " + str(e.returncode)
- print e.output
- if e.returncode == 68:
- time.sleep(2.5) # avoid flood of failed DNS requests
- continue
- match = re.search("([0-9]?[0-9]?[0-9]\.[0-9])% packet loss", output)
- if match:
- data = "{0}, {1}%".format(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"), match.group(1))
- loss = float(match.group(1))/100
- dots = int(round(loss * toolbar_width))
- spaces = toolbar_width - dots
- data = "{0},{1}[{2}{3}]".format(data, (" " * (5 - len(match.group(1)))), ("#" * dots), (" " * spaces))
- if loss > 1.18:
- print data,
- print '\a'
- else:
- print data
- with open("packet_loss_data.csv", "a") as outfile:
- outfile.write(data)
- outfile.write('\n')
- outfile.flush()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement