Malte Forkel
2009-Dec-01 18:53 UTC
How to modify environment variables before executing a command
Hi, I'd like to access and modify shell environment variables before executing a command on a remote host via ssh. Unfortenatly, I can't figure out the proper way to do this... It seems, standard shell configuration files (e.g. ~/.bash_profile) are not sourced before ssh executes a command. Of course I could explicitly source them, e.g. ssh user at host 'source ~/.bash_profile; command' But I don't like that because it delegates a server side configuration task to the clients. Is there a way to achieve this in a more general fashion on a per user basis? I tried both ~/.ssh/environment and ~/.ssh/rc, as documented in the man page. But ~/.ssh/environment only works for static variables and I didn't manage to export variables defined in ~/.ssh/rc. For example, if I have these files in my home directory on server # ~/.ssh/environment VAR1=$(date) and # ~/.ssh/rc VAR2=$(date) export VAR2 then executing env from the client via ssh malte at server env | grep VAR will result in VAR1=$(date) i.e., VAR1 is passed literally while VAR2 is not available. And where are the variables defined that I see when exexuting ssh user at server env Among them are SHELL=/bin/bash PWD=/home/malte Thanks in advance, Malte
Malte Forkel
2009-Dec-02 18:25 UTC
How to modify environment variables before executing a command
>> I'd like to access and modify shell environment variables before >> executing a command on a remote host via ssh. Unfortenatly, I can't >> figure out the proper way to do this...Thanks for the suggestions I received! Karlan T. Mitchell schrieb:> ~/.bashrc I believe. Profile is for local terminals.....pretty sure at > least. Hope this helpsJim Rees schrieb:> The bash man page suggests you should be able to do this: > ~/.ssh/environment > BASH_ENV=.bash_profileI extended my test scenario to four configuration files on the server: # .ssh/environment VAR1=$(date) BASH_ENV=~/.ssh/ssh_profile # .ssh/rc echo ".ssh/rc" VAR2=$(date) export VAR2 # .bashrc echo ".bashrc" VAR3=$(date) export VAR3 # ~/.ssh/ssh_profile echo ".ssh/ssh_profile" VAR4=$(date) export VAR4 Now, when I execute $ ssh server env | grep -E -e '^\.' -e VAR I get .ssh/ssh_profile .ssh/rc .bashrc VAR1=$(date) VAR3=Mi 2. Dez 18:16:33 CET 2009 So all four files are read or sourced, respectively. I hadn't noticed that ~/.bashrc is sourced because it starts with # If not running interactively, don't do anything [ -z "$PS1" ] && return which terminates it in the ssh context. But above that test I could insert a test for some flag set in .ssh/environment and setup the shell variables I need for ssh. Still, that seems a little awkward as .bashrc is sourced every time any shell is created with the default configuration files. I'm still looking for a more ssh-specific solution. Unfortenately, the variables exported from the two ssh-specific files .ssh/rc and .ssh/ssh_profile don't make it into the environment. Why is that? A secondary problem with using BASH_ENV could be that its value is evaluated by every non-interactive shell. So, if I use ssh to login to the server and then run a script with bash, the file specified by BASH_ENV is sourced. For example, with the above setup: $ echo "echo foo" > say_foo.sh $ bash say_foo.sh .ssh/ssh_profile foo Any more suggestions on how to modify environment variables before executing a command via ssh? Thanks, Malte
Peter Stuge
2009-Dec-02 20:06 UTC
How to modify environment variables before executing a command
Malte Forkel wrote:> Any more suggestions on how to modify environment variables before > executing a command via ssh?You can use SendEnv in the client configuration. But the server must also AcceptEnv those same variables. //Peter
Douglas E. Engert
2009-Dec-02 20:26 UTC
How to modify environment variables before executing a command
Malte Forkel wrote:> > Any more suggestions on how to modify environment variables before > executing a command via ssh? >SSH will set the SSH_CLIENT, SSH_TTY and SSH_CONNECTION So you can tell if this is an ssh session. ~./.bashrc is always run, and $- on my Ubuntu is hBc the first time. If its interactive, ~/.bash_profile is then run and $- is himBH. Some systems might run /etc/profile as well, so tests are needed try echo $- So your .bashrc could test for SSH_CLIENT,and $- for c and set what it wants. It could also set MY_ENV_HAS_BEEN_SET=1 so you only run you code once at the start of a session.> Thanks, Malte > > > > > _______________________________________________ > openssh-unix-dev mailing list > openssh-unix-dev at mindrot.org > https://lists.mindrot.org/mailman/listinfo/openssh-unix-dev > >-- Douglas E. Engert <DEEngert at anl.gov> Argonne National Laboratory 9700 South Cass Avenue Argonne, Illinois 60439 (630) 252-5444