Best Python code snippet using tempest_python
ssh.py
Source:ssh.py  
...82                time.sleep(bsleep)83    def _is_timed_out(self, start_time):84        return (time.time() - self.timeout) > start_time85    @staticmethod86    def _can_system_poll():87        return hasattr(select, 'poll')88    def exec_command(self, cmd):89        """Execute the specified command on the server90        Note that this method is reading whole command outputs to memory, thus91        shouldn't be used for large outputs.92        :param str cmd: Command to run at remote server.93        :returns: data read from standard output of the command.94        :raises: SSHExecCommandFailed if command returns nonzero95                 status. The exception contains command status stderr content.96        :raises: TimeoutException if cmd doesn't end when timeout expires.97        """98        ssh = self._get_ssh_connection()99        transport = ssh.get_transport()100        channel = transport.open_session()101        channel.fileno()  # Register event pipe102        channel.exec_command(cmd)103        channel.shutdown_write()104        exit_status = channel.recv_exit_status()105        # If the executing host is linux-based, poll the channel106        if self._can_system_poll():107            out_data_chunks = []108            err_data_chunks = []109            poll = select.poll()110            poll.register(channel, select.POLLIN)111            start_time = time.time()112            while True:113                ready = poll.poll(self.channel_timeout)114                if not any(ready):115                    if not self._is_timed_out(start_time):116                        continue117                    raise exceptions.TimeoutException(118                        "Command: '{0}' executed on host '{1}'.".format(119                            cmd, self.host))120                if not ready[0]:  # If there is nothing to read....Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
