Forum: Ruby on Rails Custom Class Problem

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Ab6662d4241124bf3f2971197915e309?d=identicon&s=25 Jason Madigan (nuphor)
on 2007-02-11 16:31
I'm a novice Ruby programmer and I've come up against a problem which I
really have no idea as to how to fix. I'm making a small Ruby/RoR web
application to poll and display information from SNMP devices. The
problem I'm having is trying to poll an IP range for SNMP enabled
devices. I hacked together a small method to do this using the
SNMP Scanner gem/command line app as a basis
( I just modified it by throwing it into
a method and have
it return an array of "live" IP addresses.

# In application.rb
class IPAddr
  # The broadcast method calculate the broadcast
  # address of the range (if any)
  def broadcast
    return @addr + (IPAddr::IN4MASK - @mask_addr)
    # The each_address method iterates over each address
    # of the ip range
    def each_address
      (@addr..broadcast).each do |addr|
        yield _to_string(addr)

#In network_controller.rb
  def poll(inc_ip)
    # Default Settings
    timeout = 200
    mib = "sysDescr.0"
    protocol = "2c"
    comunity = "public"
    verbose = false

    args = [timeout,mib,protocol,comunity]
    live = []

    iprange =
    threads = []

    iprange.each_address do |ip|
        threads <<,args) do |ip,args|
          timeout,mib,protocol,comunity = args
   => ip,
                               :Community => comunity,
                               :Port => 161,
                               :Timeout => (timeout/1000.0)) do |man|
              res = man.get([mib])
              answer = res.varbind_list[0].value
              live << "#{ip}"
              print "#{ip}:\t#{answer}\n"



    return live

To test, I just an action to poll a range ( and
flash the first result to me:

#In network_controller.rb
  def live_devices
    live =""))
    flash[:notice] = live[0]
    redirect_to :action => 'list'

This works fine and reveals the IP addresses of two SNMP devices on my
network just fine. However, if I try to poll, the
application hangs and seems to spawn hundreds of threads, most of which
never complete. In the command line window I can see it returning the
first and only SNMP device within that range, the router at
My knowledge of CIDR notation is cursory but should scan
between and unless I'm mistaken. I'm stumped
as to what the problem is. Either there's a serious problem with the
threading somewhere or something else. Another thing I noted is that
using the SNMP scanner gem from the command line to scan a single IP
address works fine, but using the code above to scan single IP returns

This is all very confusing :(

[b]Edit:[/b] I don't think I'm initialising the IPAddr object I want
correctly as calling "iprange =" after renaming the
IPAddr class in application.rb to IPTest throws an error. I think this
is where my problem lies.

I've moved the code from application.rb to iptest.rb in /lib and use
require 'iptest' in environment.rb but now calling the live_devices
action just yields a "wrong number of arguments (1 for 0)" error.
Perhaps it's not loaded correctly?
D337e2cf7c6d03caac6988d83e0e7f66?d=identicon&s=25 Michael Guterl (Guest)
on 2007-02-12 05:54
(Received via mailing list)
Hi Jason,

On 2/11/07, Jason Madigan <> wrote:
> it return an array of "live" IP addresses.
>     # of the ip range
>   def poll(inc_ip)
>     iprange =
>                                :Timeout => (timeout/1000.0)) do |man|
>       end
> network just fine. However, if I try to poll, the
> This is all very confusing :(

You may have better luck with an answer on RubyTalk instead of RailsTalk
the focus here is on Rails and this is more of a generic Ruby problem.

[b]Edit:[/b] I don't think I'm initialising the IPAddr object I want
> Posted via
> >

Michael Guterl
This topic is locked and can not be replied to.