Best Python code snippet using avocado_python
ssh.py
Source:ssh.py  
...86        else:87            options += (('PasswordAuthentication', 'yes'),88                        ('NumberOfPasswordPrompts', '1'),)89        return self._ssh_cmd(options, ('-T', '-n'))90    def _create_ssh_askpass(self):91        """92        Writes a simple program that complies with SSH_ASKPASS93        This basically writes to stdout the password given94        """95        script = "#!%s\nprint('%s')" % (sys.executable, self.password)96        fd, path = tempfile.mkstemp()97        os.write(fd, script.encode())98        os.fchmod(fd, stat.S_IRUSR | stat.S_IXUSR)99        os.close(fd)100        return path101    def _master_command(self, command):102        cmd = self._ssh_cmd(self.DEFAULT_OPTIONS, ('-O', command))103        result = process.run(cmd, ignore_status=True)104        return result.exit_status == 0105    def _check(self):106        return self._master_command('check')107    def cleanup_master(self):108        """Removes master file if exists."""109        if self.control_master:110            os.unlink(self.control_master)111    def connect(self):112        """113        Establishes the connection to the remote endpoint114        On this implementation, it means creating the master connection,115        which is a process that will live while and be used for subsequent116        commands.117        :returns: whether the connection is successfully established118        :rtype: bool119        """120        if SSH_CLIENT_BINARY is None:121            return False122        if not self._check():123            cmd = shlex.split(self._master_connection())124            if self.password is not None:125                ssh_askpass_path = self._create_ssh_askpass()126                env = {'DISPLAY': 'FAKE_VALUE_TO_SATISFY_SSH',127                       'SSH_ASKPASS': ssh_askpass_path}128                # pylint: disable=W1509129                master = subprocess.Popen(cmd,130                                          stdin=subprocess.DEVNULL,131                                          stdout=subprocess.DEVNULL,132                                          stderr=subprocess.DEVNULL,133                                          env=env,134                                          preexec_fn=os.setsid)135            else:136                master = subprocess.Popen(cmd,137                                          stdin=subprocess.DEVNULL,138                                          stdout=subprocess.DEVNULL,139                                          stderr=subprocess.DEVNULL)...test_utils_ssh.py
Source:test_utils_ssh.py  
...36        self.assertIn(" -o 'PasswordAuthentication=no'", master_connection)37    def test_master_connection_ssh_askpass_script(self):38        password = 'PASSWORD'39        session = ssh.Session('hostname', user='user', password=password)40        ssh_askpass_path = session._create_ssh_askpass()41        ssh_askpass_password = process.run(ssh_askpass_path)42        os.unlink(ssh_askpass_path)43        self.assertEqual(ssh_askpass_password.stdout_text.rstrip(), password)44    def test_no_ssh_client_binary(self):45        session = ssh.Session('hostname')46        with unittest.mock.patch('avocado.utils.ssh.SSH_CLIENT_BINARY', None):47            self.assertFalse(session.connect())48if __name__ == '__main__':...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!!
