[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: Desperate help needed with SSH

Sandor Szücs

5/16/2009 9:26:00 PM


On 13.05.2009, at 11:05, alan@isdial.net wrote:
> -- snip --
>
> require 'rubygems'
> require 'net/ssh'
>
> hosts =3D ['10.0.255.1' , '10.0.255.2' , '10.0.255.3']
>
> instructions =3D [
> '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 =3D> '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 =20
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 =20
> 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 =3D> 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=FCcs
--




2 Answers

Alan Claughan

5/18/2009 5:23:00 PM

0

Hi Sandor

Thanks for the reply.

The host I am connecting to is a Cisco Router. The commands definitely =20=

work on a manual SSH session from my terminal

The first command always works.
I get the channel failure when trying to run the second command.
I tried swapping the two commands around, but I get the same failure.

I used the following code to talk to my mac:

require 'rubygems'
require 'net/ssh'

Net::SSH.start('127.0.0.1', 'user', :password =3D> 'password') do =
|ssh|
ssh.exec "ls -al"
ssh.exec "pwd"
ssh.exec "ps -axe"
ssh.loop
end

Which worked great. I then changed the IP address and replaced the =20
commands with router commands:

require 'rubygems'
require 'net/ssh'

Net::SSH.start('10.0.255.1', 'name', :password =3D> 'password') do =
|ssh|
ssh.exec "sh ver"
ssh.exec "cdp ne"
ssh.loop
end

And it fails at the second ssh.exec, so I'm guessing this is related =20
to the Cisco SSH Server.

Alan.

On 17 May 2009, at 12:26 AM, Sandor Sz=FCcs wrote:

>
> On 13.05.2009, at 11:05, alan@isdial.net wrote:
>> -- snip --
>>
>> require 'rubygems'
>> require 'net/ssh'
>>
>> hosts =3D ['10.0.255.1' , '10.0.255.2' , '10.0.255.3']
>>
>> instructions =3D [
>> '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 =3D> '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 =20
> 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 =20
>> command. Life
>> is good and it all works. If I add a second ssh.exec! or the loop, =20=

>> the
>> error returns.
>
> Calling ssh.exec!() twice in a session is not a problem.
>
> irb> Net::SSH.start(host, user, :password =3D> 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=FCcs
> --
>
>
>
>


Sandor Szücs

5/18/2009 9:07:00 PM

0


On 18.05.2009, at 19:23, Alan Claughan wrote:

> And it fails at the second ssh.exec, so I'm guessing this is related =20=

> to the Cisco SSH Server.


Ack.
Maybe there are options that you can set on the router to let the =20
channel open?

regards, Sandor Sz=FCcs
--