Last active
August 29, 2015 14:27
-
-
Save webrgp/89aed034eecff3fc3156 to your computer and use it in GitHub Desktop.
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
import sys | |
import subprocess | |
import re | |
# REGULAR EXPRESSIONS | |
# (?=foo) Lookahead | |
# (?<=foo) Lookbehind | |
# (?!foo) Negative Lookahead | |
# (?<!foo) Negative Lookbehind | |
host = "www.centos.com" | |
# ping: to see the total time along the route | |
ping = subprocess.Popen( | |
["ping", "-c", "10", host], | |
stdout = subprocess.PIPE, | |
stderr = subprocess.PIPE | |
) | |
pingtext, error = ping.communicate() | |
# matcher for speeds (min,avg,max,mdev) | |
matcher = re.compile("(\d+.\d+)/(\d+.\d+)/(\d+.\d+)/(\d+.\d+)") | |
speeds = matcher.search(pingtext).groups() | |
print speeds #print pingtext | |
# traceroute: to collect timing info along this route | |
traceroute = subprocess.Popen( | |
["traceroute", "-q", "5", host], | |
stdout = subprocess.PIPE, | |
stderr = subprocess.PIPE | |
) | |
tracetext, error = traceroute.communicate() | |
matcher = re.compile("(?<=\()\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}(?=\) )") | |
ips = matcher.findall(tracetext) | |
print ips #print tracetext | |
matcher = re.compile("\d{1,3}\.\d{1,3}(?= ms)") | |
times = matcher.findall(tracetext) | |
print times | |
matcher = re.compile("(?<= )[a-zA-Z0-9\-\.]+\.[a-zA-Z0-9]{1,5}(?= \()") | |
urls = matcher.findall(tracetext) | |
print urls |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is from Asher Martin, for the Cape Cod Web Development Meet Up group.