Timeout Error in pingecho

hi.

i wrote a little script . (Mass IP Pinger)
But I got an error :-s

Error :

/usr/lib/ruby/1.8/timeout.rb:60:in timeout': execution expired from /usr/lib/ruby/1.8/ping.rb:46:inpingecho’
from a.rb:27
from a.rb:26:in `each’
from a.rb:26

and my script :

#!/usr/bin/env ruby

#Mass IP Pinger By MagicCoder ([email protected])

www.magiccoder.ir

#-f = File
#-t = Time Out #Defualt = 0.5
#-p = Port #Defualt = 80

#sample : ruby magic.rb -f ip.txt -t 1 -p 22
#sample : ruby magic.rb -f ip.txt

require ‘ping’

time_out = 0.5 #Defualt
port = 80 #Defualt

length = 0
while (length < ARGV.length)
file = ARGV[length+1] if ARGV[length] == ‘-f’
time_out = ARGV[length+1] if ARGV[length] == ‘-t’
port = ARGV[length+1] if ARGV[length] == ‘-p’
length += 1
end

if (file != nil)
File.open(file).each do |line|
result = Ping.pingecho(line.scan(/\d+.\d+.\d+.\d+/).to_s,
time_out.to_f , port.to_i)
if(result == true )
puts line.scan(/\d+.\d+.\d+.\d+/).to_s+"\t\t\tIs Up"
else
puts line.scan(/\d+.\d+.\d+.\d+/).to_s+"\t\t\tIs Down"
end
end
puts “Finish …”
else
puts “\n\nMass IP Pinger By MagicCoder
([email protected])\n\t\twww.magiccoder.ir”
puts “\n-f = File\n-t = Time Out\t#Defualt = 0.5\n-p =
Port\t#Defualt = 80”
puts “\nsample : ruby magic.rb -f ip.txt -t 1 -p 22”
puts “sample : ruby magic.rb -f ip.txt\n\n”
end

sh$ ruby magic.rb -f list.txt -t 0.5 -p 22
for sample Attached list

Thank you .

Sven S. wrote:

Hmmm, what platform are you running it from? Just ran in from Mac OS
10.5.8
with Ruby 1.8.7 and it ran fine (ruby 1.8.7 (2009-06-12 patchlevel 174)
[i686-darwin9.7.0]).

Maybe a tcp stack implementation difference between platforms? I did
notice
you had some addresses that have colons in them (e.g. 123456:201.92.93).
Still handled fine on my machine but maybe not on others. Anyone else?

My Platform Linux and FreeBSD.
ruby 1.8.7 (2010-01-10 patchlevel 249).

Hmmm, what platform are you running it from? Just ran in from Mac OS
10.5.8
with Ruby 1.8.7 and it ran fine (ruby 1.8.7 (2009-06-12 patchlevel 174)
[i686-darwin9.7.0]).

Maybe a tcp stack implementation difference between platforms? I did
notice
you had some addresses that have colons in them (e.g. 123456:201.92.93).
Still handled fine on my machine but maybe not on others. Anyone else?

Sajjad Po wrote:

/usr/lib/ruby/1.8/timeout.rb:60:in timeout': execution expired from /usr/lib/ruby/1.8/ping.rb:46:inpingecho’
from a.rb:27
from a.rb:26:in `each’
from a.rb:26

What if you just run it from irb? I get the following under Linux:

$ irb --simple-prompt

require ‘ping’
=> true

Ping.pingecho(‘1.1.1.1’, 2, 80)
=> false

RUBY_VERSION
=> “1.8.7”

RUBY_PATCHLEVEL
=> 174

Looking at the source code for ping.rb around line 46:

begin
  timeout(timeout) do
    s = TCPSocket.new(host, service)
    s.close
  end
rescue Errno::ECONNREFUSED
  return true
rescue Timeout::Error, StandardError
  return false
end

I don’t understand why the exception isn’t being caught. You could try
adding:

rescue Exception => e
  $stderr.puts "Actual exception: #{e.class.inspect}"

just before the ‘end’, see if that shows anything.

Incidentally, if you’re trying to ping lots of hosts, ‘multiping’ may be
a better tool. It can ping them all in parallel, and you can run it from
ruby using IO.popen or %x{…} to collect the results.