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.
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.
Net::SSH, I was wondering why you broke up the backticks style, instead of using system? - By the way, why not use ~ on remote?~ 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.File would expand locally. What's a good method for getting the remote home folder?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
net-ssh It's just a wrapper." at the time.