From: "Sandor Szücs" Date: 2009-05-17T06:26:25+09:00 Subject: Re: Desperate help needed with SSH On 13.05.2009, at 11:05, alan@isdial.net wrote: > -- snip -- > > require 'rubygems' > require 'net/ssh' > > hosts = ['10.0.255.1' , '10.0.255.2' , '10.0.255.3'] > > instructions = [ > 'sh version', > 'sh cdp ne' > ] These instructions looks strange for me. Are you sure that these run on all hosts? Try to use other instructions that should work like ["echo foo", "sh -c 'echo bar'"] Maybe your instructions kill the shell environment by calling exec or something similar. > hosts.each do |host| > Net::SSH.start(host, 'user', :password => 'password') do |ssh| File.open("output/#{host}.txt", 'w') do |fn| > > instructions.each do |command| > fn.puts "|--------\[#{command}\]--------------------------" > fn.puts ssh.exec!(command) > end end > > end > end > > -- snip -- This part should be ok, but you can improve your code and use a block for File.open(). File.open("output/#{host}.txt", 'w') do |fn| .. end # fn.close not needed, because the block end close the file already. > When I run this I get the following error : > > Net::SSH::ChannelOpenFailed: (4) > > If I remove the instructions.each loop and run only a single > command. Life > is good and it all works. If I add a second ssh.exec! or the loop, the > error returns. Calling ssh.exec!() twice in a session is not a problem. irb> Net::SSH.start(host, user, :password => passwd) do |ssh| irb> ['sh -c "echo foo"', 'echo bar'].each do |command| irb> puts ssh.exec!(command) irb> end irb> end foo bar hth. regards, Sandor Szücs --