43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
# File 'lib/rtrace.rb', line 43
def self.run_loop
ttl = @options[:first_ttl]
while true do
break if ttl>@options[:max_ttl]
@send_socket.setsockopt(Socket::IPPROTO_IP, Socket::IP_TTL, [ttl].pack('i'))
t1 = Time.now
@send_socket.send("", 0, @dest_addr)
data = []
begin
timeout(@options[:timeout]) do
data = @recv_socket.recvfrom( 512 )[1]
end
rescue Timeout::Error
puts ttl.to_s.ljust(5)+"*"
ttl += 1
next
end
time = ((Time.now - t1)*1000).round 1
time = time.to_s+" ms"
ip = data.ip_address
loc = Geocoder.search(ip).first
if loc.city.empty?
place = ""
else
place = "#{loc.city}, #{loc.state}"
end
begin
name = Socket.getaddrinfo(ip, 0, Socket::AF_UNSPEC, Socket::SOCK_STREAM, nil, Socket::AI_CANONNAME, true)[0][2]
rescue
name = ""
end
s_ttl = ttl.to_s.ljust(5)
name = name.ljust(45)
s_ip = ip.ljust(20)
time = time.ljust(15)
place = place.ljust(25)
puts s_ttl+name+s_ip+time+place
ttl += 1
break if @dest_ip==ip
end
end
|