net-ssh and remote environment

风流意气都作罢 提交于 2019-12-03 21:19:37

问题


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

回答1:


have you tried something like:

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

?




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




回答3:


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.



来源:https://stackoverflow.com/questions/6854055/net-ssh-and-remote-environment

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!