Ruby Net :: SSH Changing Directories Using an Interpolation Variable

I'm new to Ruby, so please forgive me if I missed something obvious.

The problem is that Ruby doesn't seem to do the interpolation variable in 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

Conclusion:

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

Any help is appreciated. Using Ruby 1.9.3p194

+3
source share
2 answers

The problem is that Net :: SSH uses a different shell for every call to exec. The solution is to use stateful shell. There is a stone for this, but it is out of date. Net :: SSH :: Shell via https://github.com/mitchellh/net-ssh-shell

I decided to use Rye to solve this problem. http://code.google.com/p/rye/

+4
source

Net::SSH.start( "10.2.10.1", "botp:)", :password=>"secret=)") do |session| puts session.exec! "ls -la; cd /etc; ls -la" end

: https://www.ruby-forum.com/topic/160890

: "cd" .exec!.
- , , cd exec! exec.

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

0

All Articles