1

I've tried the most popular solution to this, namely using the -t parameter on ssh and running a command before bash initializes. Sadly, the script I am trying to run requires bash variables so this is not an option for me. These are the approaches I've tried:

Approach 1:

Just to show what I was trying above:

ssh -A me@proxy -t 'echo 0 | /usr/local/bin/hop-server.sh <parameters> && bash -l'

It sshes into the proxy but then it spits out a number of missing environment variables from the script.

Approach 2:

Added the script into ~/.bashrc:

if [ "$HOP" = "dev1" ]; then
    /usr/local/bin/hop-server.sh <parameters>
fi

And to connect:

ssh -A me@proxy -t 'HOP=dev1 bash -l'

This does not spit out environment variables as missing, but the hop does not succeed and I am stuck in the proxy:

me@proxy:~$  ~/proxytodev1
Setting environment variables...
Setting project...
Resolving 'dev1'...
Connecting to 123.123.123.123...
Welcome to Ubuntu 14.04.3 LTS (GNU/Linux 3.13.0-63-generic x86_64)

 * Documentation:  https://help.ubuntu.com/

  Get cloud support with Ubuntu Advantage Cloud Guest:
    http://www.ubuntu.com/business/services/cloud

17 packages can be updated.
9 updates are security updates.

me@proxy:~$ 
2
  • What about specifying the variable when executing the script, similar to 1: ssh -A me@proxy -t HOP=<dest> /usr/local/bin/hop-server.sh <params> && bash -l though I'm not positive about the bash -l part which is why I'm keeping this as a comment instead of an answer Commented Oct 20, 2015 at 15:09
  • If you mean all of the environment variables that hop-server depends on, then no it is not possible. A couple dozen variables are being defined that allow this script to run. Commented Oct 20, 2015 at 17:06

2 Answers 2

2

You can use ssh config option SendEnv

ssh -o 'SendEnv HOP' ...

Note that ssh server must be configured to accept it (AcceptEnv in sshd).


Alternatively, you can use the .ssh/environment file on the server side to set variables you want to be present on the ssh connection:

HOP=<destination>

For the server to accept it, you must set to true the PermitUserEnvironment sshd config option.

Sign up to request clarification or add additional context in comments.

6 Comments

Is there any way to know if AcceptEnv is enabled as a non-privileged user? It doesn't seem to be working so I assume it's disabled. The problem with having a config on the server is that I need to be able to connect to multiple servers dev1, dev2, dev3, etc. so it needs to be defined from my computer.
By default, if no AcceptEnv is set on the server, no variable is accepted. However, default sshd config file set LANG and LC_* to be accepted. You can see it in /etc/ssh/sshd_config on the server.
Ah, I was looking for /etc/sysconfig/sshd. The line is AcceptEnv LANG LC_*, so maybe I'm doing something wrong.
If it is there, it means that the server only accepts the LANG variable and ones that start with LC_. Since yours is HOP, it will not be accepted. You will need to edit this file to add HOP and reset the sshd daemon.
Ohhhhh. I get what you mean now. I'll see if I can get my system administrator to sign off on server hopping like this.
|
1

You can pass environment variables to a process with this syntax:

foo=bar ./script.sh

will set foo to the value bar and pass it to the script. You can have as many of those name=value pairs as you want.

Just be careful with variable expansion; depending on which quotes you use, variables are expanded locally and then sent to the remote side or they are sent as is and then expanded remotely.

2 Comments

I can't pass environment variables into the script individually. There are many bash-specific variables and functions that are not being loaded in my first approach numbering in the dozens.
Can you give an example which variable you're missing and where it is defined?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.