extracted send_timeout_set_if_requested(), send_prompt_set_if_requested()
@@ -50,21 +50,18 @@ class Telnet(GenericUnix):
return cmd
def on_new_line(self, line, is_full_line):
+ if self.is_failure_indication(line):
+ self.set_exception(CommandFailure(self, "command failed in line '{}'".format(line)))
+ return
self.send_login_if_requested(line)
self.send_password_if_requested(line)
- if self.is_failure_indication(line):
- self.set_exception(CommandFailure(self, "command failed in line '{}'".format(line)))
- elif self._regex_helper.search_compiled(Telnet._re_has_just_connected, line):
+ if self._regex_helper.search_compiled(Telnet._re_has_just_connected, line):
self.connection.send("")
elif self._regex_helper.search_compiled(self._re_prompt, line):
- if self.set_timeout and not self._sent_timeout:
- self.connection.send("\n" + self.set_timeout)
- self._sent_timeout = True
- elif self.set_prompt and not self._sent_prompt:
- self.connection.send("\n" + self.set_prompt)
- self._sent_prompt = True
- else:
+ sent = self.send_timeout_set_if_requested()
+ sent = sent if sent else self.send_prompt_set_if_requested()
+ if not sent:
if not self._regex_helper.search(Telnet._re_new_line, line):
if self.set_prompt and self.set_timeout:
if self._sent_prompt and self._sent_timeout:
@@ -94,6 +91,20 @@ class Telnet(GenericUnix):
self._sent_login = False
self._sent_password = True
+ def send_timeout_set_if_requested(self):
+ if self.set_timeout and not self._sent_timeout:
+ self.connection.send("\n" + self.set_timeout)
+ self._sent_timeout = True
+ return True # just sent
+ return False
+
+ def send_prompt_set_if_requested(self):
+ if self.set_prompt and not self._sent_prompt:
+ self.connection.send("\n" + self.set_prompt)
+ self._sent_prompt = True
+ return True # just sent
+ return False
+
def is_failure_indication(self, line):
return self._regex_helper.search_compiled(Telnet._re_failed_strings, line)
