4

Currently I'm calling a remote script using the backtick method, it works, but it feels wrong and nasty...

`ssh user@host $(echo "~/bin/command \\\"#{parameter}\\\"")`

Can someone give me a better way of expressing this?

Many thanks.

2 Answers 2

5

I would make it a little pretty, if that is what you are after:

#!/usr/bin/env ruby
# encoding: utf-8

home    = "/path/to/users/home/on/remote"
binary  = File.join(home, "bin", "command")
command = "#{binary} '#{parameter}'"
puts `ssh user@host #{command}`

Otherwise, you can use the net-ssh library, and do something like this:

#!/usr/bin/env ruby
# encoding: utf-8

require 'net/ssh'
Net::SSH.start('host', 'user', :password => "<your-password>") do
  home    = "/path/to/users/home/on/remote"
  binary  = File.join(home, "bin", "command")
  command = "#{binary} '#{parameter}'"
  output = ssh.exec!(command)
  puts output
end

There are, obviously, automated ways of capturing remote user's home directory path, but I skipped that part.

Sign up to request clarification or add additional context in comments.

3 Comments

Looks nicer indeed, using Net::SSH, I was wondering why you broke up the backticks style, instead of using system? - By the way, why not use ~ on remote?
Reason for using explicit path over ~ is more based upon my personal convention. I prefer not to rely on automatic path detection, esp. if the server is windows based, and also, prefer to use absolute paths over relative ones. Moreover, File.expand_path("~") can not be used in this case, since it will expand to the local server's home instead of remote server's home.
thanks for the info, I assumed File would expand locally. What's a good method for getting the remote home folder?
2

Use net-ssh. It's just a wrapper.

require 'net/ssh'

Net::SSH.start('host', 'user', :password => "password") do |ssh|
  # capture all stderr and stdout output from a remote process
  output = ssh.exec!("~/bin/command '#{parameter}'")
end

puts output

3 Comments

@r3mus - suggesting to use net-ssh is hardly a comment. Although it's not a particularly verbose answer, it is an answer nonetheless.
@Slomojo heh, well, it only said "Use net-ssh It's just a wrapper." at the time.
Hi @r3mus I know, I read the history, that's the context of my comment. Granted, I would've had to go read the docs, but they were linked. Nothing wrong with the answer, at least it was correct / valid.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.