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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With