def getInterfaceListFromJunos(self, parse, cfg):
     parse_intf = CiscoConfParse(parse.find_all_children("^interface"))
     branchspec = (r'^interface', r'^    [a-z]{2}', r'unit', r'family inet',
                   r'address')
     branches = parse_intf.find_object_branches(branchspec=branchspec)
     subnets_list = {}
     for branch in branches:
         intf = ""
         unit = ""
         local_ip = ""
         intf_data = ""
         for elt in branch:
             if elt is not None:
                 if not re.search(r"inactive", elt.text):
                     if re.search(r"^    [a-z]{2}", elt.text):
                         intf = re.sub(r"\s+|{", "", elt.text)
                     if re.search(r"unit", elt.text):
                         unit = re.sub(r"\s+|unit|{", "", elt.text)
                     if re.search(r"address", elt.text):
                         local_ip = re.sub(r"\s+|address|{", "", elt.text)
                         intf_data = "{interface:%s.%s,local_ip:%s}" % (
                             intf, unit, local_ip)
                         subnets_list[local_ip] = "%s.%s" % (intf, unit)
     return subnets_list
Exemple #2
0
## Parse the list of config lines here...
parse = CiscoConfParse(config, syntax='junos', comment='#')

## Define a set of regular expressions starting with the 'root' object and
## iterating over each object's children until we get to the address object.
##
## This winds up matching a whole 'branch' of a family...
branchspec = (
    r'interfaces',
    r'^\s+(\S+)',  # Detect any intf name...
    r'^\s+unit\s+(\d+)',  # Detect intf's unit
    r'^\s+family\s+(inet\d*)',  # Detect intf's inet family
    r'^\s+address\s+(\S+)',  # Detect intf's address
)
for params in parse.find_object_branches(branchspec):

    ## Assign object names here based on the order in `branchspec` (above)
    ## Numbering starts and zero and ends with four.  We don't need to use
    ## `branchspec[0]`...
    intf_obj, intf_unit_obj, intf_family_obj, addr_obj = params[1], params[2], \
        params[3], params[4]

    ## We included regex `match groups` in the `branchspec` regular expressions
    ## so we can assign the text matched by each match group to a unique
    ## variable name
    intf_name = intf_obj.re_match_typed(branchspec[1])
    intf_unit = intf_unit_obj.re_match_typed(branchspec[2])
    intf_family = intf_family_obj.re_match_typed(branchspec[3])
    addr = addr_obj.re_match_typed(branchspec[4])