Ruby Net :: SSH使用变量插值更改目录 [英] Ruby Net::SSH Change directories using variable interpolation
问题描述
我对Ruby还是很陌生,所以如果我遗漏了一些明显的东西,请原谅我.
I am fairly new to Ruby, so please forgive me if I am missing something obvious.
问题在于Ruby似乎没有在Net :: SSH.exec中进行变量插值!方法.
The problem is that Ruby doesn't seem to be doing variable interpolation in the Net::SSH.exec! method.
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
Any help is appreciated. Using Ruby 1.9.3p194
推荐答案
问题是Net :: SSH对每个exec调用使用不同的shell.解决方案是使用有状态外壳.有一个宝石可以做到这一点,但它已经过时了. Net :: SSH :: Shell通过 https://github.com/mitchellh/net-ssh-shell
The problem is that Net::SSH uses a different shell for each exec call. The solution is to use stateful shell. There is a gem to do this, but it is outdated. Net::SSH::Shell via https://github.com/mitchellh/net-ssh-shell
我选择使用Rye来处理此任务. http://code.google.com/p/rye/
I chose to use Rye to handle this task. http://code.google.com/p/rye/
这篇关于Ruby Net :: SSH使用变量插值更改目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!