Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

net-ssh and remote environment

Tags:

ruby

net-ssh

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")
like image 236
Arkan Avatar asked Feb 23 '23 10:02

Arkan


2 Answers

have you tried something like:

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

?

like image 164
Vlad Khomich Avatar answered Mar 30 '23 08:03

Vlad Khomich


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

like image 43
Allen Avatar answered Mar 30 '23 08:03

Allen