Created
March 4, 2011 11:01
-
-
Save tshanks/854468 to your computer and use it in GitHub Desktop.
quick script that automates scraping online ping tools and graphs the results (see comment below)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# Copyright (c) 2009 Thomas Shanks and the Georgia Institute of Technology | |
# Licensed under an MIT-Style (like BSD-style) free-software license; see complete notice at end. | |
# | |
# This script emulates the process described in Savage (et al.)'s IEEE Micro article | |
# "Detour: Informed Internet Routing and Transport" at http://doi.ieeecomputersociety.org/10.1109/40.748796 . | |
import os | |
from urlparse import urlparse | |
import re | |
import socket | |
import numpy | |
import csv | |
import Gnuplot | |
URLs = open('URLs', "r").readlines() | |
URLinfos = [] | |
for aURL in URLs: | |
URLinfos.append((aURL.rstrip('\n'), socket.gethostbyname(urlparse(aURL).netloc))) | |
print URLinfos | |
N = 15 #If they didn't take so long to run, this would be len(URLs). Instead, put the number of servers to run. | |
PingTimes = numpy.zeros((N, N), dtype=float) | |
for pingto in xrange(0, N): | |
for pingfrom in xrange(0, N): | |
if pingfrom == pingto: | |
continue | |
response = os.popen("lynx -dump " + URLinfos[pingfrom][0] + URLinfos[pingto][1]).readlines() | |
#print response | |
avg=0 | |
for line in response: | |
linepingtimes = re.findall('[0-9]{1,3}[.]?[0-9]{1,3} ?ms', line) | |
linesum = 0 | |
for apingtime in linepingtimes: | |
linesum += float(apingtime.rstrip('ms')) | |
if linesum > 0: | |
avg = linesum / len(linepingtimes) | |
if avg != 0: | |
PingTimes[pingfrom][pingto] = avg | |
print pingfrom, " to ", pingto, " time ", avg | |
fileforcsv = open('pingtimesout.csv', 'w') | |
csvout = csv.writer(fileforcsv) | |
for row in PingTimes: | |
csvout.writerow(row) | |
print row | |
fileforcsv.close() | |
DetourTimes = numpy.zeros((N, N), dtype=float) | |
scatterplot = [] | |
for pingto in xrange(0, N): | |
for pingfrom in xrange(0, N): | |
detourtime = 10000 #huge | |
for pingthrough in xrange(0, N): | |
if (pingto == pingfrom) or (pingthrough == pingfrom) or (pingthrough == pingto) or (PingTimes[pingfrom][pingthrough] == 0) or (PingTimes[pingthrough][pingto] == 0): | |
continue | |
thistime = PingTimes[pingfrom][pingthrough] + PingTimes[pingthrough][pingto] | |
if (thistime < detourtime): | |
print "Better detour: ", pingfrom, " through ", pingthrough, " to ", pingto , " time ", thistime, " better than ", detourtime | |
detourtime = thistime | |
if (detourtime < 10000) and (detourtime > 0): | |
DetourTimes[pingfrom][pingto] = detourtime | |
scatterplot.append(PingTimes[pingfrom][pingto]/DetourTimes[pingfrom][pingto]) | |
fileforcsv = open('detourtimesout.csv', 'w') | |
csvout = csv.writer(fileforcsv) | |
print "\ndetour\n" | |
for row in DetourTimes: | |
csvout.writerow(row) | |
print row, ' ' | |
fileforcsv.close() | |
print "\nscatter\n" | |
scatterplot.sort(reverse=True) | |
xaxis = numpy.linspace(0, 1, len(scatterplot)) | |
outfile = open('scatterplotpoints', 'w') | |
for index in xrange(0, len(scatterplot)): | |
outfile.write(`xaxis[index] `+ ' '+`scatterplot[index]`+'\n' ) | |
print `scatterplot[index]`+ ' ' | |
outfile.close() | |
gp = Gnuplot.Gnuplot(persist = 1) | |
gp('set terminal png') | |
gp('set output "detour-traceroute-inquiry-output.png"') | |
gp(' set xlabel "Fraction of paths measured"') | |
gp(' set ylabel "Ratio of default-route latency to alternate-route latency"') | |
gp("plot 'scatterplotpoints'") | |
#Copyright (c) 2009 Thomas Shanks and the Georgia Institute of Technology | |
# | |
#Permission is hereby granted, free of charge, to any person | |
#obtaining a copy of this software and associated documentation | |
#files (the "Software"), to deal in the Software without | |
#restriction, including without limitation the rights to use, | |
#copy, modify, merge, publish, distribute, sublicense, and/or sell | |
#copies of the Software, and to permit persons to whom the | |
#Software is furnished to do so, subject to the following | |
#conditions: | |
# | |
#The above copyright notice and this permission notice shall be | |
#included in all copies or substantial portions of the Software. | |
# | |
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
#EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES | |
#OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |
#NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT | |
#HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | |
#WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | |
#FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | |
#OTHER DEALINGS IN THE SOFTWARE. | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The assignment was specifically to use these online ping tools to estimate the efficacy (albeit inaccurately, as ping RTT doesn't equal TCP RTT) of using detour routing vs the status quo. I decided to automate the process.
I put it here as a programming example for someone. It's no work of art; it's just a quick script automating some web scraping of web-based ping tools.