5

I want to execute some remote command on my server using net-ssh library.

I have the following example:

Net::SSH::start(host, user, options = {:keys => '~/.ssh/id_rsa'}) do |ssh|
  puts ssh.exec!("echo $PATH")
  ssh.loop
end

The result is: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games

My problem is that I have not my PATH loaded as it should be.

I should also have some RVM paths, and custom paths defining into my .zshrc.

How could I change this behavior to let net-ssh to use my .zshrc to load my default environment ?

Solution:

puts ssh.exec!("source ~/.zshrc; echo $PATH")
Arkan
  • 6,196
  • 3
  • 38
  • 54

3 Answers3

7

have you tried something like:

ssh.exec!("source /home/you/.zshrc")
puts ssh.exec!("echo $PATH")

?

Vlad Khomich
  • 5,820
  • 1
  • 27
  • 39
  • 1
    Thanks for your contribution. Each exec! command are independent. That means that the environment is reset each time. I've updated my question explain how I solved the problem. But thanks for your help :-) – Arkan Jul 29 '11 at 03:33
2

Found a way around this problem, actually. It's quite nice solution. Thanks to Vald for telling me that every exec is independent of itself, so then you can just use && to link the commands together.

For example, I have a ruby script that is runnable, and I'm using zsh, so I just use this:

ssh.exec("source .zshrc && ./backup.rb")

Works like a charm! Of course you can also be on the safe side and use the full path or ~, but it gets the job done.

Edit: Sorry, also just saw you put a solution above. Is there a difference between using ; and &&?

Mark Ursino
  • 31,209
  • 11
  • 51
  • 83
Allen
  • 794
  • 1
  • 6
  • 19
1

Don't use Net/SSH for running multiple commands.

Try net-ssh-session.

Commands are not independent and it loads your user's env vars.

gcstr
  • 1,466
  • 1
  • 21
  • 45