# Ruby, net-ssh, running an application on remote systems

**URL:** <https://www.ruby-forum.com/t/ruby-net-ssh-running-an-application-on-remote-systems/88841>\
**Category:** Ruby\
**Created:** [March 14, 2007, 9:57pm UTC](https://www.ruby-forum.com/t/ruby-net-ssh-running-an-application-on-remote-systems/88841 "2007-03-14T21:57:17Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 14, 2007, 9:57pm UTC](https://www.ruby-forum.com/t/ruby-net-ssh-running-an-application-on-remote-systems/88841/1 "2007-03-14T21:57:17Z")

</div>

ok, I have a server with a dozen systems attached to it. there are  
commands that I need to run on each system one at a time. i can  
connect to each system with the admin script, i can launch basic  
commands i can even launch the script i need to but when that script  
tries to launch the partimage application to backup the windows  
partitions (this is all being done from linux) it doesn’t, i don’t get  
any good errors it just dies closes the connection an moves onto the  
next system.

==admin.rb==  
#!/usr/bin/ruby  
require ‘net/ssh’  
Username=‘root’  
Password=’{plain\_text\_password}’  
Hosts = (101…112).collect {|x| “192.168.0.#{x}”}  
def classroom\_pc(host, command)  
Net::SSH.start( host, Username, Password) do |session|  
session.open\_channel do |channel|  
channel.on\_success do  
puts “Connected to #{host}”  
end  
channel.on\_close do  
puts “Connection Closed to #{host}”  
end  
#channel.on\_data do |ch, data|  
# puts “\trecieved #{data}”  
# puts “\tfrom #{host}”  
#end

```
  channel.send_request "shell", nil, true
  channel.send_data "#{command}\n"
  channel.send_data "exit\n"
end
session.loop

```

end  
end  
if $0 == **FILE**  
Hosts.length.times do |i|  
classroom\_pc(Hosts[i], “/mnt/backup/partimage.rb  
save”)  
end  
end

==/mnt/backup/partimage.rb==  
#!/usr/bin/ruby  
Win\_drive = “/dev/hda1”  
Location = “/mnt/backup/classroom/”  
File\_type = “partimag.gz”  
def help\_message  
puts “Usage:”  
puts “\tsave to save the image”  
puts “\tload to restore an image”  
puts “\thelp prints this message”  
end  
def hostname  
`hostname`.chomp  
end  
def save\_image  
system(“rm #{Location}#{hostname}\*”)  
system(“partimage -odb -f3 -z1 save #{Win\_drive}   
#{Location}#{hostname}.#{File\_type}”)  
end  
def load\_image  
system(“partimage -eb -f3 restore #{Win\_drive}   
#{Location}#{hostname}.#{File\_type}.000”)  
end

if $0 == **FILE**  
unless ARGV.length == 1  
help\_message  
end  
if ARGV.length == 1  
help\_message if ARGV[0].downcase == “help”  
save\_image if ARGV[0].downcase == “save”  
load\_image if ARGV[0].downcase == “load”  
end  
end

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 14, 2007, 10:50pm UTC](https://www.ruby-forum.com/t/ruby-net-ssh-running-an-application-on-remote-systems/88841/2 "2007-03-14T22:50:28Z")

</div>

I don’t have an answer for your specific question, but you might look  
into  
capistrano. It’s meant for exactly this, although it runs each request  
in  
parallel, not sequentially, which may or may not be a good thing for  
you…

-philip

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 25, 2007, 11:00pm UTC](https://www.ruby-forum.com/t/ruby-net-ssh-running-an-application-on-remote-systems/88841/3 "2007-09-25T23:00:58Z")

</div>

sorry about that, I was implementing the shell and a responce, mozilla  
crashed and google talk sent a blank message. the app i’m running  
seems to be using ncurses, will that cause another problem?

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 25, 2007, 11:05pm UTC](https://www.ruby-forum.com/t/ruby-net-ssh-running-an-application-on-remote-systems/88841/4 "2007-09-25T23:05:44Z")

</div>

def classroom\_pc(host, command)  
Net::SSH.start( host, Username, Password) do |session|  
shell = session.shell.sync  
puts “Opened Connection to #{host}”

```
p shell.send_command( "#{command}")
shell.exit
puts "Closed Connection to #{host}"

```

end  
end  
i modified the classroom\_pc function, now i’m getting an error  
#\<struct Net::SSH::Service:🐚:SyncShell::CommandOutput stdout="",  
stderr=“Failed to open terminal.TERM environment variable needs  
set.\r\n”, status=0\>  
the application runs in a curses environment.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 14, 2007, 11:39pm UTC](https://www.ruby-forum.com/t/ruby-net-ssh-running-an-application-on-remote-systems/88841/5 "2007-03-14T23:39:35Z")

</div>

On 3/14/07, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 

Maybe you would be better off using the net/ssh shell instead of  
launching a shell over the channel.  
[http://net-ssh.rubyforge.org/chapter-5.html](http://net-ssh.rubyforge.org/chapter-5.html)

Alternatively you might use channel.exec instead of channel.request  
and channel.send\_data that do not seem to be the right hammer for this  
nail if I recall correctly.  
[http://net-ssh.rubyforge.org/chapter-4.html](http://net-ssh.rubyforge.org/chapter-4.html)

HTH  
Robert

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 25, 2007, 11:06pm UTC](https://www.ruby-forum.com/t/ruby-net-ssh-running-an-application-on-remote-systems/88841/6 "2007-09-25T23:06:59Z")

</div>

def classroom\_pc( host, command )  
Net::SSH.start( host, Username, Password) do |session|  
shell = session.shell.sync  
puts “Opened Connection to #{host}”  
shell.send\_command( “export TERM=linux” )  
p shell.send\_command( “#{command}”)  
shell.exit  
puts “Closed Connection to #{host}”  
end  
end

that seems to have done it, it doesnt display the ncurses info but i  
can live with that for now.
