通过ssh调用远程命令,使用Ruby,改进语法?

通过ssh调用远程命令,使用Ruby,改进语法?,ruby,ssh,Ruby,Ssh,目前我正在使用backtick方法调用一个远程脚本,它可以工作,但感觉它是错误的和令人讨厌的 `ssh user@host $(echo "~/bin/command \\\"#{parameter}\\\"")` 有人能给我一个更好的表达方式吗 非常感谢。请使用。这只是一个包装 require 'net/ssh' Net::SSH.start('host', 'user', :password => "password") do |ssh| # capture all stder

目前我正在使用backtick方法调用一个远程脚本,它可以工作,但感觉它是错误的和令人讨厌的

`ssh user@host $(echo "~/bin/command \\\"#{parameter}\\\"")`
有人能给我一个更好的表达方式吗

非常感谢。

请使用。这只是一个包装

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
使用。这只是一个包装

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

如果这是你想要的,我会让它变得漂亮一点:

#!/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}`
否则,您可以使用库,并执行以下操作:

#!/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
#/usr/bin/env ruby
#编码:utf-8
需要“net/ssh”
Net::SSH.start('host','user',:password=>“”)do
home=“/path/to/users/home/on/remote”
binary=File.join(home,“bin”,“command”)
command=“#{binary}'#{parameter}'”
output=ssh.exec!(指挥部)
输出
结束

显然,有自动捕获远程用户主目录路径的方法,但我跳过了这一部分。

如果这是您想要的,我会让它变得漂亮一点:

#!/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}`
否则,您可以使用库,并执行以下操作:

#!/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
#/usr/bin/env ruby
#编码:utf-8
需要“net/ssh”
Net::SSH.start('host','user',:password=>“”)do
home=“/path/to/users/home/on/remote”
binary=File.join(home,“bin”,“command”)
command=“#{binary}'#{parameter}'”
output=ssh.exec!(指挥部)
输出
结束

显然,有自动捕获远程用户主目录路径的方法,但我跳过了这一部分。

看起来确实更好,使用
Net::SSH
,我想知道为什么您打破了backticks样式,而不是使用
system
?-顺便问一下,为什么不在远程上使用
~
?在
~
上使用显式路径的原因更多地基于我的个人习惯。我不喜欢依赖自动路径检测,尤其是在服务器基于windows的情况下,而且更喜欢使用绝对路径而不是相对路径。此外,在这种情况下,
File.expand\u path(“~”)
不能使用,因为它将扩展到本地服务器的主页,而不是远程服务器的主页。感谢您提供的信息,我假设
File
将在本地扩展。获取远程主文件夹的好方法是什么?看起来确实更好,使用
Net::SSH
,我想知道为什么您打破了backticks样式,而不是使用
system
?-顺便问一下,为什么不在远程上使用
~
?在
~
上使用显式路径的原因更多地基于我的个人习惯。我不喜欢依赖自动路径检测,尤其是在服务器基于windows的情况下,而且更喜欢使用绝对路径而不是相对路径。此外,在这种情况下,
File.expand\u path(“~”)
不能使用,因为它将扩展到本地服务器的主页,而不是远程服务器的主页。感谢您提供的信息,我假设
File
将在本地扩展。获取远程主文件夹的好方法是什么?@r3mus-建议使用NetSSH并不是一个评论。虽然这不是一个特别冗长的答案,但它仍然是一个答案。@Slomojo呵呵,它只是说“使用
netssh
它只是一个包装器。”当时。嗨@r3mus我知道,我读了历史,这是我评论的上下文。当然,我得去看文件,但它们是链接的。答案没有问题,至少它是正确的/有效的。@r3mus-建议使用netssh很难成为评论。虽然这不是一个特别冗长的答案,但它仍然是一个答案。@Slomojo呵呵,它只是说“使用
netssh
它只是一个包装器。”当时。嗨@r3mus我知道,我读了历史,这是我评论的上下文。当然,我得去看文件,但它们是链接的。答案没有错,至少是正确的/有效的。