gpt4 book ai didi

Ruby Net::SSH 使用变量插值更改目录

转载 作者:数据小太阳 更新时间:2023-10-29 07:57:01 27 4
gpt4 key购买 nike

我是 Ruby 的新手,所以如果我遗漏了一些明显的东西,请原谅我。

问题是 Ruby 似乎没有在 Net::SSH.exec 中进行变量插值!方法。

VCL_DIR = "/usr/local/etc/varnish/"
host = '0.0.0.0'
Net::SSH.start(host, 'root') do |ssh|
puts "Changing directories to #{VCL_DIR}"
ssh.exec!("cd #{VCL_DIR}")
res = ssh.exec!("pwd")
puts "Server reports current directory as #{res}"
end

输出:

Changing directories to /usr/local/etc/varnish/
Server reports current directory as /root

感谢任何帮助。使用 ruby 1.9.3p194

最佳答案

问题是 Net::SSH 对每个 exec 调用使用不同的 shell。解决方案是使用有状态 shell。有一个 gem 可以做到这一点,但它已经过时了。 Net::SSH::Shell 通过 https://github.com/mitchellh/net-ssh-shell

我选择使用 Rye 来处理这个任务。 http://code.google.com/p/rye/

关于Ruby Net::SSH 使用变量插值更改目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10606290/

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