On Thu, 7 Dec 2006, Richard Conroy wrote:

> I wrote a simple tool to iterate a network to try and find web servers
> running on specific ports. We have a lot of devices & software with a
> web UI, and I thought that this would be a handy way to find them,
> and even tell what they are.
>
> I thought this would be a handy coding project too, and a good way to
> cut my teeth on Ruby threads, and build up some usage with
> Mechanize.
>
> BTW I am running this on *Windows XP*.
>
> However my code is quite obviously executing this serially. Is there
> something obviously wrong with my code below? (results after
> code snippet). I am aware this could make my machine choke from
> thread overkill, but I wanted to get it working in parallel first.
> Perhaps Mechanize instances have some shared elements?
>
> ============================

require 'mechanize'

threads = Array.new

puts "sweep of 153.200.72.* segment http ports"

(1..254).each do |ran|
 	threads << Thread.new(ran) { |r|
 		agent = WWW::Mechanize.new
 		agent.user_agent_alias = 'Windows Mozilla'
 		ports = [80,8080]
 		ports.each do |p|
 			begin
 				page = agent.get("http://153.200.72."+r.to_s+":"+p.to_s)
 				puts "153.200.72."+r.to_s+":"+p.to_s+" - "+page.title
 			rescue
 				puts "153.200.72."+r.to_s+":"+p.to_s+" - NOTHING"
 			end
 		end
 	}
end

threads.each { |aThread|  aThread.join }  # THIS MUST BE OUTSIDE THE LOOP!



fyi.  starting a thread, and then immediately joining it is the same as not
using a thread at all!

another fyi - threads are io (even socket io) is a dealy combination on
windows.  run this on linux/mac if possible.

regards.

-a
-- 
if you want others to be happy, practice compassion.
if you want to be happy, practice compassion.  -- the dalai lama