From 5f25e08691f9c1b49863cb112968c7ea1868c7d8 Mon Sep 17 00:00:00 2001 From: Jeff Forcier Date: Mon, 12 Dec 2016 15:29:06 -0800 Subject: Revert "Add timeout to Transport.start_client()" This reverts commit 5c7f30be9737f73fd024a23f5db0b6a7578026b6. --- paramiko/client.py | 2 +- paramiko/transport.py | 11 +++-------- 2 files changed, 4 insertions(+), 9 deletions(-) diff --git a/paramiko/client.py b/paramiko/client.py index 69666360..40cd5cf2 100644 --- a/paramiko/client.py +++ b/paramiko/client.py @@ -335,7 +335,7 @@ class SSHClient (ClosingContextManager): t.set_log_channel(self._log_channel) if banner_timeout is not None: t.banner_timeout = banner_timeout - t.start_client(timeout=timeout) + t.start_client() ResourceManager.register(self, t) server_key = t.get_remote_server_key() diff --git a/paramiko/transport.py b/paramiko/transport.py index 7906c9f2..71d5109e 100644 --- a/paramiko/transport.py +++ b/paramiko/transport.py @@ -1,5 +1,4 @@ # Copyright (C) 2003-2007 Robey Pointer -# Copyright (C) 2003-2007 Robey Pointer # # This file is part of paramiko. # @@ -446,7 +445,7 @@ class Transport (threading.Thread, ClosingContextManager): # We need the FQDN to get this working with SSPI self.gss_host = socket.getfqdn(gss_host) - def start_client(self, event=None, timeout=None): + def start_client(self, event=None): """ Negotiate a new SSH2 session as a client. This is the first step after creating a new `.Transport`. A separate thread is created for protocol @@ -457,7 +456,7 @@ class Transport (threading.Thread, ClosingContextManager): be triggered. On failure, `is_active` will return ``False``. (Since 1.4) If ``event`` is ``None``, this method will not return until - negotiation is done. On success, the method returns normally. + negotation is done. On success, the method returns normally. Otherwise an SSHException is raised. After a successful negotiation, you will usually want to authenticate, @@ -474,9 +473,6 @@ class Transport (threading.Thread, ClosingContextManager): :param .threading.Event event: an event to trigger when negotiation is complete (optional) - :param float timeout: - a timeout, in seconds, for SSH2 session negotiation (optional) - :raises SSHException: if negotiation fails (and no ``event`` was passed in) """ @@ -490,7 +486,6 @@ class Transport (threading.Thread, ClosingContextManager): # synchronous, wait for a result self.completion_event = event = threading.Event() self.start() - max_time = time.time() + timeout if timeout is not None else None while True: event.wait(0.1) if not self.active: @@ -498,7 +493,7 @@ class Transport (threading.Thread, ClosingContextManager): if e is not None: raise e raise SSHException('Negotiation failed.') - if event.is_set() or (timeout is not None and time.time() >= max_time): + if event.is_set(): break def start_server(self, event=None, server=None): -- cgit v1.2.3 From d4a5806d23e95cc386d88a361956d0e0c1df9fb6 Mon Sep 17 00:00:00 2001 From: Jeff Forcier Date: Mon, 12 Dec 2016 15:33:01 -0800 Subject: Remove code re #398 from 2.0 branch, as it's feature work --- paramiko/channel.py | 52 ------------------------------------------------- paramiko/client.py | 23 ++-------------------- sites/www/changelog.rst | 10 ---------- tests/test_client.py | 45 ------------------------------------------ 4 files changed, 2 insertions(+), 128 deletions(-) diff --git a/paramiko/channel.py b/paramiko/channel.py index 52b5d849..3a05bdc4 100644 --- a/paramiko/channel.py +++ b/paramiko/channel.py @@ -283,58 +283,6 @@ class Channel (ClosingContextManager): m.add_int(height_pixels) self.transport._send_user_message(m) - @open_only - def update_environment(self, environment): - """ - Updates this channel's remote shell environment. - - .. note:: - This operation is additive - i.e. the current environment is not - reset before the given environment variables are set. - - .. warning:: - Servers may silently reject some environment variables; see the - warning in `set_environment_variable` for details. - - :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: - err = "Failed to set environment variable \"{0}\"." - raise SSHException(err.format(name), e) - - @open_only - def set_environment_variable(self, name, value): - """ - Set the value of an environment variable. - - .. warning:: - The server may reject this request depending on its ``AcceptEnv`` - setting; such rejections will fail silently (which is common client - practice for this particular request type). Make sure you - understand your server's configuration before using! - - :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(False) - m.add_string(name) - m.add_string(value) - self.transport._send_user_message(m) - def exit_status_ready(self): """ Return true if the remote process has exited and returned an exit diff --git a/paramiko/client.py b/paramiko/client.py index 40cd5cf2..ebf21b08 100644 --- a/paramiko/client.py +++ b/paramiko/client.py @@ -398,14 +398,7 @@ class SSHClient (ClosingContextManager): self._agent.close() self._agent = None - def exec_command( - self, - command, - bufsize=-1, - timeout=None, - get_pty=False, - environment=None, - ): + def exec_command(self, command, bufsize=-1, timeout=None, get_pty=False): """ Execute a command on the SSH server. A new `.Channel` is opened and the requested command is executed. The command's input and output @@ -418,14 +411,6 @@ class SSHClient (ClosingContextManager): Python :param int timeout: set command's channel timeout. See `Channel.settimeout`.settimeout - :param dict environment: - a dict of shell environment variables, to be merged into the - default environment that the remote command executes within. - - .. warning:: - Servers may silently reject some environment variables; see the - warning in `.Channel.set_environment_variable` for details. - :return: the stdin, stdout, and stderr of the executing command, as a 3-tuple @@ -436,8 +421,6 @@ class SSHClient (ClosingContextManager): if get_pty: chan.get_pty() chan.settimeout(timeout) - if environment: - chan.update_environment(environment) chan.exec_command(command) stdin = chan.makefile('wb', bufsize) stdout = chan.makefile('r', bufsize) @@ -445,7 +428,7 @@ class SSHClient (ClosingContextManager): return stdin, stdout, stderr def invoke_shell(self, term='vt100', width=80, height=24, width_pixels=0, - height_pixels=0, environment=None): + height_pixels=0): """ Start an interactive shell session on the SSH server. A new `.Channel` is opened and connected to a pseudo-terminal using the requested @@ -457,14 +440,12 @@ class SSHClient (ClosingContextManager): :param int height: the height (in characters) of the terminal window :param int width_pixels: the width (in pixels) of the terminal window :param int height_pixels: the height (in pixels) of the terminal window - :param dict environment: the command's environment :return: a new `.Channel` connected to the remote shell :raises SSHException: if the server fails to invoke a shell """ chan = self._transport.open_session() chan.get_pty(term, width, height, width_pixels, height_pixels) - chan.update_environment_variables(environment or {}) chan.invoke_shell() return chan diff --git a/sites/www/changelog.rst b/sites/www/changelog.rst index 36460eff..72ae1548 100644 --- a/sites/www/changelog.rst +++ b/sites/www/changelog.rst @@ -45,16 +45,6 @@ Changelog signature. Caught by ``@Score_Under``. * :bug:`681` Fix a Python3-specific bug re: the handling of read buffers when using ``ProxyCommand``. Thanks to Paul Kapp for catch & patch. -* :feature:`398 (1.18+)` Add an ``environment`` dict argument to - `Client.exec_command ` (plus the - lower level `Channel.update_environment - ` and - `Channel.set_environment_variable - ` methods) which - implements the ``env`` SSH message type. This means the remote shell - environment can be set without the use of ``VARNAME=value`` shell tricks, - provided the server's ``AcceptEnv`` lists the variables you need to set. - Thanks to Philip Lorenz for the pull request. * :support:`819 backported (>=1.15,<2.0)` Document how lacking ``gmp`` headers at install time can cause a significant performance hit if you build PyCrypto from source. (Most system-distributed packages already have this enabled.) diff --git a/tests/test_client.py b/tests/test_client.py index 32d9ac60..63ff9297 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -82,16 +82,6 @@ class NullServer (paramiko.ServerInterface): return False return True - def check_channel_env_request(self, channel, name, value): - if name == 'INVALID_ENV': - return False - - if not hasattr(channel, 'env'): - setattr(channel, 'env', {}) - - channel.env[name] = value - return True - class SSHClientTest (unittest.TestCase): @@ -379,38 +369,3 @@ class SSHClientTest (unittest.TestCase): password='pygmalion', ) self._test_connection(**kwargs) - - def test_update_environment(self): - """ - Verify that environment variables can be set by the client. - """ - threading.Thread(target=self._run).start() - - self.tc = paramiko.SSHClient() - self.tc.set_missing_host_key_policy(paramiko.AutoAddPolicy()) - self.assertEqual(0, len(self.tc.get_host_keys())) - self.tc.connect(self.addr, self.port, username='slowdive', password='pygmalion') - - self.event.wait(1.0) - self.assertTrue(self.event.isSet()) - self.assertTrue(self.ts.is_active()) - - target_env = {b'A': b'B', b'C': b'd'} - - self.tc.exec_command('yes', environment=target_env) - schan = self.ts.accept(1.0) - self.assertEqual(target_env, getattr(schan, 'env', {})) - schan.close() - - # Cannot use assertRaises in context manager mode as it is not supported - # in Python 2.6. - try: - # Verify that a rejection by the server can be detected - self.tc.exec_command('yes', environment={b'INVALID_ENV': b''}) - except SSHException as e: - self.assertTrue('INVALID_ENV' in str(e), - 'Expected variable name in error message') - self.assertTrue(isinstance(e.args[1], SSHException), - 'Expected original SSHException in exception') - else: - self.assertFalse(False, 'SSHException was not thrown.') -- cgit v1.2.3