Skip to content

Instantly share code, notes, and snippets.

@sammy8806
Last active August 29, 2015 14:11
Show Gist options
  • Save sammy8806/7be6b1bad6c2b002b3c0 to your computer and use it in GitHub Desktop.
Save sammy8806/7be6b1bad6c2b002b3c0 to your computer and use it in GitHub Desktop.
Peerfinder implementation in Ruby to go cross-plattform
#!/usr/local/bin/ruby
# TODO: Fix ipv6
require 'net/ping'
#require_relative 'net-ping/lib/net/ping/icmp.rb'
require 'net/http'
require 'uri'
DEBUG = true
UUID = '00000000-0000-0000-0000-000000000000'
Peerfinder = 'http://peerfinder.polynome.dn42'
NB_PINGS = 5
Logfile = '/dev/stdout'
Family = 'ipv4'
#Family = 'any'
uri_get = URI(Peerfinder + '/target/' + UUID + '/' + Family)
response = Net::HTTP.get_response(uri_get)
if DEBUG == true
puts "--------------------------------\n"
puts "Response from Server (#{response.code})\n"
puts response.body
puts "--------------------------------\n"
end
if response.body.split.size != 2
puts "No action to perform"
exit
end
target = response.body.split[1]
target_id = response.body.split[0]
if DEBUG == 1
puts "Get new Job for \"#{target}\" (#{target_id})"
end
@icmp = Net::Ping::ICMP.new(target)
rtary = []
pingfails = 0
(1..NB_PINGS).each do
if @icmp.ping
rtary << @icmp.duration * 1000
puts "pong in #{@icmp.duration * 1000}"
else
pingfails += 1
puts "timeout"
end
end
avgrtt = rtary.inject(0) {|sum, i| sum + i}/(NB_PINGS - pingfails)
minrtt = rtary.min
maxrtt = rtary.max
if DEBUG == true
puts "Average round-trip is #{avgrtt}\n"
puts "Minimal round-trip is #{minrtt}\n"
puts "Maximal round-trip is #{maxrtt}\n"
puts "#{pingfails} packets were droped"
end
uri_post = URI(Peerfinder + '/result/report/' + UUID)
params = {
"target" => target_id,
"probes_sent" => NB_PINGS,
"probes_received" => rtary.size,
"minrtt" => minrtt,
"avgrtt" => avgrtt,
"maxrtt" => maxrtt
}
response = Net::HTTP.post_form( uri_post, params )
if DEBUG == true
puts "--------------------------------\n"
puts params
puts "Response from Server (#{response.code})\n"
puts response.body
puts "--------------------------------\n"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment