Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass variables to remote script through SSH

I am running scripts on a remote server from a local server via SSH. The script gets copied over using SCP in a first place, then called while being passed some arguments as follows:

scp /path/to/script server.example.org:/another/path/

ssh server.example.org \
MYVAR1=1 \
MYVAR2=2 \
/another/path/script

This works fine and on the remote server, the variables MYVAR1 and MYVAR2 are available with their corresponding value.

The issue is that these scripts are in constant development which requires the SSH command to be changed every-time a variable is renamed, added, or removed.

I'm looking for a way of passing all the local environment variables to the remote script (since MYVAR1 and MYVAR2 are actually local environment variables) which would address the SSH command maintenance issue.

Since MYVAR1=1 \ and MYVAR1=1 \ are lines which follow the env command output I tried replacing them with the actual command as follows:

ssh server.example.org \
`env`
/another/path/script

This seems to work for "simple" env output lines (e.g. SHELL=/bin/bash or LOGNAME=sysadmin), however I get errors for more "complex" output lines (e.g. LS_COLORS=rs=0:di=01;34:ln=01;[...] which gives errors such as -bash: 34:ln=01: command not found ). I can get rid of these errors by unsetting the variables corresponding to those complex output lines before running the SSH command (e.g. unset LS_COLORS, then ssh [...]) however I don't find this very solution very reliable.

Q: Does anybody know how to pass all the local environment variables to a remote script via SSH?

PS: the local environment variables are not environment variables available on the remote machine so I cannot use this solution.

Update with solution

I ended using sed to format the env command output from VAR=VALUE to VAR="VALUE" (and concatenating all lines in to 1) which prevents bash from interpreting some of the output as commands and fixes my problem.

ssh server.example.org \
`env | sed 's/\([^=]*\)=\(.*\)/\1="\2"/' | tr '\n' ' '` \
"/another/path/script"
like image 438
Max Avatar asked Jul 19 '11 11:07

Max


People also ask

Can I use environment variables in SSH config?

Some of the keywords in the ssh config file support environment variables, but most do not. Fortunately IndentyFile is one of them. You need to use ${HOME} rather than $HOME in the file. You can also use tokens , such as %d to refer to the home directory.


1 Answers

I happened to read the sshd_config man page unrelated to this and found the option AcceptEnv:

AcceptEnv Specifies what environment variables sent by the client will be copied into the session's environ(7). See SendEnv in ssh_config(5) for how to configure the client. Note that envi- ronment passing is only supported for protocol 2. Variables are specified by name, which may contain the wildcard characters *' and?'. Multiple environment variables may be separated by whitespace or spread across multiple AcceptEnv directives. Be warned that some environment variables could be used to bypass restricted user environments. For this reason, care should be taken in the use of this directive. The default is not to accept any environment variables.

Maybe you could use this with AcceptEnv: *? I haven't got a box with sshd handy, but try it out!

like image 155
carlpett Avatar answered Oct 05 '22 06:10

carlpett