Dan Siegel
Dan Siegel

Reputation: 5799

Set environment variables within SSH Session

I have a situation where I have a host machine where I need certain applications installed with side by side versions. Obviously only one can get added to the exports to run by default. As an example we might say it's Python 2.7, Python 3.5, & Python 3.7.

I need to be able to establish an SSH connection to the host, where each connection can set the correct path for the specific version that is required. Is there an easy way to do this. The key here is that each connection cannot affect either the host itself or other connections. Someone running on the host itself shouldn't break because the path was updated by a remote connection.

Upvotes: 0

Views: 1107

Answers (1)

dash-o
dash-o

Reputation: 14452

For the case of multiple (python and other) hierarchies, and assuming that the tools are invokes by the tool name (python ...), prepending the preferred path to the system path will provide a way to specify per-instance tool setting, without having side effect between jobs.

ssh ... 'PATH=/path/to/python3.1/bin:$PATH command'

Depending on the number of tools, and complexity of the setup, you might want to implement this as a wrapper

ssh ... '/path/to/run-with-pkgs python-3.2 pkg2 -- command'

With the pkg-setup script source various config script. Something along the lines of:

run-with-pkgs

#! /bin/bash
while [ $# -gt 0 ] && [ "$1" != "--" ] ; do
   source "/path/to/setup.d/$1.sh"
   shift
done
if [ "$1" = "--" ] ; then
    shift
    exec "$@"
fi

Upvotes: 1

Related Questions