简体   繁体   English

使用ssh运行远程ruby脚本(以及传递参数)

[英]run a remote ruby script (pass parameters as well) using ssh

I want to connect to a remote host and run a ruby script on the remote host. 我想连接到远程主机并在远程主机上运行ruby脚本。 Following is the code that I am using - 以下是我正在使用的代码-

ssh = Net::SSH.start(host, user)
args = "some argument" //can be any data type, list, string, anything
results = conn.exec!('ruby runfile.rb args')

It's not passing args to the file in this case. 在这种情况下,它没有将args传递给文件。 I have also tried using double quotes instead of single quotes. 我也尝试使用双引号而不是单引号。 How do I send the parameters as well? 如何发送参数?

  • the name of the connection variable must be consistent (ssh ≠ conn). 连接变量的名称必须一致(ssh≠conn)。

  • You need to send the content of args instead of the string "args" : 您需要发送args的内容,而不是字符串"args"

  • Double quotes are needed for the #{...} syntax to work. #{...}语法需要双引号才能起作用。 Or use 'ruby runfile.rb ' + args if you prefer. 或者,根据需要使用'ruby runfile.rb ' + args

  • Use # instead of // to comment Ruby code. 使用#代替//注释Ruby代码。

  • Use .shellescape to harden against unwanted (accidental or malicious) effects in the remote shell. 使用.shellescape可以加固远程外壳中的有害(偶然或恶意)影响。

This code does work: 这段代码确实有效:

require 'net/ssh'
require 'shellwords'

ssh = Net::SSH.start(host, user)
args = "some argument".shellescape #can be any data type, list, string, anything
results = ssh.exec!("ruby runfile.rb #{args}")
puts results

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM