Skip to content

Instantly share code, notes, and snippets.

@webrgp
Last active August 29, 2015 14:27
Show Gist options
  • Save webrgp/89aed034eecff3fc3156 to your computer and use it in GitHub Desktop.
Save webrgp/89aed034eecff3fc3156 to your computer and use it in GitHub Desktop.
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
@webrgp
Copy link
Author

webrgp commented Aug 22, 2015

This is from Asher Martin, for the Cape Cod Web Development Meet Up group.

@htmlfarmer
Copy link

:)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment