I am fairly new to Ruby, so please forgive me if I am missing something obvious.
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
Output:
Changing directories to /usr/local/etc/varnish/
Server reports current directory as /root
Any help is appreciated. Using Ruby 1.9.3p194
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
I chose to use Rye to handle this task. http://code.google.com/p/rye/
Net::SSH.start( "10.2.10.1", "botp:)", :password=>"secret=)") do |session| puts session.exec! "ls -la; cd /etc; ls -la" end
Reference: https://www.ruby-forum.com/topic/160890
Net::SSH.start( "10.2.10.1", "botp:)", :password=>"secret=)") do |session| puts session.exec!("ls -la; cd /etc; ls -la") puts session.exec!("ls -la") end