From: Justin Rudd Date: 2004-09-12T01:36:03+09:00 Subject: Re: Net-SSH Threaded PortForward Script Problems Take this with a grain of salt because I have't used the Ruby Thread library, but is is possible that you have a race condition? Your thread has 4 Ruby instructions to execute before you get to the next SSH.start. That seems like a race condition to me. You need sometype of event to signal after the secondary thread has gotten started. Maybe something like this... m = Mutex.new() cv = ConditionVariable.new() t = Thread.new do m.synchronize { Net::SSH.start( 'localhost', 'localuser', 'passwd' ) do |session| mgr = PortForwardManager.new( session ) mgr.forward_local( 1236, '10.0.0.4', 22 ) cv.signal } session.main_loop end end # let other threads work Thread.pass m.synchronize { cv.wait(m) } Net::SSH.start( 'localhost', 1236, 'remoteuser', 'passwd' ) do |session| result = session.exec("hostname; uptime") puts result.data end threads.push( t ) threads.each { |t| t.join } On Sun, 12 Sep 2004 00:54:59 +0900, otaku wrote: > jamis, > > i reversed order and still no luck i get the following error message > from the below code irb is telling me that the thread is still being > killed prior > to anything starting: > > BEGIN ERROR MESSAGE > ---------- > > [root@falcon1 src]# ./example.rb > /usr/local/lib/ruby/site_ruby/1.8/net/ssh/transport/session.rb:132:in > `initialize': Connection refused - connect(2) (Errno::ECONNREFUSED) > from > /usr/local/lib/ruby/site_ruby/1.8/net/ssh/transport/session.rb:132:in > `open' > from > /usr/local/lib/ruby/site_ruby/1.8/net/ssh/transport/session.rb:132:in > `initialize' > from /usr/local/lib/ruby/site_ruby/1.8/net/ssh.rb:175:in `new' > from /usr/local/lib/ruby/site_ruby/1.8/net/ssh.rb:175:in `open' > from /usr/local/lib/ruby/site_ruby/1.8/net/ssh.rb:93:in `start' > from ./example.rb:19 > --------- > END ERROR MESSAGE > > BEGIN CODE > ------------- > > #!/usr/local/bin/ruby > > require 'thread' > require 'net/ssh' > require 'net/ssh/service/forward' > > threads = [] > > t = Thread.new do > Net::SSH.start( 'localhost', 'localuser', 'passwd' ) do |session| > mgr = PortForwardManager.new( session ) > mgr.forward_local( 1236, '10.0.0.4', 22 ) > session.main_loop > end > end > > Net::SSH.start( 'localhost', 1236, 'remoteuser', 'passwd' ) do > |session| > result = session.exec("hostname; uptime") > puts result.data > end > > threads.push( t ) > threads.each { |t| t.join } > > ------ > END > > -- Justin Rudd http://seagecko.org/thoughts/