# Timeout Error in pingecho

**URL:** <https://www.ruby-forum.com/t/timeout-error-in-pingecho/188440>\
**Category:** Ruby\
**Created:** [May 12, 2010, 12:49am UTC](https://www.ruby-forum.com/t/timeout-error-in-pingecho/188440 "2010-05-12T00:49:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![magiccoder](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bc8723/32.png) [@magiccoder](https://www.ruby-forum.com/u/magiccoder)\
**Post date:** [May 12, 2010, 12:49am UTC](https://www.ruby-forum.com/t/timeout-error-in-pingecho/188440/1 "2010-05-12T00:49:26Z")

</div>

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:in`pingecho’ 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 (removed\_email\_address@domain.invalid)

# 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 (removed\_email\_address@domain.invalid)\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 .

---

<div class="post-metadata">

**Author:** ![magiccoder](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bc8723/32.png) [@magiccoder](https://www.ruby-forum.com/u/magiccoder)\
**Post date:** [May 12, 2010, 1:35am UTC](https://www.ruby-forum.com/t/timeout-error-in-pingecho/188440/2 "2010-05-12T01:35:45Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![magiccoder](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bc8723/32.png) [@magiccoder](https://www.ruby-forum.com/u/magiccoder)\
**Post date:** [May 12, 2010, 1:30am UTC](https://www.ruby-forum.com/t/timeout-error-in-pingecho/188440/3 "2010-05-12T01:30:15Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![magiccoder](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bc8723/32.png) [@magiccoder](https://www.ruby-forum.com/u/magiccoder)\
**Post date:** [May 12, 2010, 11:20am UTC](https://www.ruby-forum.com/t/timeout-error-in-pingecho/188440/4 "2010-05-12T11:20:02Z")

</div>

Sajjad Po wrote:

> /usr/lib/ruby/1.8/timeout.rb:60:in `timeout': execution expired from /usr/lib/ruby/1.8/ping.rb:46:in`pingecho’  
> 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.
