I am trying to use monitors for the first time in ruby. I am familiar
with the concept but am having some problems making things work.
Background: I have a program that uses nmap-parser to parse large nmap
scans and uses the call back to deliver results for each host. The call
back is threaded.
I want to build lists of IP addresses for further investigation from the
callback routine. I have made a class AddressList:
class AddressList < Array
include MonitorMixin
def initialise # redundant ?
super
end
def <<(addr)
synchronise do # make sure we don't clobber another thread
super( addr )
end
end
end
we need to protect the push code so that to threads don't collide so I
put a synchronise block around it.
What appears to happen is that the code never exits the synchronise
block.
puts before the block get executed but ones after do not and nothing is
ever added to the list.
Clearly I'm missing something.
The callback routine:
callback = proc do |host|
return unless host.status == 'up'
host.scripts do |script|
s = script.output.gsub(/\n/, ':')
next if s =~ / 2008 R2|ERROR/;
next unless s =~ /Vista|2008/
$addresses << host.addr
end # scripts
schedule if $addresses.size > BATCH_SIZE
end
yes, I'm looking for machines running smb2 to get them before the worm
does
BTW I do have this program working without treads but I want to know why
this does not work !
synchronize is spelled with a z. You misspelled it in the example you
posted here. Is it possible that you misspelled it in your actual code,
and the NoMethodError is being raised in a place where it doesn't
generate output to alert you to the problem?
···
On Thu, 01 Oct 2009 08:22:26 +0900, Russell Fulton wrote:
Hi
I am trying to use monitors for the first time in ruby. I am familiar
with the concept but am having some problems making things work.
Background: I have a program that uses nmap-parser to parse large nmap
scans and uses the call back to deliver results for each host. The call
back is threaded.
I want to build lists of IP addresses for further investigation from the
callback routine. I have made a class AddressList:
class AddressList < Array
include MonitorMixin
def initialise # redundant ?
super
end
def <<(addr)
synchronise do # make sure we don't clobber another thread
super( addr )
end
end
end
we need to protect the push code so that to threads don't collide so I
put a synchronise block around it.
What appears to happen is that the code never exits the synchronise
block.
puts before the block get executed but ones after do not and nothing is
ever added to the list.
Clearly I'm missing something.
The callback routine:
callback = proc do |host|
return unless host.status == 'up'
host.scripts do |script|
s = script.output.gsub(/\n/, ':')
next if s =~ / 2008 R2|ERROR/;
next unless s =~ /Vista|2008/
$addresses << host.addr
end # scripts
schedule if $addresses.size > BATCH_SIZE
end
yes, I'm looking for machines running smb2 to get them before the worm
does
BTW I do have this program working without treads but I want to know why
this does not work !
Russell
--
Chanoch (Ken) Bloom. PhD candidate. Linguistic Cognition Laboratory.
Department of Computer Science. Illinois Institute of Technology. http://www.iit.edu/~kbloom1/
On Thu, 01 Oct 2009 08:22:26 +0900, Russell Fulton wrote:
callback routine. I have made a class AddressList:
synchronise do # make sure we don't clobber another thread
block.
return unless host.status == 'up'
does
BTW I do have this program working without treads but I want to know why
this does not work !
Russell
synchronize is spelled with a z. You misspelled it in the example you
posted here. Is it possible that you misspelled it in your actual code,
and the NoMethodError is being raised in a place where it doesn't
generate output to alert you to the problem?
On Fri, 02 Oct 2009 07:49:40 +0900, Russell Fulton wrote:
Ken Bloom wrote:
On Thu, 01 Oct 2009 08:22:26 +0900, Russell Fulton wrote:
callback routine. I have made a class AddressList:
synchronise do # make sure we don't clobber another thread
block.
return unless host.status == 'up'
does
BTW I do have this program working without treads but I want to know
why this does not work !
Russell
synchronize is spelled with a z. You misspelled it in the example you
posted here. Is it possible that you misspelled it in your actual code,
and the NoMethodError is being raised in a place where it doesn't
generate output to alert you to the problem?
Of course. American spelling, sigh...
Yup! that fixed it! Thanks very much Ken
Need a "pragma: Ex British Empire"
I wonder why the NoMethodError does not appear?
--
Chanoch (Ken) Bloom. PhD candidate. Linguistic Cognition Laboratory.
Department of Computer Science. Illinois Institute of Technology. http://www.iit.edu/~kbloom1/
Interesting, I somewhat managed to screw up the name of the exception, which thankfully causes *another* exception, still demonstrating the usefulness of Thread.abort_on_exception
irb(main):003:0> Thread.start { raise NoMethodException }
(irb):3:in `irb_binding': uninitialized constant NoMethodException
(NameError)
from (irb):3:in `start'
from (irb):3:in `irb_binding'
from /opt/local/lib/ruby/1.8/irb/workspace.rb:52:in `irb_binding'
from /opt/local/lib/ruby/1.8/irb/workspace.rb:52
Or you can save the reference to the thread and join it:
irb(main):003:0> t = Thread.start{raise NoMethodError}
=> #<Thread:0x7f5ee476a058 dead>
irb(main):004:0> t.join
NoMethodError: NoMethodError
from (irb):3
from (irb):4:in `join'
from (irb):4
···
On Fri, Oct 2, 2009 at 1:25 AM, Luc Heinrich <luc@honk-honk.com> wrote: