def populate(self): super(Tty, self).populate() shportd = "\n" + self.responses[0] shtty = self.responses[1] entries = [] for shportd_line, shtty_line in zip(shportd.split('\n'), shtty.split('\n')): portd_match = re.search( r'(\d+)\s+(.+)\s+(\d)\s+(\d)\s+(both|tel|ssh)\s+(both|rw|ro)\s+(ssh|-)\s+(on|off)\s+(.+)\s+(cr|lf|crlf)\s+(.+)\s+(cr|lf|crlf|-)', shportd_line ) tty_match = re.search( r'(\d+)\s+(.+)\s+(\d)\s+(none|even|odd)\s+(1|2)\s+(none|rts|xon)\s+(on|off)', shtty_line ) if portd_match and tty_match: entry = dict( tty=int(tty_match.group(1)), label=portd_match.group(2).strip(), baud=int(tty_match.group(2)), bitchar=int(tty_match.group(3)), parity=tty_match.group(4), stop=int(tty_match.group(5)), flow=tty_match.group(6), ) if entry: entries.append(entry) self.facts['tty'] = entries
def populate_ipv6_interfaces(self, data): for key, value in iteritems(data): self.facts['interfaces'][key]['ipv6'] = list() addresses = re.findall(r'\s+(.+), subnet', value, re.M) subnets = re.findall(r', subnet is (.+)$', value, re.M) for addr, subnet in zip(addresses, subnets): ipv6 = dict(address=addr.strip(), subnet=subnet.strip()) self.add_ip_address(addr.strip(), 'ipv6') self.facts['interfaces'][key]['ipv6'].append(ipv6)
def populate_ipv6_interfaces(self, data): for key, value in iteritems(data): self.facts["interfaces"][key]["ipv6"] = list() addresses = re.findall(r"\s+(.+), subnet", value, re.M) subnets = re.findall(r", subnet is (.+)$", value, re.M) for addr, subnet in zip(addresses, subnets): ipv6 = dict(address=addr.strip(), subnet=subnet.strip()) self.add_ip_address(addr.strip(), "ipv6") self.facts["interfaces"][key]["ipv6"].append(ipv6)
def run_commands(self): responses = self.connection.run_commands(self._commands) for resp, cmd in zip(responses, self._commands): cmd.response = resp # wipe out the commands list to avoid issues if additional # commands are executed later self._commands = list() return responses
def diff_exact(self, other, path=None): diff = list() if len(other) != len(self.items): diff.extend(self.items) else: for ours, theirs in zip(self.items, other): if ours != theirs: diff.extend(self.items) break return diff
def diff_exact(self, other): diff = list() if len(other.items) != len(self.items): diff.extend(self.items) else: for ours, theirs in zip(self.items, other.items): if ours != theirs: diff.extend(self.items) break return diff
def _diff_exact(self, other): updates = list() if len(other) != len(self.items): updates.extend(self.items) else: for ours, theirs in zip(self.items, other): if ours != theirs: updates.extend(self.items) break return updates
def populate_ipv6_interfaces(self, data): for key, value in iteritems(data): if key in ["No", "RPF"] or key.startswith("IP"): continue self.facts["interfaces"][key]["ipv6"] = list() addresses = re.findall(r"\s+(.+), subnet", value, re.M) subnets = re.findall(r", subnet is (.+)$", value, re.M) for addr, subnet in zip(addresses, subnets): ipv6 = dict(address=addr.strip(), subnet=subnet.strip()) self.add_ip_address(addr.strip(), "ipv6") self.facts["interfaces"][key]["ipv6"].append(ipv6)
def populate_ipv6_interfaces(self, data): for key, value in iteritems(data): try: self.facts["interfaces"][key]["ipv6"] = list() except KeyError: self.facts["interfaces"][key] = dict() self.facts["interfaces"][key]["ipv6"] = list() addresses = re.findall(r"\s+(.+), subnet", value, re.M) subnets = re.findall(r", subnet is (.+)$", value, re.M) for addr, subnet in zip(addresses, subnets): ipv6 = dict(address=addr.strip(), subnet=subnet.strip()) self.add_ip_address(addr.strip(), "ipv6") self.facts["interfaces"][key]["ipv6"].append(ipv6)
def populate_interfaces(self, lines1, lines2): interfaces = dict() for line1, line2 in zip(lines1, lines2): line = line1 + " " + line2 intfSplit = line.split() innerData = dict() innerData['description'] = intfSplit[6].strip() innerData['macaddress'] = intfSplit[8].strip() innerData['mtu'] = intfSplit[9].strip() innerData['speed'] = intfSplit[1].strip() innerData['duplex'] = intfSplit[2].strip() innerData['operstatus'] = intfSplit[5].strip() interfaces[intfSplit[0].strip()] = innerData return interfaces
def _is_equal(b_a, b_b): """ Comparing 2 byte arrays in constant time to avoid timing attacks. It would be nice if there were a library for this but hey. """ if not (isinstance(b_a, binary_type) and isinstance(b_b, binary_type)): raise TypeError('_is_equal can only be used to compare two byte strings') # http://codahale.com/a-lesson-in-timing-attacks/ if len(b_a) != len(b_b): return False result = 0 for b_x, b_y in zip(b_a, b_b): result |= b_x ^ b_y return result == 0
def populate_interfaces(self, lines1, lines2): interfaces = dict() for line1, line2 in zip(lines1, lines2): line = line1 + " " + line2 intfSplit = line.split() innerData = dict() innerData['description'] = intfSplit[6].strip() innerData['macaddress'] = intfSplit[8].strip() innerData['mtu'] = intfSplit[9].strip() innerData['speed'] = intfSplit[1].strip() innerData['duplex'] = intfSplit[2].strip() innerData['operstatus'] = intfSplit[5].strip() if("up" not in intfSplit[5].strip()) and ("down" not in intfSplit[5].strip()): innerData['description'] = intfSplit[7].strip() innerData['macaddress'] = intfSplit[9].strip() innerData['mtu'] = intfSplit[10].strip() innerData['operstatus'] = intfSplit[6].strip() interfaces[intfSplit[0].strip()] = innerData return interfaces
def to_subnet(addr, mask, dotted_notation=False): """ coverts an addr / mask pair to a subnet in cidr notation """ try: if not is_masklen(mask): raise ValueError cidr = int(mask) mask = to_netmask(mask) except ValueError: cidr = to_masklen(mask) addr = addr.split('.') mask = mask.split('.') network = list() for s_addr, s_mask in zip(addr, mask): network.append(str(int(s_addr) & int(s_mask))) if dotted_notation: return '%s %s' % ('.'.join(network), to_netmask(cidr)) return '%s/%s' % ('.'.join(network), cidr)
def _is_equal(b_a, b_b): """ Comparing 2 byte arrrays in constant time to avoid timing attacks. It would be nice if there was a library for this but hey. """ if not (isinstance(b_a, binary_type) and isinstance(b_b, binary_type)): raise TypeError('_is_equal can only be used to compare two byte strings') # http://codahale.com/a-lesson-in-timing-attacks/ if len(b_a) != len(b_b): return False result = 0 for b_x, b_y in zip(b_a, b_b): if PY3: result |= b_x ^ b_y else: result |= ord(b_x) ^ ord(b_y) return result == 0
def main(): spec = dict(gather_subset=dict(default=['!config'], type='list')) spec.update(iosxr_argument_spec) module = AnsibleModule(argument_spec=spec, supports_check_mode=True) warnings = list() check_args(module, warnings) gather_subset = module.params['gather_subset'] runable_subsets = set() exclude_subsets = set() for subset in gather_subset: if subset == 'all': runable_subsets.update(VALID_SUBSETS) continue if subset.startswith('!'): subset = subset[1:] if subset == 'all': exclude_subsets.update(VALID_SUBSETS) continue exclude = True else: exclude = False if subset not in VALID_SUBSETS: module.fail_json(msg='Bad subset') if exclude: exclude_subsets.add(subset) else: runable_subsets.add(subset) if not runable_subsets: runable_subsets.update(VALID_SUBSETS) runable_subsets.difference_update(exclude_subsets) runable_subsets.add('default') facts = dict() facts['gather_subset'] = list(runable_subsets) instances = list() for key in runable_subsets: instances.append(FACT_SUBSETS[key]()) try: for inst in instances: commands = inst.commands() responses = run_commands(module, commands) results = dict(zip(commands, responses)) inst.populate(results) facts.update(inst.facts) except Exception: module.exit_json(out=module.from_json(results)) ansible_facts = dict() for key, value in iteritems(facts): key = 'ansible_net_%s' % key ansible_facts[key] = value module.exit_json(ansible_facts=ansible_facts, warnings=warnings)
def main(): spec = dict( gather_subset=dict(default=['!config'], type='list') ) spec.update(iosxr_argument_spec) module = AnsibleModule(argument_spec=spec, supports_check_mode=True) warnings = list() gather_subset = module.params['gather_subset'] runable_subsets = set() exclude_subsets = set() for subset in gather_subset: if subset == 'all': runable_subsets.update(VALID_SUBSETS) continue if subset.startswith('!'): subset = subset[1:] if subset == 'all': exclude_subsets.update(VALID_SUBSETS) continue exclude = True else: exclude = False if subset not in VALID_SUBSETS: module.fail_json(msg='Bad subset') if exclude: exclude_subsets.add(subset) else: runable_subsets.add(subset) if not runable_subsets: runable_subsets.update(VALID_SUBSETS) runable_subsets.difference_update(exclude_subsets) runable_subsets.add('default') facts = dict() facts['gather_subset'] = list(runable_subsets) instances = list() for key in runable_subsets: instances.append(FACT_SUBSETS[key]()) try: for inst in instances: commands = inst.commands() responses = run_command(module, commands) results = dict(zip(commands, responses)) inst.populate(results) facts.update(inst.facts) except Exception: module.exit_json(out=module.from_json(results)) ansible_facts = dict() for key, value in iteritems(facts): key = 'ansible_net_%s' % key ansible_facts[key] = value module.exit_json(ansible_facts=ansible_facts, warnings=warnings)