def _parse_show_interfaces_interface(self, output): # Parses show interface output for a single interface # Parse output into a dict, replace certain values with types parsed = cli_parse_basic(output) # Parse IP address fields if 'ip address' in parsed and parsed['ip address'] and \ 'netmask' in parsed and parsed['netmask']: parsed['ip address'] = ipaddress.ip_interface( "%s/%s" % (parsed['ip address'], parsed['netmask'])) del parsed['netmask'] for key in ['ipv6 address', 'ipv6 link-local address']: if key in parsed and parsed[key]: parsed[key] = ipaddress.ip_interface(parsed[key]) # Parse MAC Addr fields. for key in ['hw address']: if key in parsed: parsed[key] = netaddr.EUI(parsed[key]) # Parse Datetime fields. for key in ['counters cleared date']: if key in parsed: # Format: 2014/11/19 16:31:12 parsed[key] = datetime.datetime.strptime( parsed[key], '%Y/%m/%d %H:%M:%S') # Add in 'name' name = re.match('Interface (\S+) ', output).group(1) parsed['name'] = name return parsed
def test_cli_parse_basic(): parsed_output = parsers.cli_parse_basic(ANY_CLI_OUTPUT) assert parsed_output['key'] == 'value' assert parsed_output['foo'] == 'bar' assert parsed_output['numbers'] == '1 / 2 / 3' assert parsed_output['measurements'] == '15 Flg / 10 Wuzzits' assert parsed_output['time'] == '12d 15h 3m 10s' assert parsed_output['date'] == '2013-11-11 11:15:29'
def test_numerics_output(): parsed_output = parsers.cli_parse_basic(ANY_NUMERICS_OUTPUT) assert parsed_output['key'] == 'string_value' assert parsed_output['key2'] == 'numeric 10 with string' assert parsed_output['key3'] == 10 assert type(parsed_output['key3']) is int assert parsed_output['key4'] == 10.5 assert type(parsed_output['key4']) is float assert parsed_output['key5'] == '10.5.10.1'
def show_stats_bandwidth(self, port='all', type=None, frequency=None): """ Method to show Bandwidth Stats on a SteelHead :param port: Optional parameter to filter the bandwidth summary to traffic on a specific port. The value is simply the port number (e.g., "80") and defaults to "all." :type port: string :param type: The type of traffic to summarize. Options include bi-directional, lan-to-wan, and wan-to-lan. :type type: string :param frequency: Last period of time to lookback during stats collection. Options include 1min, 5min, hour, day, week, or month. :return: dictionary .. code-block:: python { 'wan data': '5.4 GB', 'lan data': '6 GB', 'data reduction': 10, 'data reduction peak': 95, 'data reduction peak time': '2014/12/05 14:50:00', 'capacity increase': '1.1' } """ cmd = "show stats bandwidth %s" % port if type is not None: cmd = cmd + " " + type if frequency is not None: cmd = cmd + " " + frequency result = self.cli.exec_command(cmd, output_expected=True) result.strip() parsed = cli_parse_basic(result) pct_pattern = "(\d+\.*\d*) [%X]" regex = re.compile(pct_pattern) for stat in parsed: if not parsed[stat]: parsed[stat] = 'None' continue match = regex.search(parsed[stat]) if match: parsed[stat] = float(match.group(1)) return parsed
def show_version(self): """ Returns parsed output of 'show version'. .. code-block:: none Product name: rbt_sh Product release: 9.0.1 Build ID: #19 Build date: 2014-11-19 01:59:36 Build arch: x86_64 Built by: mockbuild@bannow-worker4 Uptime: 15d 23h 22m 33s Product model: CX1555 System memory: 6378 MB used / 1552 MB free / 7931 MB total Number of CPUs: 4 CPU load averages: 0.08 / 0.17 / 0.10 :return: Dictionaries of values returned .. code-block:: python {'product name': 'rbt_sh', 'product release': '9.0.1', 'build id': '#19', 'build arch': 'x86_64', ... """ output = self.cli.exec_command("show version", output_expected=True) parsed = cli_parse_basic(output) # Force 'product model' to be a string. for key in ['product model']: if key in parsed: parsed[key] = unicode(parsed[key]) # Remove 'uptime' because we don't know what object to parse it into, # along with some others we don't want to format right now. for key in [ 'uptime', 'cpu load averages', 'system memory', 'build date' ]: if key in parsed: del parsed[key] return parsed
def test_multiple_enabled(): parsed_output = parsers.cli_parse_basic(ANY_MULTIPLE_ENABLED) with pytest.raises(KeyError): parsed_output = parsers.enable_squash(parsed_output)
def test_repeat_output(): with pytest.raises(KeyError): parsers.cli_parse_basic(ANY_REPEAT_OUTPUT)
def test_empty_output(): parsed_output = parsers.cli_parse_basic(ANY_EMPTY_OUTPUT) assert parsed_output['key'] is None