-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtraceroute.rb
56 lines (44 loc) · 1.38 KB
/
traceroute.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# perform traceroute & put values in DB
require 'rubygems'
require 'sqlite3'
host= 'www.teksavvy.ca'
#
# open db, create if necessary
#
db= SQLite3::Database.new('ping_data.db')
begin
rows= db.execute("select * from results")
rescue => detail
#database not initialized
db.execute("CREATE TABLE results( time DATE PRIMARY KEY, hop1_ip TEXT, hop1_time REAL, hop2_ip TEXT, hop2_time REAL, hop3_ip TEXT, hop3_time REAL,hop4_ip TEXT, hop4_time REAL,hop5_ip TEXT, hop5_time REAL);")
end
#
# perform traceroute
#
output= `traceroute www.teksavvy.ca`
#split output into individual lines
lines= output.split("\n")
lines.each {|line|
#split line into fields
fields= line.split(" ")
#find field with ()
#count # of 'ms' - that's # of readings
#count # of * - that's # of missing readings
#both of above should = 3
#calc max and min time for each hop
hop= fields[0]
name= fields[1] #will not be there if no reverse DNS
ip_addr= fields[2]
time= fields[3]
#put fields into database
time= Time.now()
#if 1st row
# INSERT INTO results (time,hop1_ip,hop1_time) VALUES ( ,)
#if 2nd row
# UPDATE results SET hop2_time= $(), hop2_ip= ${} WHERE time = ${}
#if 3rd row
# UPDATE results SET hop3_time= $(), hop3_ip= ${} WHERE time = ${}
# UPDATE results SET hop4_time= $(), hop4_ip= ${} WHERE time = ${}
# UPDATE results SET hop5_time= $(), hop5_ip= ${} WHERE time = ${}
#save in database
}