def recv(self, size): """ Read from the standard output of the forked program. :param int size: how many chars should be read :return: the length of the read content, as an `int` """ try: start = datetime.now() while len(self.buffer) < size: if self.timeout is not None: elapsed = (datetime.now() - start).microseconds timeout = self.timeout * 1000 * 1000 # to microseconds if elapsed >= timeout: raise socket.timeout() r, w, x = select([self.process.stdout], [], [], 0.0) if r and r[0] == self.process.stdout: b = os.read(self.process.stdout.fileno(), 1) # Store in class-level buffer for persistence across # timeouts; this makes us act more like a real socket # (where timeouts don't actually drop data.) self.buffer.append(b) result = ''.join(self.buffer) self.buffer = [] return result except socket.timeout: raise # socket.timeout is a subclass of IOError except IOError, e: raise ProxyCommandFailure(' '.join(self.cmd), e.strerror)
def recv(self, size): """ Read from the standard output of the forked program. :param int size: how many chars should be read :return: the string of bytes read, which may be shorter than requested """ try: buffer = b"" start = time.time() while len(buffer) < size: select_timeout = None if self.timeout is not None: elapsed = time.time() - start if elapsed >= self.timeout: raise socket.timeout() select_timeout = self.timeout - elapsed r, w, x = select([self.process.stdout], [], [], select_timeout) if r and r[0] == self.process.stdout: buffer += os.read(self.process.stdout.fileno(), size - len(buffer)) return buffer except socket.timeout: if buffer: # Don't raise socket.timeout, return partial result instead return buffer raise # socket.timeout is a subclass of IOError except IOError as e: raise ProxyCommandFailure(" ".join(self.cmd), e.strerror)
def send(self, content): """ Write the content received from the SSH client to the standard input of the forked command. :param str content: string to be sent to the forked command """ try: self.process.stdin.write(content) except IOError, e: # There was a problem with the child process. It probably # died and we can't proceed. The best option here is to # raise an exception informing the user that the informed # ProxyCommand is not working. raise ProxyCommandFailure(' '.join(self.cmd), e.strerror)
def recv(self, size): """ Read from the standard output of the forked program. @param size: how many chars should be read @type size: int @return: the length of the read content @rtype: int """ try: return os.read(self.process.stdout.fileno(), size) except IOError: e = sys.exc_info()[1] raise ProxyCommandFailure(' '.join(self.cmd), e.strerror)
def recv(self, size): """ Read from the standard output of the forked program. :param int size: how many chars should be read :return: the string of bytes read, which may be shorter than requested """ buffer = b'' try: if sys.platform == 'win32': # windows does not support select() on pipes (only on sockets) return os.read(self.process.stdout.fileno(), size) start = time.time() while len(buffer) < size: if self.closed: if buffer: return buffer raise EOFError() select_timeout = None if self.timeout is not None: elapsed = (time.time() - start) if elapsed >= self.timeout: raise socket.timeout() select_timeout = self.timeout - elapsed r = poll_read([self.process.stdout], select_timeout) if r and r[0] == self.process.stdout: buffer += os.read(self.process.stdout.fileno(), size - len(buffer)) return buffer except socket.timeout: if buffer: # Don't raise socket.timeout, return partial result instead return buffer raise # socket.timeout is a subclass of IOError except IOError as e: raise ProxyCommandFailure(self.cmd, e.strerror)
def recv(self, size): """ Read from the standard output of the forked program. On Windows select() only works on sockets, so the loop will fail on this platform. Therefore, use a simple blocking call in this case. :param int size: how many chars should be read :return: the string of bytes read, which may be shorter than requested """ try: if sys.platform == 'win32': return os.read(self.process.stdout.fileno(), size) buffer = b"" start = time.time() while len(buffer) < size: select_timeout = None if self.timeout is not None: elapsed = time.time() - start if elapsed >= self.timeout: raise socket.timeout() select_timeout = self.timeout - elapsed r, w, x = select([self.process.stdout], [], [], select_timeout) if r and r[0] == self.process.stdout: buffer += os.read(self.process.stdout.fileno(), size - len(buffer)) return buffer except socket.timeout: if buffer: # Don't raise socket.timeout, return partial result instead return buffer raise # socket.timeout is a subclass of IOError except IOError as e: raise ProxyCommandFailure(" ".join(self.cmd), e.strerror)
import pickle import pytest from paramiko import RSAKey from paramiko.ssh_exception import ( BadAuthenticationType, PartialAuthentication, ChannelException, BadHostKeyException, ProxyCommandFailure, ) @pytest.mark.parametrize(['exc'], [ (BadAuthenticationType("Bad authentication type", ["ok", "also-ok"]), ), (PartialAuthentication(["ok", "also-ok"]), ), (BadHostKeyException("myhost", RSAKey.generate(2048), RSAKey.generate(2048)), ), (ProxyCommandFailure("nc servername 22", 1), ), (ChannelException(17, "whatever"), ), ]) def test_ssh_exception_strings(exc): assert isinstance(str(exc), str) assert isinstance(repr(exc), str) if type(exc) != BadHostKeyException: ne = pickle.loads(pickle.dumps(exc)) assert type(ne) == type(exc)
def test_ProxyCommandFailure(self): exc = ProxyCommandFailure("man squid", 7) expected = 'ProxyCommand("man squid") returned nonzero exit status: 7' assert str(exc) == expected