Arkan
Arkan

Reputation: 6236

net-ssh and remote environment

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")

Upvotes: 5

Views: 2154

Answers (3)

gcstr
gcstr

Reputation: 1527

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.

Upvotes: 1

Allen
Allen

Reputation: 834

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 &&?

Upvotes: 2

Vlad Khomich
Vlad Khomich

Reputation: 5880

have you tried something like:

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

?

Upvotes: 7

Related Questions