How to use handle_prompts method in autotest

Best Python code snippet using autotest_python Github


Full Screen

...38 self.msg = msg39 self.output = output40 def __str__(self):41 return "%s (output: %r)" % (self.msg, self.output)42def handle_prompts(session, username, password, prompt, timeout=50,43 debug=False):44 """45 Connect to a remote host (guest) using SSH or Telnet or other else.46 Wait for questions and provide answers. If timeout expires while47 waiting for output from the child -- fail.48 :param session: An Expect or ShellSession instance to operate on49 :param username and password: to send in reply to a login50 :paramprompt: The shell prompt that indicates a successful login51 :raise LoginTimeoutError52 :rasie LoginAuthenticationError53 :raise LoginProcessTerminatedError54 :raise LoginError: If some other error occurs55 """56 password_prompt_count = 057 login_prompt_count = 058 while True:59 try:60 match, text = session.read_until_last_line_matches(61 [r"[Aa]re you sure", r"[Pp]assword:\s*",62 r"(?<![Ll]ast).*[Ll]ogin:\s*$",63 r"[Cc]onnection.*closed", r"[Cc]onnection.*refused",64 r"[Pp]lease wait",65 r"[Ww]arning", r"[Ee]nter.*username",66 r"[Ee]nter.*password", prompt67 ],68 timeout=timeout, internal_timeout=0.5)69 if match == 0: # are you sure you want to continue connecting70 if debug:71 logging.debug("Got 'Are you sure...', sending 'yes'")72 session.sendline("yes")73 continue74 elif match == 1 or match == 8: # "password"75 if password_prompt_count == 0:76 if debug:77 logging.debug("Got password prompt, sending '%s'",78 password)79 session.sendline(password)80 password_prompt_count += 181 continue82 else:83 raise LoginAuthenticationError(84 "Got password prompt twice", text)85 elif match == 2 or match == 7: # "login:"86 if login_prompt_count == 0 and password_prompt_count == 0:87 if debug:88 logging.debug("Got username prompt; sending '%s'",89 username)90 session.sendline(username)91 login_prompt_count += 192 continue93 else:94 if login_prompt_count > 0:95 msg = "Got username prompt twice"96 else:97 msg = "Got username prompt after password prompt"98 raise LoginAuthenticationError(msg, text)99 elif match == 3: # "Connection closed"100 raise LoginError("client said 'connection closed'", text)101 elif match == 4: # "Connection refused"102 raise LoginError("client said 'connection refused'", text)103 elif match == 5: # "Please wait"104 if debug:105 logging.debug("Got 'Please wait'")106 timeout = 30107 continue108 elif match == 6: # "warning added Rsa"109 if debug:110 logging.debug("Got 'warning added RSA to known host list'")111 continue112 elif match == 9: # prompt113 if debug:114 logging.debug("Got shell prompt -- logged in")115 break116 except aexpect.ExpectTimeoutError, e:117 raise LoginTimeoutError(e.output)118 except aexpect.ExpectProcessTerminatedError, e:119 raise LoginProcessTerminatedError(e.status, e.output)120def remote_login(client, host, port, username, password, prompt, linesep="\n",121 log_filename=None, timeout=10, interface=None):122 """123 log into a remote host(guest) using SSH/Telnet/Netcat.124 :param client : the client to use ('ssh', 'telnet' or the other nc)125 :param host : Hostname or IP address126 :param username : Username (if required)127 :param password : Password (if required)128 :param prompt : shell prompt (regular expression)129 :param linesep : The line separator to use when sending lines (e.g.130 '\\n' or '\\r\\n')131 :param log_filename : if specified, log all output to this file132 :param timeout : the maximal time duration (in seconds) to wait for133 each step of the login procedure134 :interface : The interface the neighbours attach to (only use135 when using the ipv6 linklocal address.)136 :raise LoginError : If using ipv6 linklocal but not assign a interface137 that the neighbour attache138 :raise LoginBadClientError: If an unknown client is requested139 :raise Whatever handle_prompts() raises140 :return : A ShellSession object141 """142 if host and host.lower().startswith("fe800"):143 if not interface:144 raise LoginError("When using ipv6 linklocal an interface must be"145 " assigned")146 host = "%s%%%s" % (host, interface)147 if client == "ssh":148 cmd = ("ssh -o UserKnownHostsFile=/dev/null -o"149 " PreferredAuthentications=password -p %s %s@%s "150 % (port, username, host))151 elif client == "telnet":152 cmd = "telent -l %s %s %s" % (username, host, port)153 else:154 raise LoginBadClientError(client)155 logging.debug("Login Command: '%s'", cmd)156 # need to realize the aexpect.ShellSession157 session = aexpect.ShellSession(cmd, linesep=linesep, prompt=prompt)158 try:159 handle_prompts(session, username, password, prompt, timeout, True)160 except Exception:161 session.close()162 raise163 if log_filename:164 session.set_output_func(utils.log_line)165 session.set_output_params((log_filename,))166 session.set_log_file(log_filename)...

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:


You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run autotest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?