gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 17:52:57 25 4
gpt4 key购买 nike

目前我正在使用反引号方法调用远程脚本,它可以工作,但感觉不对而且令人讨厌...

`ssh user@host $(echo "~/bin/command \\\"#{parameter}\\\"")`

谁能给我一个更好的表达方式?

非常感谢。

最佳答案

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

#!/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}`

否则,您可以使用 net-ssh图书馆,然后做这样的事情:

#!/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

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

关于ruby - 通过 ssh 调用远程命令,使用 Ruby,改进语法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20672733/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com