def _parse_close_bracket(self, line): if self._regex_helper.search_compiled(Nft._re_close_bracket, line): if self._inner is not None: self._inner = None raise ParsingDone() elif self._outer is not None: self._outer = None raise ParsingDone()
def _settings_after_login(self, line, is_full_line): sent = self._send_after_login_settings(line) if sent: raise ParsingDone() if (not sent) and self._is_target_prompt(line) and (not is_full_line): if self._all_after_login_settings_sent() or self._no_after_login_settings_needed(): if not self.done(): self.set_result({}) raise ParsingDone()
def _parse_open_bracket(self, line): if self._regex_helper.search_compiled(Nft._begin_brakcet, line): if self._outer is None: self._outer = self._regex_helper.group("KEY") self.current_ret[self._outer] = dict() raise ParsingDone() elif self._inner is None: self._inner = self._regex_helper.group("KEY") self.current_ret[self._outer][self._inner] = dict() raise ParsingDone()
def _commands_after_established(self, line, is_full_line): sent = self._send_after_login_settings(line) if sent: raise ParsingDone() if (not sent) and self._is_target_prompt(line): if not is_full_line or self.allowed_newline_after_prompt: if self._all_after_login_settings_sent() or self._no_after_login_settings_needed(): if not self.done(): self.set_result({}) raise ParsingDone()
def _process_embedded_command(self, line, is_full_line): """ Processes embedded command, passes output from device to embedded command. :param line: Line from device :param is_full_line: True if line had new line chars, False otherwise. :return: Nothing but raises ParsingDone if sudo has embedded command object. """ if self.cmd_object: if not self._sent_command_string: self._sent_command_string = True cs = "{}{}".format(self.cmd_object.command_string, self.newline_seq) self.cmd_object.data_received(cs) if is_full_line: line = "{}{}".format(line, self.newline_seq) prev_cmd_timeout = self.cmd_object.timeout self.cmd_object.data_received(line) new_cmd_timeout = self.cmd_object.timeout if self.timeout_from_embedded_command and prev_cmd_timeout != new_cmd_timeout: timedelta = new_cmd_timeout - prev_cmd_timeout self.extend_timeout(timedelta=timedelta) self.current_ret["cmd_ret"] = self.cmd_object.current_ret if self.cmd_object.done(): try: self.cmd_object.result() except Exception as ex: self.set_exception(ex) raise ParsingDone()
def _parse_packets_summary(self, line): if self._regex_helper.search_compiled(Nping._re_packets_sent, line): self.current_ret['STATISTICS'] = dict() if 'STATISTICS' not in self.current_ret else \ self.current_ret['STATISTICS'] self.current_ret['STATISTICS'][ 'PKT_SENT'] = self._converter_helper.to_number( self._regex_helper.group("PKT_SENT"), raise_exception=False) self.current_ret['STATISTICS'][ 'PKT_SENT_SIZE'] = self._converter_helper.to_number( self._regex_helper.group("SENT_SIZE"), raise_exception=False) self.current_ret['STATISTICS'][ 'PKT_SENT_UNIT'] = self._regex_helper.group("SENT_UNIT") self.current_ret['STATISTICS'][ 'PKT_RCVD'] = self._converter_helper.to_number( self._regex_helper.group("PKT_RCVD"), raise_exception=False) self.current_ret['STATISTICS'][ 'PKT_RCVD_SIZE'] = self._converter_helper.to_number( self._regex_helper.group("RCVD_SIZE"), raise_exception=False) self.current_ret['STATISTICS'][ 'PKT_RCVD_UNIT'] = self._regex_helper.group("RCVD_UNIT") self.current_ret['STATISTICS'][ 'LOST_PKT'] = self._converter_helper.to_number( self._regex_helper.group("LOST_PKT"), raise_exception=False) self.current_ret['STATISTICS'][ 'LOST_PERCENTAGE'] = self._converter_helper.to_number( self._regex_helper.group("LOST_PERCENTAGE"), raise_exception=False) raise ParsingDone()
def _host_key_verification(self, line): if self._regex_helper.search_compiled(Ssh._re_host_key_verification_failed, line): if self._hosts_file: self._handle_failed_host_key_verification() else: self.set_exception(CommandFailure(self, "command failed in line '{}'".format(line))) raise ParsingDone()
def _parse_sent_password(self, line): """ Sends password if necessary. :param line: Line from device. :return: None. :raises ParsingDone: if password was sent. """ if (not self._sent_password) and self._is_password_requested(line): try: pwd = self._passwords.pop(0) self._last_password = pwd self.connection.sendline(pwd, encrypt=self.encrypt_password) except IndexError: if self.repeat_password: self.connection.sendline(self._last_password, encrypt=self.encrypt_password) else: self.set_exception( CommandFailure( self, "Password was requested but no more passwords provided." )) self.break_cmd() self._sent_password = True raise ParsingDone()
def _parse_line_data(self, line): """ Parse data from output. :param line: Line from connection. :return: None """ if self._headers: item = dict() max_column = len(self._headers) previous_end_pos = 0 for column_nr in range(max_column): org_start_pos = self._header_pos[column_nr] start_pos = line.find(" ", previous_end_pos) if start_pos > org_start_pos or start_pos < 0: start_pos = org_start_pos if column_nr < max_column - 1: end_pos = self._header_pos[column_nr + 1] end_pos = line.rfind(" ", start_pos, end_pos + 1) else: end_pos = len(line) content = line[start_pos:end_pos] content = content.strip() try: content = self._converter_helper.to_number( value=content, raise_exception=True) except ValueError: pass item[self._headers[column_nr]] = content previous_end_pos = end_pos self.current_ret.append(item) raise ParsingDone()
def _send_password_if_requested(self, line): """ Sends server if requested by server. :param line: Line from device. :return: None but raises ParsingDone if password was sent. """ if (not self._sent) and self._is_password_requested(line): try: pwd = self._passwords.pop(0) self._last_password = pwd self.connection.send("{}{}".format(pwd, self.target_newline), encrypt=self.encrypt_password) except IndexError: if self.repeat_password: self.connection.send("{}{}".format(self._last_password, self.target_newline), encrypt=self.encrypt_password) else: self.set_exception( CommandFailure( self, "Password was requested but no more passwords provided." )) self.break_cmd() self._sent_login = False self._sent = True raise ParsingDone()
def _parse_outlet(self, line): if self._regex_helper.search_compiled(ReadStatus._re_outlet, line): value = self._regex_helper.group("VALUE") if ReadStatus._outlet_id not in self.current_ret: self.current_ret[ReadStatus._outlet_id] = dict() self.current_ret[ReadStatus._outlet_id][self._regex_helper.group("ID")] = value self.current_ret["STATUS"] = value raise ParsingDone()
def _command_error(self, line): if self._regex_helper.search_compiled(LxcAttach._re_command_error, line): self.set_exception( CommandFailure( self, "ERROR: {}".format(self._regex_helper.group("ERROR")))) raise ParsingDone()
def _parse_value(self, line): if self._regex_helper.search_compiled(ReadMeter._re_value, line): value_raw = self._regex_helper.group("VALUE") self.current_ret['TYPE'] = self._regex_helper.group("TYPE") self.current_ret["VALUE_RAW"] = value_raw self.current_ret["VALUE"] = self._converter_helper.to_number( value_raw) raise ParsingDone()
def _ignore_permission_denied(self, line): """ Checks if line contains information about permission denied :param line: Line from device :return: Nothing but raises ParsingDone if regex matches. """ if self._regex_helper.search_compiled(Find._re_permission_denied, line): raise ParsingDone()
def _find_error(self, line): for error_regex in self.error_regexes: if self._regex_helper.search_compiled(error_regex, line): self.set_exception( CommandFailure( self, "Found error regex '{}' in line '{}'".format( error_regex, line))) raise ParsingDone()
def _send_login_if_requested(self, line): if (not self._sent_login ) and self._is_login_requested(line) and self.login: self.connection.send("{}{}".format(self.login, self.target_newline)) self._sent_login = True self._sent_password = False raise ParsingDone()
def _parse_advise(self, line): if self._regex_helper.search(AdviseToChangeYourPassword._re_advise, line): self.current_ret["time"] = datetime.datetime.now() self.current_ret["days"] = int(self._regex_helper.group("DAYS")) self.event_occurred(event_data=self.current_ret) self.current_ret = dict() raise ParsingDone()
def _send_commands_after_establish_connection_if_requested( self, line, is_full_line): if self._telnet_command_mode: if self._send_telnet_commands( line, is_full_line, self.cmds_after_establish_connection): self.connection.send(self.target_newline) self._telnet_command_mode = False raise ParsingDone()
def _parse_file(self, line): """ Add line to value returning by the command :param line: Line from device :return: Nothing but raises ParsingDone """ self.current_ret['RESULT'].append(line) raise ParsingDone()
def _send_password_if_requested(self, line): if (not self._sent_password ) and self._is_password_requested(line) and self.password: self.connection.send("{}{}".format(self.password, self.target_newline), encrypt=self.encrypt_password) self._sent_login = False self._sent_password = True raise ParsingDone()
def _commands_after_established(self, line, is_full_line): """ Performs commands after ssh connection is established and user is logged in. :param line: Line from device. :param is_full_line: True is line contained new line chars, False otherwise. :return: Nothing but raises ParsingDone if all required commands are sent. """ sent = self._send_after_login_settings(line) if sent: raise ParsingDone() if (not sent) and self._is_target_prompt(line): if not is_full_line or self.allowed_newline_after_prompt: if self._all_after_login_settings_sent( ) or self._no_after_login_settings_needed(): if not self.done(): self.set_result({}) raise ParsingDone()
def _settings_after_login(self, line, is_full_line): """ Checks if settings after login are requested and sent. :param line: Line from device. :param is_full_line: True if line had new line chars, False otherwise. :return: Nothing but raises ParsingDone if line has information to handle by this method. """ sent = self._send_after_login_settings(line) if sent: raise ParsingDone() if (not sent) and self._is_target_prompt(line) and ( not is_full_line or self.allowed_newline_after_prompt): if self._all_after_login_settings_sent( ) or self._no_after_login_settings_needed(): if not self.done(): self.set_result(self.current_ret) raise ParsingDone()
def _parse_headers(self, line): if not self._headers: last_pos = 0 self._headers = re.findall(Lsof._re_output_line, line) for header in self._headers: position = line.find(header, last_pos) last_pos = position + len(header) self._header_pos.append(position) raise ParsingDone()
def _parse_uptime(self, line): if self._regex_helper.search_compiled(Uptime._re_uptime_line, line): val = self._regex_helper.group("UPTIME_VAL") users = int(self._regex_helper.group("USERS")) uptime_seconds = self._calculate_seconds(val, line) self.current_ret["UPTIME"] = val self.current_ret["UPTIME_SECONDS"] = uptime_seconds self.current_ret["USERS"] = users raise ParsingDone()
def _parse(self, line): """ Add output to result. :param line: Line from device :return: Nothing but raises ParsingDone """ self.current_ret['RESULT'].append(line) raise ParsingDone()
def _send_password_if_requested(self, line): if (not self._sent_password) and self._is_password_requested(line): try: pwd = self._passwords.pop(0) self.connection.sendline(pwd, encrypt=self.encrypt_password) except IndexError: self.set_exception(CommandFailure(self, "Password was requested but no more passwords provided.")) self._sent_password = True raise ParsingDone()
def _command_failure(self, line): """ Checks if line contains information about failure of command :param line: Line from device :return: Nothing but raises ParsingDone if regex matches. """ if self._regex_helper.search_compiled(Find._re_error, line): self.set_exception(CommandFailure(self, "ERROR: {}".format(self._regex_helper.group("ERROR_MSG_FIND")))) raise ParsingDone()
def _parse_advise(self, line): if self._regex_helper.search(AdviseToChangeYourPassword._re_advise, line): self.current_ret[ "time"] = self._last_recv_time_data_read_from_connection self.current_ret["days"] = int(self._regex_helper.group("DAYS")) self.event_occurred(event_data=self.current_ret) self.current_ret = dict() raise ParsingDone()
def _id_dsa(self, line): """ Checks id dsa. :param line: Line from device. :return: Nothing but raises ParsingDone if regex matches. """ if Ssh._re_id_dsa.search(line): self.connection.sendline("") raise ParsingDone()
def _get_hosts_file_if_displayed(self, line): """ Checks if line from device has info about hosts file. :param line: Line from device. :return: Nothing but raises ParsingDone if regex matches. """ if (self.known_hosts_on_failure is not None) and self._regex_helper.search_compiled(Ssh._re_host_key, line): self._hosts_file = self._regex_helper.group("HOSTS_FILE") raise ParsingDone()