如何通过SSH执行远程Ruby脚本文件?

时间:2021-11-17 00:00:41

I have a Ruby file called "test.rb", and I uploaded it to the server via Net::SCP.

我有一个名为“test.rb”的Ruby文件,我通过Net :: SCP将其上传到服务器。

The contents of the file are:

该文件的内容是:

puts "Hello, World"

How do I go about executing that file via Net::SSH and grab the STDOUT or STDERR?

如何通过Net :: SSH执行该文件并获取STDOUT或STDERR?

This is the error I'm getting:

这是我得到的错误:

Bash: Ruby not found 

That's because Net::SSH won't load a login shell.

那是因为Net :: SSH不会加载登录shell。

I've tried everything from Net::SSH::Shell to SSHkit and rye to solve the problem of executing the script and getting any STDOUT back.

我尝试了从Net :: SSH :: Shell到SSHkit和黑麦的所有内容,以解决执行脚本和获取任何STDOUT的问题。

How do I execute a script via Net::SSH when I don't have access to a login shell, and get any STDOUT or STDERR?

当我无法访问登录shell并获取任何STDOUT或STDERR时,如何通过Net :: SSH执行脚本?

I'm using Ruby-2.1.0.

我正在使用Ruby-2.1.0。

command = "ruby -e'print :hello'"
Net::SSH.start(server_connection.host, server_connection.ssh_username, port: server_connection.ssh_port, paranoid: false)  do |ssh|
  job.stdout  = ""
  job.stderr = ""
  ssh.exec! command do |channel, stream, data|
    job.stdout << data if stream == :stdout
    job.stderr << data if stream == :stderr
  end
  ssh.close
end

2 个解决方案

#1


1  

This might help explain a bit:

这可能有助于解释一下:

require 'net/ssh'

# put commands to send to the remote Ruby here...
CMDs = [
  '-v',
]

print 'Enter your password: '
password = gets.chomp

Net::SSH.start('localhost', ENV['USER'], :password => password) do |ssh|

  remote_ruby = ssh.exec!('/usr/bin/which ruby').chomp
  puts 'Using remote Ruby: "%s"' % remote_ruby

  CMDs.each do |cmd|

    puts 'Sending: "%s"' % cmd

    stdout = ''
    ssh.exec!("#{ remote_ruby } #{ cmd }") do |channel, stream, data|
      stdout << data if stream == :stdout
    end

    puts 'Got: %s' % stdout
    puts
  end

end

Save that to a Ruby file. Turn on SSH access on your local machine, then run that script. It'll prompt for your password, then connect to the localhost and grab the path to the default Ruby. Then it'll loop through all commands in CMDs, executing them and returning their STDOUT.

将其保存到Ruby文件中。打开本地计算机上的SSH访问权限,然后运行该脚本。它将提示您输入密码,然后连接到localhost并获取默认Ruby的路径。然后它将循环遍历CMD中的所有命令,执行它们并返回它们的STDOUT。

For more options see the Net::SSH synopsis.

有关更多选项,请参阅Net :: SSH概要。

/usr/bin/which ruby

is a standard way to figure out which executable the system will use for a particular command. It searches the PATH and returns the path to that command. Usually that'll be /usr/bin/ruby for a *nix machine if Ruby was bundled with the OS or installed using yum or apt-get. If you installed it from source it might be in /usr/local/bin/ruby.

是确定系统将用于特定命令的可执行文件的标准方法。它搜索PATH并返回该命令的路径。如果Ruby与操作系统捆绑在一起或使用yum或apt-get安装,那么对于* nix机器通常都是/ usr / bin / ruby​​。如果您从源代码安装它,它可能位于/ usr / local / bin / ruby​​中。

If you used RVM or rbenv or Homebrew, you'll have to sniff out their presence, and use whatever tricks their authors recommend. This code will hang for a bit, then raise an exception probably.

如果您使用RVM或rbenv或Homebrew,您将不得不嗅出他们的存在,并使用他们的作者推荐的任何技巧。这段代码会挂起一点,然后可能会引发一个异常。

On my machine, running that code outputs:

在我的机器上,运行该代码输出:

Enter your password: some(secret)thang
Using remote Ruby: "/usr/bin/ruby"
Sending: "-v"
Got: ruby 1.8.7 (2012-02-08 patchlevel 358) [universal-darwin12.0]

#2


0  

Try this:

 ssh username@host "ruby -e'print :hello'"

This would execute Ruby on the host machine, and give you output the same way you can also run any other script on a remote machine.

这将在主机上执行Ruby,并以与在远程计算机上运行任何其他脚本相同的方式为您提供输出。

require 'net/ssh'

host = "localhost"
username = "tuxdna"
password = "SOMEAWESOMEPASSWORDHERE"
command = "ruby -e'print :hello'"

class Job
  attr_accessor :stdout
  attr_accessor :stderr
end

job = Job.new

Net::SSH.start(host, username, password: password)  do |ssh|
  job.stdout  = ""
  job.stderr = ""
  ssh.exec! command do |channel, stream, data|
    job.stdout << data if stream == :stdout
    job.stderr << data if stream == :stderr
  end
  # ssh.close
end
p job

OUTPUT:

$ ruby myssh.rb
#<Job:0x00000002bed0a0 @stdout="hello", @stderr="">

#1


1  

This might help explain a bit:

这可能有助于解释一下:

require 'net/ssh'

# put commands to send to the remote Ruby here...
CMDs = [
  '-v',
]

print 'Enter your password: '
password = gets.chomp

Net::SSH.start('localhost', ENV['USER'], :password => password) do |ssh|

  remote_ruby = ssh.exec!('/usr/bin/which ruby').chomp
  puts 'Using remote Ruby: "%s"' % remote_ruby

  CMDs.each do |cmd|

    puts 'Sending: "%s"' % cmd

    stdout = ''
    ssh.exec!("#{ remote_ruby } #{ cmd }") do |channel, stream, data|
      stdout << data if stream == :stdout
    end

    puts 'Got: %s' % stdout
    puts
  end

end

Save that to a Ruby file. Turn on SSH access on your local machine, then run that script. It'll prompt for your password, then connect to the localhost and grab the path to the default Ruby. Then it'll loop through all commands in CMDs, executing them and returning their STDOUT.

将其保存到Ruby文件中。打开本地计算机上的SSH访问权限,然后运行该脚本。它将提示您输入密码,然后连接到localhost并获取默认Ruby的路径。然后它将循环遍历CMD中的所有命令,执行它们并返回它们的STDOUT。

For more options see the Net::SSH synopsis.

有关更多选项,请参阅Net :: SSH概要。

/usr/bin/which ruby

is a standard way to figure out which executable the system will use for a particular command. It searches the PATH and returns the path to that command. Usually that'll be /usr/bin/ruby for a *nix machine if Ruby was bundled with the OS or installed using yum or apt-get. If you installed it from source it might be in /usr/local/bin/ruby.

是确定系统将用于特定命令的可执行文件的标准方法。它搜索PATH并返回该命令的路径。如果Ruby与操作系统捆绑在一起或使用yum或apt-get安装,那么对于* nix机器通常都是/ usr / bin / ruby​​。如果您从源代码安装它,它可能位于/ usr / local / bin / ruby​​中。

If you used RVM or rbenv or Homebrew, you'll have to sniff out their presence, and use whatever tricks their authors recommend. This code will hang for a bit, then raise an exception probably.

如果您使用RVM或rbenv或Homebrew,您将不得不嗅出他们的存在,并使用他们的作者推荐的任何技巧。这段代码会挂起一点,然后可能会引发一个异常。

On my machine, running that code outputs:

在我的机器上,运行该代码输出:

Enter your password: some(secret)thang
Using remote Ruby: "/usr/bin/ruby"
Sending: "-v"
Got: ruby 1.8.7 (2012-02-08 patchlevel 358) [universal-darwin12.0]

#2


0  

Try this:

 ssh username@host "ruby -e'print :hello'"

This would execute Ruby on the host machine, and give you output the same way you can also run any other script on a remote machine.

这将在主机上执行Ruby,并以与在远程计算机上运行任何其他脚本相同的方式为您提供输出。

require 'net/ssh'

host = "localhost"
username = "tuxdna"
password = "SOMEAWESOMEPASSWORDHERE"
command = "ruby -e'print :hello'"

class Job
  attr_accessor :stdout
  attr_accessor :stderr
end

job = Job.new

Net::SSH.start(host, username, password: password)  do |ssh|
  job.stdout  = ""
  job.stderr = ""
  ssh.exec! command do |channel, stream, data|
    job.stdout << data if stream == :stdout
    job.stderr << data if stream == :stderr
  end
  # ssh.close
end
p job

OUTPUT:

$ ruby myssh.rb
#<Job:0x00000002bed0a0 @stdout="hello", @stderr="">