# Ruby script for parsing traceroute stream to JSON require 'rubygems' require 'open3' require 'json' #Array of top 25 US sites by Alexa rank (daily visits & pageviews) destinations = %w{ google.com facebook.com yahoo.com youtube.com amazon.com wikipedia.org twitter.com ebay.com craigslist.org blogspot.com live.com msn.com go.com bing.com aol.com linkedin.com espn.go.com myspace.com cnn.com wordpress.com netflix.com paypal.com imdb.com flickr.com microsoft.com } traceroutes = Hash.new() destinations.each do |destination| puts "\nTracing: #{destination}" cmd_string = "traceroute -m 25 -w 1 #{destination}" #25 hops, 1s timeout stdin, stdout, stderr = Open3.popen3(cmd_string) hop_count = 1 hop_array = [] stdout.each_with_index do |line, index| line_parse = line.delete(' ') if line_parse.include?('***') #If we've received * for all three packets hop_array[index]=["#{hop_count}","timeout"] p hop_array[index] hop_count+=1 else line.strip! hop_num = line[0...line.index(' ')] #1: hop number #Only the first line has a hop number #If we don't find a digit, the hop is returning from a different ip if hop_num =~ /\D/ hop_count-=1 hop_num = "#{hop_count}" line_parse = line hop_count+=1 else line_parse = line[hop_num.size,line.size] line_parse.strip! hop_count+=1 end hostname = line_parse[0...line_parse.index(' ')] #2: hostname #check for up to two asterisk responses at beginning of line (3 = complete timeout) if hostname =="*" hostname = line_parse[2...line_parse.index(' ', 2)] if hostname =="*" hostname = line_parse[4...line_parse.index(' ',4)] end end #if hostname contains alphabetical characters if hostname =~ /[a-z]/ #if it also contains periods if hostname.include?(".") == true hostname_array = hostname.split(".") else #not an address (device name on local?) hostname_array = [hostname] end #if we don't find alphabetical characters, we just have an ip else hostname_array = [hostname] end #offset for leading asterisks line_parse = line_parse[hostname.size+line_parse.index(hostname),line_parse.size] line_parse.strip! ip = line_parse[0, line_parse.index(' ')] line_parse = line_parse[ip.size, line_parse.size] line_parse.strip! if ip #trim parentheses from ip ip.delete!('(') ip.delete!(')') end line_parse.delete!('ms') line_parse.strip! time_array = line_parse.split(" ") hop_array[index]=[hop_num,hostname_array,ip,time_array] p hop_array[index] end traceroutes[destination]=hop_array end end json_doc = JSON.generate(traceroutes) puts "\nJSON:" puts json_doc time = Time.now.strftime("%y-%m-%d-%H%M%S") filename = "json/traceroute_#{time}.json" File.open(filename,"w") do |tracefile| tracefile.puts json_doc end puts "\nsaved to #{filename}"