Beispiel #1
0
    def __init__(self,
                 connection,
                 prompt=None,
                 newline_chars=None,
                 options=None,
                 path=None,
                 runner=None):
        """
        Unix command ls

        :param connection: Moler connection to device, terminal when command is executed.
        :param prompt: prompt (on system where command runs).
        :param newline_chars: Characters to split lines - list.
        :param options: Options of unix ls command
        :param path: path to list
        :param runner: Runner to run command.
        """
        super(Ls, self).__init__(connection=connection,
                                 prompt=prompt,
                                 newline_chars=newline_chars,
                                 runner=runner)
        self._converter_helper = ConverterHelper()
        self.current_ret["files"] = dict()
        # Parameters defined by calling the command
        self.options = options
        self.path = path
Beispiel #2
0
 def __init__(self,
              connection,
              prompt=None,
              newline_chars=None,
              options=None,
              runner=None):
     super(Ls, self).__init__(connection=connection,
                              prompt=prompt,
                              newline_chars=newline_chars,
                              runner=runner)
     self._converter_helper = ConverterHelper()
     # Parameters defined by calling the command
     self.options = options
     self.matched = 0
Beispiel #3
0
def test_converterhelper_number_wrong_format():
    from moler.util.converterhelper import ConverterHelper
    converter = ConverterHelper.get_converter_helper()
    with pytest.raises(ValueError):
        converter.to_number(value="abc", raise_exception=True)
    val = converter.to_number(value="abc", raise_exception=False)
    assert val == 0
Beispiel #4
0
def test_converterhelper_seconds():
    from moler.util.converterhelper import ConverterHelper
    converter = ConverterHelper.get_converter_helper()
    value, value_in_units, unit = converter.to_seconds_str("3m")
    assert 180 == value
    assert 3 == value_in_units
    assert 'm' == unit
Beispiel #5
0
def test_converterhelper_m():
    from moler.util.converterhelper import ConverterHelper
    converter = ConverterHelper.get_converter_helper()
    bytes_value, value_in_units, unit = converter.to_bytes(".3m", False)
    assert 300000 == bytes_value
    assert 0.3 == value_in_units
    assert 'm' == unit
Beispiel #6
0
def test_converterhelper_k():
    from moler.util.converterhelper import ConverterHelper
    converter = ConverterHelper.get_converter_helper()
    bytes_value, value_in_units, unit = converter.to_bytes("2.5K")
    assert 2560 == bytes_value
    assert 2.5 == value_in_units
    assert 'k' == unit
Beispiel #7
0
 def __init__(self,
              connection,
              destination,
              options=None,
              prompt=None,
              newline_chars=None,
              runner=None):
     """
     Traceroute command.
     :param connection: moler connection to device, terminal when command is executed.
     :param destination: address.
     :param options: options of traceroute command for unix.
     :param prompt: prompt on system where traceroute is executed.
     :param newline_chars: characters to split lines.
     :param runner: Runner to run command
     """
     super(Traceroute, self).__init__(connection=connection,
                                      prompt=prompt,
                                      newline_chars=newline_chars,
                                      runner=runner)
     # Parameters defined by calling the command
     self.options = options
     self.destination = destination
     self._converter_helper = ConverterHelper.get_converter_helper()
     self.current_ret['hops'] = list()
Beispiel #8
0
def test_converterhelper_number():
    from moler.util.converterhelper import ConverterHelper
    converter = ConverterHelper.get_converter_helper()
    val = converter.to_number(value="1")
    assert 1 == val
    val = converter.to_number(value="0.1")
    assert val == 0.1
Beispiel #9
0
    def __init__(self,
                 connection=None,
                 options='',
                 prompt=None,
                 newline_chars=None,
                 runner=None):
        """
        Represents Unix command ps.

        :param connection: moler connection to device, terminal where command is executed
        :param options: ps command options as string
        :param prompt: prompt (on system where command runs).
        :param newline_chars: characters to split lines
        :param runner: Runner to run command
        """
        super(Ps, self).__init__(connection=connection,
                                 prompt=prompt,
                                 newline_chars=newline_chars,
                                 runner=runner)
        self.current_ret = list()
        self.options = options
        self._headers = None
        self._header_pos = None
        self.ret_required = False
        self._converter_helper = ConverterHelper.get_converter_helper()
Beispiel #10
0
 def __init__(self,
              connection,
              destination,
              options=None,
              prompt=None,
              newline_chars=None,
              runner=None):
     """
     Nping command.
     :param connection: moler connection to device, terminal when command is executed.
     :param destination: address (IP v4 or v6) of unit to ping.
     :param options: options of ping command for unix.
     :param prompt: prompt on system where ping is executed.
     :param newline_chars: characters to split lines.
     :param runner: Runner to run command
     """
     super(Nping, self).__init__(connection=connection,
                                 prompt=prompt,
                                 newline_chars=newline_chars,
                                 runner=runner)
     # Parameters defined by calling the command
     self.options = options
     self.destination = destination
     self._converter_helper = ConverterHelper.get_converter_helper()
     self._current_statistics = None
Beispiel #11
0
    def __init__(self,
                 connection,
                 target,
                 measurement,
                 outlet=None,
                 output_format=None,
                 prompt=">",
                 newline_chars=None,
                 runner=None):
        """
        Class for command read meter for PDU Aten device.

        :param connection: connection to device.
        :param outlet: outlet id.
        :param output_format: format of output. Maybe be None for simple format.
        :param prompt: expected prompt sending by device after command execution. Maybe String or compiled re.
        :param newline_chars:  new line chars on device (a list).
        :param runner: runner to run command.
        """
        super(ReadMeter, self).__init__(connection=connection,
                                        prompt=prompt,
                                        newline_chars=newline_chars,
                                        runner=runner)
        self.outlet = outlet
        self.format = output_format
        self.target = target
        self.measurement = measurement
        self._converter_helper = ConverterHelper.get_converter_helper()
Beispiel #12
0
 def __init__(self,
              connection,
              ip,
              is_ping=False,
              options=None,
              prompt=None,
              newline_chars=None,
              runner=None):
     """
     :param connection: Moler connection to device, terminal when command is executed.
     :param ip: IP address of host.
     :param is_ping: If True then skip host discovery.
     :param options: Options of command nmap.
     :param prompt: prompt (on system where command runs).
     :param newline_chars: Characters to split lines - list.
     :param runner: Runner to run command.
     """
     super(Nmap, self).__init__(connection=connection,
                                prompt=prompt,
                                newline_chars=newline_chars,
                                runner=runner)
     self.options = options
     self.ip = ip
     self.is_ping = is_ping
     self.timeout = 120  # Time in seconds
     self._converter_helper = ConverterHelper.get_converter_helper()
Beispiel #13
0
    def __init__(self,
                 connection,
                 prompt=None,
                 newline_chars=None,
                 runner=None,
                 is_ipv6=False,
                 addr_get=None,
                 addr_from=None):
        """
        Unix command ip route.

        :param connection: Moler connection to device, terminal when command is executed.
        :param prompt: prompt (on system where command runs).
        :param newline_chars: Characters to split lines - list.
        :param runner: Runner to run command.
        :param is_ipv6: Set True if IP v6 or False for IP v4.
        :param addr_get: Address get, parameter of unix ip route command.
        :param addr_from: From address, parameter of unix ip route command.
        """
        super(IpRoute, self).__init__(connection=connection,
                                      prompt=prompt,
                                      newline_chars=newline_chars,
                                      runner=runner)
        self._converter_helper = ConverterHelper()
        # Parameters defined by calling the command
        self.is_ipv6 = is_ipv6
        self.addr_get = addr_get
        self.addr_from = addr_from
        self.current_ret["VIA"] = dict()
        self.current_ret["ALL"] = []
        self.current_ret["ADDRESS"] = dict()
Beispiel #14
0
    def __init__(self,
                 connection,
                 options,
                 prompt=None,
                 newline_chars=None,
                 runner=None):
        super(Iperf, self).__init__(connection=connection,
                                    prompt=prompt,
                                    newline_chars=newline_chars,
                                    runner=runner)
        self.options = options
        self.current_ret['CONNECTIONS'] = dict()
        self.current_ret['INFO'] = list()

        # private values
        self._connection_dict = dict()
        self._list_of_connections = dict()
        self._converter_helper = ConverterHelper()
Beispiel #15
0
 def __init__(self,
              connection,
              prompt=None,
              newline_chars=None,
              runner=None):
     super(Df, self).__init__(connection=connection,
                              prompt=prompt,
                              newline_chars=newline_chars,
                              runner=runner)
     self._converter_helper = ConverterHelper()
Beispiel #16
0
    def __init__(self,
                 connection,
                 options=None,
                 prompt=None,
                 newline_chars=None,
                 runner=None):
        """
        Unix command du.

        :param connection: moler connection to device, terminal when command is executed
        :param options: Options of unix du command
        :param prompt: expected prompt sending by device after command execution
        :param newline_chars: Characters to split lines
        :param runner: Runner to run command
        """
        super(Du, self).__init__(connection=connection,
                                 prompt=prompt,
                                 newline_chars=newline_chars,
                                 runner=runner)
        self.options = options
        self._converter_helper = ConverterHelper()
Beispiel #17
0
    def __init__(self, connection, options=None, prompt=None, newline_chars=None, runner=None):
        """
        :param connection: Moler connection to device, terminal when command is executed.
        :param options: uptime unix command options
        :param prompt: prompt (on system where command runs).
        :param newline_chars: Characters to split lines - list.
        :param runner: Runner to run command.
        """
        super(Uptime, self).__init__(connection=connection, prompt=prompt, newline_chars=newline_chars, runner=runner)
        # Parameters defined by calling the command
        self.options = options

        self._converter_helper = ConverterHelper.get_converter_helper()
Beispiel #18
0
 def __init__(self, connection, options=None, prompt=None, newline_chars=None, runner=None, n=1):
     """
     Top command.
     :param connection: moler connection to device, terminal when command is executed.
     :param options: options of top command for unix.
     :param prompt: prompt on system where ping is executed.
     :param newline_chars: characters to split lines.
     :param runner: Runner to run command.
     :param n: Specifies number of measurements.
     """
     super(Top, self).__init__(connection=connection, prompt=prompt, newline_chars=newline_chars, runner=runner)
     self.options = options
     self._processes_list_headers = list()
     self.current_ret = dict()
     self.n = n
     self._converter_helper = ConverterHelper.get_converter_helper()
Beispiel #19
0
 def __init__(self,
              connection,
              options,
              prompt=None,
              newline_chars=None,
              runner=None):
     super(Grep, self).__init__(connection=connection,
                                prompt=prompt,
                                newline_chars=newline_chars,
                                runner=runner)
     self._convert_helper = ConverterHelper()
     # Parameters defined by calling the command
     self.options = options
     self.ret_required = False
     self.current_ret["LINES"] = []
     self.line_no = 0
Beispiel #20
0
 def __init__(self,
              connection,
              prompt=None,
              newline_chars=None,
              runner=None,
              is_ipv6=False,
              addr_get=None,
              addr_from=None):
     super(IpRoute, self).__init__(connection=connection,
                                   prompt=prompt,
                                   newline_chars=newline_chars,
                                   runner=runner)
     self._converter_helper = ConverterHelper()
     # Parameters defined by calling the command
     self.is_ipv6 = is_ipv6
     self.addr_get = addr_get
     self.addr_from = addr_from
     self.current_ret["VIA"] = dict()
     self.current_ret["ALL"] = []
     self.current_ret["ADDRESS"] = dict()
Beispiel #21
0
def test_converterhelper_seconds_ms():
    from moler.util.converterhelper import ConverterHelper
    converter = ConverterHelper.get_converter_helper()
    value = converter.to_seconds(0.408, "ms")
    assert pytest.approx(0.000408, 0.000001) == value
Beispiel #22
0
class Ls(GenericUnixCommand):
    _re_files_list = re.compile(r"\S{2,}")
    _re_total = re.compile(r"total\s+(\d+\S*)")
    _re_long = re.compile(
        r"([\w-]{10})\s+(\d+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S.*\S)\s+(\S+)\s*$")
    _re_long_links = re.compile(
        r"([\w-]{10})\s+(\d+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S.*\S)\s+(\S+)\s+->\s+(\S+)\s*$"
    )

    def __init__(self,
                 connection,
                 prompt=None,
                 newline_chars=None,
                 options=None,
                 runner=None):
        super(Ls, self).__init__(connection=connection,
                                 prompt=prompt,
                                 newline_chars=newline_chars,
                                 runner=runner)
        self._converter_helper = ConverterHelper()
        # Parameters defined by calling the command
        self.options = options
        self.matched = 0

    def build_command_string(self):
        cmd = "ls"
        if self.options:
            cmd = cmd + " " + self.options
        return cmd

    def on_new_line(self, line, is_full_line):
        if not is_full_line:
            return super(Ls, self).on_new_line(line, is_full_line)
        if self._regex_helper.search_compiled(Ls._re_total, line):
            if "total" not in self.current_ret:
                self.current_ret["total"] = dict()
            self.current_ret["total"]["raw"] = self._regex_helper.group(1)
            self.current_ret["total"][
                "bytes"] = self._converter_helper.to_bytes(
                    self._regex_helper.group(1))[0]
        elif self._regex_helper.search_compiled(Ls._re_long_links, line):
            self._add_new_file_long(True)
        elif self._regex_helper.search_compiled(Ls._re_long, line):
            self._add_new_file_long(False)
        elif self._regex_helper.search_compiled(Ls._re_files_list, line):
            files = line.split()
            if "files" not in self.current_ret:
                self.current_ret["files"] = dict()
            for filename in files:
                self.current_ret["files"][filename] = dict()
                self.current_ret["files"][filename]["name"] = filename
        return super(Ls, self).on_new_line(line, is_full_line)

    def _add_new_file_long(self, islink):
        filename = self._regex_helper.group(7)
        if "files" not in self.current_ret:
            self.current_ret["files"] = dict()
        self.current_ret["files"][filename] = dict()
        self.current_ret["files"][filename][
            "permissions"] = self._regex_helper.group(1)
        self.current_ret["files"][filename]["hard_links_count"] = int(
            self._regex_helper.group(2))
        self.current_ret["files"][filename][
            "owner"] = self._regex_helper.group(3)
        self.current_ret["files"][filename][
            "group"] = self._regex_helper.group(4)
        self.current_ret["files"][filename][
            "size_raw"] = self._regex_helper.group(5)
        self.current_ret["files"][filename]["size_bytes"] = \
            self._converter_helper.to_bytes(self._regex_helper.group(5))[0]
        self.current_ret["files"][filename]["date"] = self._regex_helper.group(
            6)
        self.current_ret["files"][filename]["name"] = self._regex_helper.group(
            7)
        if islink:
            self.current_ret["files"][filename][
                "link"] = self._regex_helper.group(8)

    def _get_types(self, requested_type):
        if not self.done():
            raise ResultNotAvailableYet("Command not executed already")
        requested_type = requested_type.lower()
        ret = dict()
        result = self.result()
        if 'files' in result:
            for file_name in result["files"]:
                file_dict = result["files"][file_name]
                permissions = file_dict["permissions"]
                current_type = permissions[0]
                if requested_type == current_type:
                    ret[file_name] = file_dict
        return ret

    def get_dirs(self):
        return self._get_types('d')

    def get_links(self):
        return self._get_types('l')

    def get_files(self):
        return self._get_types('-')
Beispiel #23
0
class Ls(GenericUnixCommand):
    """Unix command ls"""

    _re_files_list = re.compile(r"\S{2,}")
    _re_total = re.compile(r"total\s+(\d+\S*)")
    _re_long = re.compile(
        r"([\w-]{10}[\.\+]?)\s+(\d+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S.*\S)\s+(\S+)\s*$"
    )
    _re_long_links = re.compile(
        r"([\w-]{10}[\.\+]?)\s+(\d+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S.*\S)\s+(\S+)\s+->\s+(\S+)\s*$"
    )

    def __init__(self,
                 connection,
                 prompt=None,
                 newline_chars=None,
                 options=None,
                 path=None,
                 runner=None):
        """
        Unix command ls

        :param connection: Moler connection to device, terminal when command is executed.
        :param prompt: prompt (on system where command runs).
        :param newline_chars: Characters to split lines - list.
        :param options: Options of unix ls command
        :param path: path to list
        :param runner: Runner to run command.
        """
        super(Ls, self).__init__(connection=connection,
                                 prompt=prompt,
                                 newline_chars=newline_chars,
                                 runner=runner)
        self._converter_helper = ConverterHelper()
        self.current_ret["files"] = dict()
        # Parameters defined by calling the command
        self.options = options
        self.path = path

    def get_dirs(self):
        """
        Returns only directories (folders) from command output

        :return: Dict, key is item, value is parsed information about item
        """
        return self._get_types('d')

    def get_links(self):
        """
        Returns only links from command output

        :return: Dict, key is item, value is parsed information about item
        """
        return self._get_types('l')

    def get_files(self):
        """
        Returns only files from command output

        :return: Dict, key is item, value is parsed information about item
        """
        return self._get_types('-')

    def build_command_string(self):
        """
        Builds command string from parameters passed to object.

        :return: String representation of command to send over connection to device.
        """
        cmd = "ls"
        if self.options:
            cmd = "{} {}".format(cmd, self.options)
        if self.path:
            cmd = "{} {}".format(cmd, self.path)
        return cmd

    def on_new_line(self, line, is_full_line):
        """
        Processes line from output form connection/device.

        :param line: Line to process, can be only part of line. New line chars are removed from line.
        :param is_full_line: True if line had new line chars, False otherwise
        :return: None
        """
        if is_full_line:
            try:
                self._parse_total(line)
                self._parse_long_links(line)
                self._parse_long_file(line)
                self._parse_files_list(line)
            except ParsingDone:
                pass
        return super(Ls, self).on_new_line(line, is_full_line)

    def _parse_files_list(self, line):
        """
        Parses list of files.

        :param line: Line from device.
        :return: None but raises ParsingDone if matches success.
        """
        if self._regex_helper.search_compiled(Ls._re_files_list, line):
            files = line.split()
            for filename in files:
                self.current_ret["files"][filename] = dict()
                self.current_ret["files"][filename]["name"] = filename
            raise ParsingDone()

    def _parse_long_file(self, line):
        """
        Parses line with long information with file or directory.

        :param line: Line from device.
        :return: None but raises ParsingDone if matches success.
        """
        if self._regex_helper.search_compiled(Ls._re_long, line):
            self._add_new_file_long(False)
            raise ParsingDone()

    def _parse_long_links(self, line):
        """
        Parses line with long information with link.

        :param line: Line from device.
        :return: None but raises ParsingDone if matches success.
        """
        if self._regex_helper.search_compiled(Ls._re_long_links, line):
            self._add_new_file_long(True)
            raise ParsingDone()

    def _parse_total(self, line):
        """
        Parses information about total in ls output.

        :param line: Line from device.
        :return: None but raises ParsingDone if matches success.
        """
        if self._regex_helper.search_compiled(Ls._re_total, line):
            if "total" not in self.current_ret:
                self.current_ret["total"] = dict()
            self.current_ret["total"]["raw"] = self._regex_helper.group(1)
            self.current_ret["total"][
                "bytes"] = self._converter_helper.to_bytes(
                    self._regex_helper.group(1))[0]
            raise ParsingDone()

    def _add_new_file_long(self, islink):
        """
        Adds parsed output to command ret.

        :param islink: True if parsed output is link or False otherwise.
        :return: None.
        """
        filename = self._regex_helper.group(7)
        self.current_ret["files"][filename] = dict()
        self.current_ret["files"][filename][
            "permissions"] = self._regex_helper.group(1)
        self.current_ret["files"][filename]["hard_links_count"] = int(
            self._regex_helper.group(2))
        self.current_ret["files"][filename][
            "owner"] = self._regex_helper.group(3)
        self.current_ret["files"][filename][
            "group"] = self._regex_helper.group(4)
        self.current_ret["files"][filename][
            "size_raw"] = self._regex_helper.group(5)
        self.current_ret["files"][filename]["size_bytes"] = \
            self._converter_helper.to_bytes(self._regex_helper.group(5))[0]
        self.current_ret["files"][filename]["date"] = self._regex_helper.group(
            6)
        self.current_ret["files"][filename]["name"] = self._regex_helper.group(
            7)
        if islink:
            self.current_ret["files"][filename][
                "link"] = self._regex_helper.group(8)

    def _get_types(self, requested_type):
        """
        Method to return only object of specific type.
        :param requested_type: Type of object. Available values: 'd', 'l', '-'
        :return: Dict of files
        """
        if not self.done():
            raise ResultNotAvailableYet("Command not executed already")
        requested_type = requested_type.lower()
        ret = dict()
        result = self.result()
        if 'files' in result:
            for file_name in result["files"]:
                file_dict = result["files"][file_name]
                permissions = file_dict["permissions"]
                current_type = permissions[0]
                if requested_type == current_type:
                    ret[file_name] = file_dict
        return ret
Beispiel #24
0
class Iperf(GenericUnixCommand):
    def __init__(self,
                 connection,
                 options,
                 prompt=None,
                 newline_chars=None,
                 runner=None):
        super(Iperf, self).__init__(connection=connection,
                                    prompt=prompt,
                                    newline_chars=newline_chars,
                                    runner=runner)
        self.options = options
        self.current_ret['CONNECTIONS'] = dict()
        self.current_ret['INFO'] = list()

        # private values
        self._connection_dict = dict()
        self._list_of_connections = dict()
        self._converter_helper = ConverterHelper()

    def build_command_string(self):
        cmd = 'iperf ' + str(self.options)
        return cmd

    def on_new_line(self, line, is_full_line):
        if is_full_line:
            try:
                self._command_failure(line)
                self._parse_connection_name_and_id(line)
                self._parse_headers(line)
                self._parse_connection_info(line)
                self._parse_connection_headers(line)
            except ParsingDone:
                pass
        return super(Iperf, self).on_new_line(line, is_full_line)

    _re_command_failure = re.compile(
        r"(?P<FAILURE_MSG>.*failed.*|.*error.*|.*command not found.*|.*iperf:.*)"
    )

    def _command_failure(self, line):
        if self._regex_helper.search_compiled(Iperf._re_command_failure, line):
            self.set_exception(
                CommandFailure(
                    self, "ERROR: {}".format(
                        self._regex_helper.group("FAILURE_MSG"))))
            raise ParsingDone

    _re_connection_name_and_id = re.compile(
        r"(?P<ID>\[\s*\d*\])\s*(?P<ID_NAME>.*port\s*\d*\s*connected with.*)")

    def _parse_connection_name_and_id(self, line):
        if self._regex_helper.search_compiled(Iperf._re_connection_name_and_id,
                                              line):
            connection_id = self._regex_helper.group("ID")
            connection_name = self._regex_helper.group("ID_NAME")
            connection_dict = {connection_id: connection_name}
            self._connection_dict.update(connection_dict)
            raise ParsingDone

    _re_headers = re.compile(r"(?P<HEADERS>\[\s+ID\].*)")

    def _parse_headers(self, line):
        if self._regex_helper.search_compiled(Iperf._re_headers, line):
            matched = line.split()[2:]
            self._list_of_connections = [header.strip() for header in matched]
            raise ParsingDone

    _re_connection_info = re.compile(
        r"(?P<CONNECTION_ID>\[\s*\d*\])\s*(?P<CONNECTION_REPORT>.*)")

    def _parse_connection_info(self, line):
        if self._regex_helper.search_compiled(Iperf._re_connection_info, line):
            connection_id = self._regex_helper.group("CONNECTION_ID")
            connection_report = self._regex_helper.group(
                "CONNECTION_REPORT").split('  ')
            connection_report = [
                report.strip() for report in connection_report
            ]
            connection_name = self._connection_dict[connection_id]
            info_dict = dict(zip(self._list_of_connections, connection_report))
            self._normalise_units(connection_report, info_dict)
            self._update_current_ret(connection_name, info_dict)
            raise ParsingDone

    def _update_current_ret(self, connection_name, info_dict):
        if connection_name in self.current_ret['CONNECTIONS']:
            self.current_ret['CONNECTIONS'][connection_name].append(info_dict)
        else:
            connection_dict = {connection_name: [info_dict]}
            self.current_ret['CONNECTIONS'].update(connection_dict)

    _re_ornaments = re.compile(r"(?P<ORNAMENTS>----*|\[\s*ID\].*)",
                               re.IGNORECASE)

    def _parse_connection_headers(self, line):
        if not self._regex_helper.search_compiled(Iperf._re_ornaments, line):
            self.current_ret['INFO'].append(line.strip())
            raise ParsingDone

    def _normalise_units(self, report, dictionary_to_update):
        for (index, item) in enumerate(report):
            if 'Bytes' in item or 'bits' in item:
                header = self._list_of_connections[index]
                raw_bites = self._converter_helper.to_bytes(item)[0]
                new_column_title = header + " Raw"
                read_bites = dictionary_to_update[header]
                dictionary_to_update[header] = raw_bites
                dictionary_to_update.update({new_column_title: read_bites})
Beispiel #25
0
def test_converterhelper_seconds_wrong_unit():
    from moler.util.converterhelper import ConverterHelper
    converter = ConverterHelper.get_converter_helper()
    with pytest.raises(ValueError):
        converter.to_seconds_str("3UU")
Beispiel #26
0
class Du(GenericUnixCommand):
    """Unix command du."""
    def __init__(self,
                 connection,
                 options=None,
                 prompt=None,
                 newline_chars=None,
                 runner=None):
        """
        Unix command du.

        :param connection: moler connection to device, terminal when command is executed
        :param options: Options of unix du command
        :param prompt: expected prompt sending by device after command execution
        :param newline_chars: Characters to split lines
        :param runner: Runner to run command
        """
        super(Du, self).__init__(connection=connection,
                                 prompt=prompt,
                                 newline_chars=newline_chars,
                                 runner=runner)
        self.options = options
        self._converter_helper = ConverterHelper()

    def build_command_string(self):
        """
        Build command string from parameters passed to object.

        :return: String representation of command to send over connection to device.
        """
        cmd = "du"
        if self.options:
            cmd = '{} {}'.format(cmd, self.options)
        return cmd

    def on_new_line(self, line, is_full_line):
        """
        Put your parsing code here.

        :param line: Line to process, can be only part of line. New line chars are removed from line.
        :param is_full_line: True if line had new line chars, False otherwise
        :return: Nothing
        """
        if is_full_line:
            try:
                self._parse_du(line)
                self._parse_du_bytes(line)
            except ParsingDone:
                pass
        return super(Du, self).on_new_line(line, is_full_line)

    # 4       ./directory/directory2
    _re_du = re.compile(r"(?P<NUMBER>^\d+)\s+(?P<DIRECTORY>\S*$)")

    def _parse_du(self, line):
        if self._regex_helper.search_compiled(Du._re_du, line):
            self.current_ret[self._regex_helper.group('DIRECTORY')] = dict()
            self.current_ret[self._regex_helper.group(
                'DIRECTORY')]["size_bytes"] = 1024 * int(
                    self._regex_helper.group('NUMBER'))
            self.current_ret[self._regex_helper.group(
                'DIRECTORY')]["size_raw"] = self._regex_helper.group('NUMBER')
            raise ParsingDone

    # 4.0K    ./directory2/directory3
    _re_du_bytes = re.compile(
        r"(?P<NUMBER>^\d+\.?\d*\w)\s+(?P<DIRECTORY>\S*$)")

    def _parse_du_bytes(self, line):
        if self._regex_helper.search_compiled(Du._re_du_bytes, line):
            self.current_ret[self._regex_helper.group('DIRECTORY')] = dict()
            self.current_ret[self._regex_helper.group('DIRECTORY')]["size_bytes"] = \
                self._converter_helper.to_bytes(self._regex_helper.group('NUMBER'))[0]
            self.current_ret[self._regex_helper.group(
                'DIRECTORY')]["size_raw"] = self._regex_helper.group('NUMBER')
            raise ParsingDone
Beispiel #27
0
    def __init__(self,
                 connection,
                 host,
                 login=None,
                 password=None,
                 newline_chars=None,
                 prompt=None,
                 runner=None,
                 port=0,
                 expected_prompt=r'^>\s*',
                 set_timeout=r'export TMOUT=\"2678400\"',
                 set_prompt=None,
                 term_mono="TERM=xterm-mono",
                 encrypt_password=True,
                 target_newline="\n",
                 allowed_newline_after_prompt=False,
                 repeat_password=True,
                 failure_exceptions_indication=None,
                 prompt_after_login=None,
                 send_enter_after_connection=True,
                 username=None):
        """
        Base Moler class of Unix commands telnet and ssh.

        :param connection: moler connection to device, terminal when command is executed.
        :param host: address of telnet server.
        :param login: login to telnet server.
        :param password: password to telnet server.
        :param port: port to listen on server.
        :param prompt: prompt on start system (where command telnet starts).
        :param expected_prompt: prompt on server (where command telnet connects).
        :param set_timeout: Command to set timeout after telnet connects.
        :param set_prompt: Command to set prompt after telnet connects.
        :param term_mono: Params to set ssh mono connection (useful in script).
        :param newline_chars: characters to split lines.
        :param encrypt_password: If True then * will be in logs when password is sent, otherwise plain text.
        :param runner: Runner to run command.
        :param target_newline: newline chars on remote system where ssh connects.
        :param allowed_newline_after_prompt: If True then newline chars may occur after expected (target) prompt.
        :param repeat_password: If True then repeat last password if no more provided. If False then exception is set.
        :param failure_exceptions_indication: String with regex or regex object to omit failure even if failed string
         was found.
        :param prompt_after_login: prompt after login before send export PS1. If you do not change prompt exporting PS1
         then leave it None.
        :param send_enter_after_connection: set True to send new line char(s) after connection is established, False
         otherwise.
        """
        super(GenericTelnetSsh, self).__init__(
            connection=connection,
            prompt=prompt,
            newline_chars=newline_chars,
            runner=runner,
            expected_prompt=expected_prompt,
            set_timeout=set_timeout,
            set_prompt=set_prompt,
            target_newline=target_newline,
            allowed_newline_after_prompt=allowed_newline_after_prompt,
            prompt_after_login=prompt_after_login)

        self.timeout = 90
        # Parameters defined by calling the command
        self._re_failure_exceptions_indication = None
        if failure_exceptions_indication:
            self._re_failure_exceptions_indication = CommandTextualGeneric._calculate_prompt(
                failure_exceptions_indication)
        self.login = login
        if isinstance(password, six.string_types):
            self._passwords = [password]
        elif password is None:
            self._passwords = []
        else:
            self._passwords = copy_list(
                password,
                deep_copy=False)  # copy of list of passwords to modify
        self.host = host
        self.port = port
        self.term_mono = term_mono
        self.encrypt_password = encrypt_password
        self.repeat_password = repeat_password
        self.send_enter_after_connection = send_enter_after_connection

        # Internal variables
        self._sent_login = False
        self._last_password = ""

        if login and username:
            self.command_string = self.__class__.__name__
            raise CommandFailure(
                self,
                "Please set login ('{}') or username ('{}') but not both.".
                format(login, username))
        elif username:
            self.login = username
        self.current_ret['LINES'] = list()
        self.current_ret['LAST_LOGIN'] = dict()
        self.current_ret['FAILED_LOGIN_ATTEMPTS'] = None
        self._converter_helper = ConverterHelper.get_converter_helper()