Starting with version 2.1.0 2016-12-09, you can add a dictionary of environment variables in exec_command :
import paramiko paramiko.util.log_to_file("paramiko.log") ssh = paramiko.SSHClient() k = paramiko.RSAKey.from_private_key_file("<private_key_file>") ssh.connect(<hostname>,username=<username>,pkey=k) env_dict={"LC_TELEPHONE":"ET_HOME","LC_MEASUREMENT":"MILES_APART"} stdin , stdout, stderr = ssh.exec_command('echo $LC_TELEPHONE; echo "..."; echo $LC_MEASUREMENT',environment=env_dict) print stdout.read()
output:
ET_HOME ... MILES_APART
But why did I choose LC_TELEPHONE and LC_MEASUREMENT? Since these are two of several environments that the target ssh configurator allows you to configure:
grep AcceptEnv /etc/ssh/sshd_config
output:
AcceptEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES AcceptEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT AcceptEnv LC_IDENTIFICATION LC_ALL
In other words, this does not work:
env_dict={"HELLO":"WORLD","GOODBYE":"CRUEL_WORLD"} stdin , stdout, stderr = ssh.exec_command("echo $HELLO; echo '...'; echo $GOODBYE") print stdout.read()
output:
...
As the documentation warns, environment variables are silently rejected http://docs.paramiko.org/en/2.1/api/client.html http://docs.paramiko.org/en/2.1/api/channel.html#paramiko.channel .Channel.set_environment_variable
If you cannot control the sshd configuration of the target server by inserting environment variables into the file and using it:
stdin , stdout, stderr = ssh.exec_command("cat .set_env;source .set_env; echo $HELLO; echo '...'; echo $GOODBYE") print stdout.read()
output:
# begin .set_env HELLO="WORLD" GOODBYE="CRUEL_WORLD"