summaryrefslogtreecommitdiff
path: root/paramiko/channel.py
diff options
context:
space:
mode:
authorPhilip Lorenz <philip@bithub.de>2014-09-21 12:31:40 +0200
committerJeff Forcier <jeff@bitprophet.org>2016-12-05 18:43:38 -0800
commit28c8be18c25e9d10f8e4759e489949f1e22d6346 (patch)
tree1b436c140475b86450a3555d3a37915c7f3fa303 /paramiko/channel.py
parenta29d22de793a634537057840919f061b71283a86 (diff)
downloadparamiko-28c8be18c25e9d10f8e4759e489949f1e22d6346.tar.gz
Support transmission of environment variables
The SSH protocol allows the client to transmit environment variables to the server. This is particularly useful if the user wants to modify the environment of an executed command without having to reexecute the actual command from a shell. This patch extends the Client and Channel interface to allow the transmission of environment variables to the server side. In order to use this feature the SSH server must accept environment variables from the client (e.g. the AcceptEnv configuration directive of OpenSSH). FROM BITPROPHET: backport cherry-pick to 1.x line
Diffstat (limited to 'paramiko/channel.py')
-rw-r--r--paramiko/channel.py41
1 files changed, 41 insertions, 0 deletions
diff --git a/paramiko/channel.py b/paramiko/channel.py
index 3a05bdc4..7735e1f1 100644
--- a/paramiko/channel.py
+++ b/paramiko/channel.py
@@ -283,6 +283,47 @@ class Channel (ClosingContextManager):
m.add_int(height_pixels)
self.transport._send_user_message(m)
+ @open_only
+ def update_environment_variables(self, environment):
+ """
+ Updates this channel's environment. This operation is additive - i.e.
+ the current environment is not reset before the given environment
+ variables are set.
+
+ :param dict environment: a dictionary containing the name and respective
+ values to set
+ :raises SSHException:
+ if any of the environment variables was rejected by the server or
+ the channel was closed
+ """
+ for name, value in environment.items():
+ try:
+ self.set_environment_variable(name, value)
+ except SSHException as e:
+ raise SSHException("Failed to set environment variable \"%s\"." % name, e)
+
+ @open_only
+ def set_environment_variable(self, name, value):
+ """
+ Set the value of an environment variable.
+
+ :param str name: name of the environment variable
+ :param str value: value of the environment variable
+
+ :raises SSHException:
+ if the request was rejected or the channel was closed
+ """
+ m = Message()
+ m.add_byte(cMSG_CHANNEL_REQUEST)
+ m.add_int(self.remote_chanid)
+ m.add_string('env')
+ m.add_boolean(True)
+ m.add_string(name)
+ m.add_string(value)
+ self._event_pending()
+ self.transport._send_user_message(m)
+ self._wait_for_event()
+
def exit_status_ready(self):
"""
Return true if the remote process has exited and returned an exit