Created
June 3, 2015 08:18
-
-
Save jamesbee/4d174b5d8f68a46be1c6 to your computer and use it in GitHub Desktop.
ping urls
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PingPool | |
def initialize(count) | |
@count = count | |
DATA.each_line do |line| | |
line = line.strip | |
next if line.start_with? '#' | |
next if line == '' | |
if line.index("#(") | |
self.batch /#(\(.*\))/, line | |
elsif line.index("#[") | |
self.batch /#(\[.*\])/, line | |
else | |
self.run line | |
end | |
end | |
end | |
def batch(reg, line) | |
list = eval reg.match(line)[1] | |
list.each do |idx| | |
url = $` + idx.to_s + $' | |
self.run url | |
end | |
end | |
def run(url) | |
puts "Pinging #{url} #{@count} times." | |
res = `ping -c #{@count} #{url}` | |
if res == "" | |
puts "Unknown host\n" | |
return | |
end | |
res = res.split("\n") | |
self.pretty(res) | |
puts '' | |
end | |
def pretty(res) | |
puts res[-2].split(',')[2].strip | |
puts ("avg=#{res[-1].split(' ')[3].split('/')[1]} ms") | |
end | |
end | |
if $0 == __FILE__ | |
count = 10 | |
if ARGV[0] | |
count = ARGV[0].to_i | |
count = 10 if count == 0 | |
end | |
PingPool.new(count) | |
end | |
__END__ | |
# this is one line comment | |
# ping urls in file data, default 10 times, usage: | |
# ./this_file [count] | |
# count is default set to 10, here is an example: | |
# $ ruby this_file 4 | |
# ping normal.com | |
# outs puts looks like: | |
# Pinging normal.com 10 times. | |
# 10.0% packet loss | |
# avg=36.251 ms | |
normal.com | |
# this equals ping | |
# some1.url.com | |
# some2.url.com | |
# some3.url.com | |
some#(1..3).url.com | |
# this equals ping | |
# other4.url.com | |
# otherd.url.com | |
other#[4,'d'].url.com |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment