def test_display_and_sort_name(self): t = taglib.tag("kind", "name", sort_name="sort name") taglib.tag("kind", "name", display_name="display name") print repr(t.changes) eq_(t.kind, "kind") eq_(t.name, "name") eq_(t.display_name, "display name") eq_(t.sort_name, "sort name")
def test_implies(self): p = taglib.tag("interface", "foo") s = taglib.tag("device", "goo") s.used(1, "myfile") p.implies(s, 2, "myfile") r = self.roundtrip() eq_(r[0]["tag"], "device--goo") eq_(r[0]["location"], "myfile:1") eq_(r[1]["tag"], "interface--foo") eq_(r[1]["implies"], "device--goo") eq_(r[1]["location"], "myfile:2")
def tag_scripts(top): for script_type in ("commit", "event", "op"): for cs in top.xpath("system/scripts/%s/file" % script_type): file_elem = cs.xpath("name")[0] t = taglib.tag("%s script" % script_type, "%s %s" % (device_tag.name,file_elem.text)) t.implies(device_tag, file_elem.sourceline) tf = taglib.tag("%s script file" % script_type, file_elem.text) tf.implied_by(t, file_elem.sourceline) source_elems = cs.xpath("source") if source_elems: elem = source_elems[0] src = taglib.tag("%s script source" % script_type, elem.text) src.implied_by(t, elem.sourceline)
def tag_scripts(top): for script_type in ("commit", "event", "op"): for cs in top.xpath("system/scripts/%s/file" % script_type): file_elem = cs.xpath("name")[0] t = taglib.tag("%s script" % script_type, "%s %s" % (device_tag.name, file_elem.text)) t.implies(device_tag, file_elem.sourceline) tf = taglib.tag("%s script file" % script_type, file_elem.text) tf.implied_by(t, file_elem.sourceline) source_elems = cs.xpath("source") if source_elems: elem = source_elems[0] src = taglib.tag("%s script source" % script_type, elem.text) src.implied_by(t, elem.sourceline)
def domain(self): cmd = self.expect(Keyword) if cmd == "name": t = taglib.tag("domain name", self.expect(String)) t.implied_by(taglib.env_tags.device, self.lineNum) self.expect(EndOfCommand) elif cmd == 'name-server': t = taglib.tag("name server", self.expect(Literal)) t.implied_by(taglib.env_tags.device, self.lineNum) self.expect(EndOfCommand) else: self.skipTo(EndOfCommand)
def dot11(self): subcmd = self.expect(Keyword) if subcmd == 'ssid': ssid = self.accept(String) self.expect(EndOfCommand) while True: if self.accept(Whitespace) is None: return try: op = self.accept(Operator) cmd = self.expect(Keyword) if False: pass elif cmd == "vlan": vlan_id = self.expect(Literal) ssidDict[vlan_id] = ssid ssidLineDict[vlan_id] = self.lineNum t = taglib.tag("VLAN ID", vlan_id, sort_name="%05d" % int(vlan_id)) t.used(self.lineNum) self.expect(EndOfCommand) else: self.skipTo(EndOfCommand) except UnexpectedToken: self.skipTo(EndOfCommand) else: self.skipTo(EndOfCommand)
def router(self): protocol = self.expect(Keyword) protocol_tag = taglib.tag("routing protocol", protocol.upper()) protocol_tag.implied_by(taglib.env_tags.device, self.lineNum) if protocol == "bgp": local_as = self.expect(Literal) local_as_tag = taglib.as_number_tag(local_as, "local AS") local_as_lineNum = self.lineNum local_as_tag.implies(taglib.as_number_tag(local_as), local_as_lineNum) self.skipTo(EndOfCommand) while True: if self.accept(Token.EndOfMode) is not None: return if self.accept(Whitespace) is not None: continue if self.accept(Comment) is not None: self.skipTo(EndOfCommand) continue try: op = self.accept(Operator) if op: pass cmd = self.expect(Keyword) if False: pass elif cmd == "neighbor-group": self.bgp_neighbor_group(self.expect(Literal)) elif cmd == "neighbor": peer = self.expect(Literal) peer_tag = taglib.ip_address_tag(peer, kind="%s peer" % protocol.upper()) peer_lineNum = self.lineNum address_tag = taglib.ip_address_tag(peer) peer_tag.implies(address_tag, self.lineNum) self.expect(EndOfCommand) self.bgp_neighbor(protocol_tag, peer, peer_tag, peer_lineNum, local_as, local_as_tag, local_as_lineNum) elif cmd == "vrf": self.expect(Literal) self.expect(EndOfCommand) else: self.skipTo(EndOfCommand) except UnexpectedToken: self.skipTo(EndOfCommand) elif protocol == "static": self.static() else: self.skipToEndOfMode()
def main(): startup = file(taglib.default_filename) running = file(dirname(taglib.default_filename) + "running.cfg") m = re.compile(r'^version') line = startup.readline() lineno = 1 while line and not m.match(line): line = startup.readline() lineno += 1 line2 = running.readline() while line2 and not m.match(line2): line2 = running.readline() m = re.compile(r'clock-period') while line and line2 and line == line2: line = startup.readline() lineno += 1 line2 = running.readline() if line and line2 and m.search(line): line = startup.readline() lineno += 1 line2 = running.readline() if line != line2: t = taglib.tag("flag", "unsaved changes") t.implied_by(taglib.env_tags.snapshot, lineno) taglib.output_tagging_log()
def ntp(self): self.expect(EndOfCommand) while True: if self.accept(Token.EndOfMode) is not None: return if self.accept(Whitespace) is not None: continue if self.accept(Comment) is not None: self.skipTo(EndOfCommand) continue try: cmd = self.expect(Keyword) if False: pass elif cmd == "server": t = taglib.tag("NTP server", self.expect(Literal)) t.implied_by(taglib.env_tags.device, self.lineNum) self.expect(EndOfCommand) else: self.skipTo(EndOfCommand) except UnexpectedToken: self.skipTo(EndOfCommand)
def radius(self): cmd = self.expect(Keyword) if cmd == "host": t = taglib.tag("RADIUS server", self.expect(Literal)) t.implied_by(taglib.env_tags.device, self.lineNum) self.skipTo(EndOfCommand)
def snmp_server(self): cmd = self.expect(Keyword) if cmd == "community": t = taglib.tag("SNMP community", self.expect(Literal)) t.implied_by(taglib.env_tags.device, self.lineNum) self.skipTo(EndOfCommand)
def ntp(self): cmd = self.expect(Keyword) if cmd == "server": t = taglib.tag("NTP server", self.expect(Literal)) t.implied_by(taglib.env_tags.device, self.lineNum) self.skipTo(EndOfCommand)
def ssh(self): protocol = 'ssh' nextCmd = self.expect(Keyword) if nextCmd == 'server': if self.sshEnabled == False: t = taglib.tag("service", taglib.protocol_name(protocol)) t.implied_by(taglib.env_tags.device, self.lineNum) self.sshEnabled = True t = taglib.tag("service", "SSHv2") t.implied_by(taglib.env_tags.device, self.lineNum) version = self.accept(Keyword) if not version == 'v2': t = taglib.tag("service", "SSHv1") t.implied_by(taglib.env_tags.device, self.lineNum) self.skipTo(EndOfCommand)
def tacacs_server(self): cmd = self.expect(Keyword) if cmd == 'host': t = taglib.tag("TACACS+ server", self.expect(Literal)) t.implied_by(taglib.env_tags.device, self.lineNum) self.skipTo(EndOfCommand) else: self.skipTo(EndOfCommand)
def tacacs_server(self): cmd = self.expect(Keyword) if cmd == 'host': t = taglib.tag("TACACS+ server", self.expect(Literal)) t.implied_by(taglib.env_tags.device, self.lineNum) self.expect(EndOfCommand) else: self.skipTo(EndOfCommand)
def test_single_used_tag(self): t = taglib.tag("interface", "foo") t.used(10, "myfile") r = self.roundtrip() eq_(r[0]["tag"], "interface--foo")
def test_unused_tag(self): taglib.tag("interface", "foo") r = self.roundtrip() eq_(len(r), 0)
def main(filename): lines = open(filename, 'rU') n = 0 for line in lines: n += 1 if re.match(r'Syntax error at token detail', line): lines.close() taglib.default_filename = filename = re.sub( r'-detail', '', filename) main(filename) return # time m = re.match( r'configure sntp-client (primary|secondary) (?:server )? "?([\w\d.-]+)"?( vr [\w\d.]+)?', line) if m: which, server, vr = m.groups() taglib.tag("NTP server", server).implied_by(taglib.env_tags.device, line=n) continue # dns m = re.match(r'configure dns-client add domain-suffix ([\w\d.-]+)', line) if m: server = m.group(1) taglib.tag("domain name", server).implied_by(taglib.env_tags.device, line=n) continue m = re.match( r'configure dns-client add (name-server )?([\w\d.-]+)( vr [\w\d.]+)?', line) if m: ignore, server, vr = m.groups() taglib.tag("name server", server).implied_by(taglib.env_tags.device, line=n) continue m = re.match(r'configure dns-client default-domain ([\w\d.-]+)', line) if m: server = m.group(1) taglib.tag("domain name", server).implied_by(taglib.env_tags.device, line=n) continue # radius m = re.match( r'configure radius (?:mgmt-access )?(primary|secondary) server ([\w\d.-]+)( [\d]+)? client-ip ([\w\d.-]+)( vr [\w\d.]+)?', line) if m: which, server, port, client, vr = m.groups() taglib.tag("RADIUS server", server).implied_by(taglib.env_tags.device, line=n) continue # tacacs+ m = re.match( r'configure tacacs (primary|secondary) server ([\w\d.-]+)( [\d]+)? client-ip ([\w\d.-]+)( vr [\w\d.]+)?', line) if m: which, server, port, client, vr = m.groups() taglib.tag("TACACS+ server", server).implied_by(taglib.env_tags.device, line=n) continue # interfaces m = re.match(r'create vlan "?([\w\d.-]+)"?', line) if m: vlan = m.group(1) vlanTag = taglib.tag("interface", "%s %s" % (taglib.env_tags.device.name, vlan)) vlanTag.implied_by(taglib.env_tags.snapshot, line=n) taglib.env_tags.device.implied_by(vlanTag, line=n) continue m = re.match(r'configure vlan "?([\w\d]+)"? tag ([\d]+)', line) if m: vlan, vlan_id = m.group(1), int(m.group(2)) vlan_tag = taglib.tag( "interface", "%s %s" % (taglib.env_tags.device.name, vlan)) vlan_id_tag = taglib.tag("VLAN ID", vlan_id, sort_name="%05d" % vlan_id) vlan_tag.implied_by(taglib.env_tags.snapshot, line=n) vlan_tag.implies(taglib.env_tags.device, line=n) vlan_tag.implies(vlan_id_tag, line=n) continue m = re.match( r'config(?:ure)? vlan "?([\w\d]+)"? ipaddress ([\d.]+)\s+([\d.]+)\s+', line) if m: vlan, ipaddress, mask = m.groups() address = ipaddress + "/" + mask ifaddr_tag = taglib.ip_address_tag(address, "interface address") address_tag = taglib.ip_address_tag(address) subnet_tag = taglib.ip_subnet_tag(address) vlan_tag = taglib.tag( "interface", "%s %s" % (taglib.env_tags.device.name, vlan)) vlan_tag.implied_by(taglib.env_tags.snapshot, line=n) vlan_tag.implies(taglib.env_tags.device, line=n) vlan_tag.implies(ifaddr_tag, line=n) ifaddr_tag.implies(address_tag, line=n) address_tag.implies(subnet_tag, line=n) continue m = re.match( r'configure "?([\w\d]+)"? ipaddress ([a-fA-F\d:]+)/([\d]+)\s+', line) if m: vlan, ipaddress, mask = m.groups() address = ipaddress + "/" + mask ifaddr_tag = taglib.ip_address_tag(address, "interface address") address_tag = taglib.ip_address_tag(address) subnet_tag = taglib.ip_subnet_tag(address) vlan_tag = taglib.tag( "interface", "%s %s" % (taglib.env_tags.device.name, vlan)) vlan_tag.implied_by(taglib.env_tags.snapshot, line=n) vlan_tag.implies(taglib.env_tags.device, line=n) vlan_tag.implies(ifaddr_tag, line=n) ifaddr_tag.implies(address_tag, line=n) address_tag.implies(subnet_tag, line=n) continue # dhcp/bootp m = re.match(r'configure bootprelay add ([\w\d.-]+)', line) if m: relay = m.group(1) taglib.tag("BOOTP relay", relay).implied_by(taglib.env_tags.device, line=n) continue # accounts m = re.match(r'create account (admin|user) "?([\w\d.-]+)"?\s+.*', line) if m: account = m.group(2) taglib.tag("user", account).implied_by(taglib.env_tags.device, line=n) continue m = re.match(r'configure account (admin|user).*', line) if m: account = m.group(1) taglib.tag("user", account).implied_by(taglib.env_tags.device, line=n) continue # protocols m = re.match(r'enable (bgp|igmp|MLD|msdp|rip|ripng)$', line) if m: protocolTag = taglib.tag("routing protocol", taglib.protocol_name(m.group(1))) protocolTag.implied_by(taglib.env_tags.device, line=n) continue m = re.match(r'enable (igmp|MLD) snooping.*', line) if m: protocol = m.group(1) protocolTag = taglib.tag("routing protocol", taglib.protocol_name(protocol)) protocolTag.implied_by(taglib.env_tags.device, line=n) continue m = re.match(r'configure DVMRP add vlan "?([\w\d.-]+)"?', line) if m: vlan = m.group(1) vlan_tag = taglib.tag( "interface", "%s %s" % (taglib.env_tags.device.name, vlan)) taglib.tag("routing protocol", "DVMRP").implied_by(vlan_tag, n) continue m = re.match( r'configure ospf add vlan "?([\w\d.-]+)"? area ([\d.]+).*', line) if m: vlan, area = m.groups() vlan_tag = taglib.tag( "interface", "%s %s" % (taglib.env_tags.device.name, vlan)) area_tag = taglib.tag("OSPF area", area) area_tag.implied_by(vlan_tag, line=n) area_tag.implies(taglib.tag("routing protocol", "OSPF"), line=n) continue m = re.match(r'configure ospf vlan ([\w\d.-]+) area ([\d.]+)', line) if m: vlan, area = m.groups() vlan_tag = taglib.tag( "interface", "%s %s" % (taglib.env_tags.device.name, vlan)) area_tag = taglib.tag("OSPF area", area) area_tag.implied_by(vlan_tag, line=n) area_tag.implies(taglib.tag("routing protocol", "OSPF"), line=n) continue m = re.match( r'configure ospfv3 domain ([\w\d.-]+) add vlan ([\w\d.-]+) instance-id ([\d]+) area ([\d.]+)', line) if m: domain, vlan, instance, area = m.groups() vlan_tag = taglib.tag( "interface", "%s %s" % (taglib.env_tags.device.name, vlan)) area_tag = taglib.tag("OSPFv3 area", area) area_tag.implied_by(vlan_tag, line=n) area_tag.implies(taglib.tag("routing protocol", "OSPFv3"), line=n) continue # services serviceLabelDict = { 'ssh': 'SSHv1', 'ssh1': 'SSHv1', 'ssh2': 'SSHv2', 'telnet': 'TELNET', 'web': 'HTTP' } m = re.match( r'enable (ssh2|telnet|web)( access-profile ([\w\d.-]+)( port ([\d]+))?)?', line) if m: service = m.group(1) taglib.tag("service", serviceLabelDict[service]).implied_by( taglib.env_tags.device, n) continue m = re.match( r'configure telnet( access-profile ([\w\d.-]+))?( port ([\d]+))?( vr ([\w\d.-]+))?', line) if m: taglib.tag("service", "TELNET").implied_by(taglib.env_tags.device, n) continue
def main(filename): lines = open(filename, 'rU') n = 0 for line in lines: n += 1 if re.match(r'Syntax error at token detail', line): lines.close() taglib.default_filename = filename = re.sub(r'-detail', '', filename) main(filename) return # time m = re.match(r'configure sntp-client (primary|secondary) (?:server )? "?([\w\d.-]+)"?( vr [\w\d.]+)?', line) if m: which, server, vr = m.groups() taglib.tag("NTP server", server).implied_by(taglib.env_tags.device, line=n) continue # dns m = re.match(r'configure dns-client add domain-suffix ([\w\d.-]+)', line) if m: server = m.group(1) taglib.tag("domain name", server).implied_by(taglib.env_tags.device, line=n) continue m = re.match(r'configure dns-client add (name-server )?([\w\d.-]+)( vr [\w\d.]+)?', line) if m: ignore, server, vr = m.groups() taglib.tag("name server", server).implied_by(taglib.env_tags.device, line=n) continue m = re.match(r'configure dns-client default-domain ([\w\d.-]+)', line) if m: server = m.group(1) taglib.tag("domain name", server).implied_by(taglib.env_tags.device, line=n) continue # radius m = re.match(r'configure radius (?:mgmt-access )?(primary|secondary) server ([\w\d.-]+)( [\d]+)? client-ip ([\w\d.-]+)( vr [\w\d.]+)?', line) if m: which, server, port, client, vr = m.groups() taglib.tag("RADIUS server", server).implied_by(taglib.env_tags.device, line=n) continue # tacacs+ m = re.match(r'configure tacacs (primary|secondary) server ([\w\d.-]+)( [\d]+)? client-ip ([\w\d.-]+)( vr [\w\d.]+)?', line) if m: which, server, port, client, vr = m.groups() taglib.tag("TACACS+ server", server).implied_by(taglib.env_tags.device, line=n) continue # interfaces m = re.match(r'create vlan "?([\w\d.-]+)"?', line) if m: vlan = m.group(1) vlanTag = taglib.tag("interface", "%s %s" % (taglib.env_tags.device.name, vlan)) vlanTag.implied_by(taglib.env_tags.snapshot, line=n) taglib.env_tags.device.implied_by(vlanTag, line=n) continue m = re.match(r'configure vlan "?([\w\d]+)"? tag ([\d]+)', line) if m: vlan, vlan_id = m.group(1), int(m.group(2)) vlan_tag = taglib.tag("interface", "%s %s" % (taglib.env_tags.device.name, vlan)) vlan_id_tag = taglib.tag("VLAN ID", vlan_id, sort_name="%05d" % vlan_id) vlan_tag.implied_by(taglib.env_tags.snapshot, line=n) vlan_tag.implies(taglib.env_tags.device, line=n) vlan_tag.implies(vlan_id_tag, line=n) continue m = re.match(r'config(?:ure)? vlan "?([\w\d]+)"? ipaddress ([\d.]+)\s+([\d.]+)\s+', line) if m: vlan, ipaddress, mask = m.groups() address = ipaddress + "/" + mask ifaddr_tag = taglib.ip_address_tag(address, "interface address") address_tag = taglib.ip_address_tag(address) subnet_tag = taglib.ip_subnet_tag(address) vlan_tag = taglib.tag("interface", "%s %s" % (taglib.env_tags.device.name, vlan)) vlan_tag.implied_by(taglib.env_tags.snapshot, line=n) vlan_tag.implies(taglib.env_tags.device, line=n) vlan_tag.implies(ifaddr_tag, line=n) ifaddr_tag.implies(address_tag, line=n) address_tag.implies(subnet_tag, line=n) continue m = re.match(r'configure "?([\w\d]+)"? ipaddress ([a-fA-F\d:]+)/([\d]+)\s+', line) if m: vlan, ipaddress, mask = m.groups() address = ipaddress + "/" + mask ifaddr_tag = taglib.ip_address_tag(address, "interface address") address_tag = taglib.ip_address_tag(address) subnet_tag = taglib.ip_subnet_tag(address) vlan_tag = taglib.tag("interface", "%s %s" % (taglib.env_tags.device.name, vlan)) vlan_tag.implied_by(taglib.env_tags.snapshot, line=n) vlan_tag.implies(taglib.env_tags.device, line=n) vlan_tag.implies(ifaddr_tag, line=n) ifaddr_tag.implies(address_tag, line=n) address_tag.implies(subnet_tag, line=n) continue # dhcp/bootp m = re.match(r'configure bootprelay add ([\w\d.-]+)', line) if m: relay = m.group(1) taglib.tag("BOOTP relay", relay).implied_by(taglib.env_tags.device, line=n) continue # accounts m = re.match(r'create account (admin|user) "?([\w\d.-]+)"?\s+.*', line) if m: account = m.group(2) taglib.tag("user", account).implied_by(taglib.env_tags.device, line=n) continue m = re.match(r'configure account (admin|user).*', line) if m: account = m.group(1) taglib.tag("user", account).implied_by(taglib.env_tags.device, line=n) continue # protocols m = re.match(r'enable (bgp|igmp|MLD|msdp|rip|ripng)$', line) if m: protocolTag = taglib.tag("routing protocol", taglib.protocol_name(m.group(1))) protocolTag.implied_by(taglib.env_tags.device, line=n) continue m = re.match(r'enable (igmp|MLD) snooping.*', line) if m: protocol = m.group(1) protocolTag = taglib.tag("routing protocol", taglib.protocol_name(protocol)) protocolTag.implied_by(taglib.env_tags.device, line=n) continue m = re.match(r'configure DVMRP add vlan "?([\w\d.-]+)"?', line) if m: vlan = m.group(1) vlan_tag = taglib.tag("interface", "%s %s" % (taglib.env_tags.device.name, vlan)) taglib.tag("routing protocol", "DVMRP").implied_by(vlan_tag, n) continue m = re.match(r'configure ospf add vlan "?([\w\d.-]+)"? area ([\d.]+).*', line) if m: vlan, area = m.groups() vlan_tag = taglib.tag("interface", "%s %s" % (taglib.env_tags.device.name, vlan)) area_tag = taglib.tag("OSPF area", area) area_tag.implied_by(vlan_tag, line=n) area_tag.implies(taglib.tag("routing protocol", "OSPF"), line=n) continue m = re.match(r'configure ospf vlan ([\w\d.-]+) area ([\d.]+)', line) if m: vlan, area = m.groups() vlan_tag = taglib.tag("interface", "%s %s" % (taglib.env_tags.device.name, vlan)) area_tag = taglib.tag("OSPF area", area) area_tag.implied_by(vlan_tag, line=n) area_tag.implies(taglib.tag("routing protocol", "OSPF"), line=n) continue m = re.match(r'configure ospfv3 domain ([\w\d.-]+) add vlan ([\w\d.-]+) instance-id ([\d]+) area ([\d.]+)', line) if m: domain, vlan, instance, area = m.groups() vlan_tag = taglib.tag("interface", "%s %s" % (taglib.env_tags.device.name, vlan)) area_tag = taglib.tag("OSPFv3 area", area) area_tag.implied_by(vlan_tag, line=n) area_tag.implies(taglib.tag("routing protocol", "OSPFv3"), line=n) continue # services serviceLabelDict = {'ssh':'SSHv1', 'ssh1':'SSHv1', 'ssh2':'SSHv2', 'telnet':'TELNET', 'web':'HTTP'} m = re.match(r'enable (ssh2|telnet|web)( access-profile ([\w\d.-]+)( port ([\d]+))?)?', line) if m: service = m.group(1) taglib.tag("service", serviceLabelDict[service]).implied_by(taglib.env_tags.device, n) continue m = re.match(r'configure telnet( access-profile ([\w\d.-]+))?( port ([\d]+))?( vr ([\w\d.-]+))?', line) if m: taglib.tag("service", "TELNET").implied_by(taglib.env_tags.device, n) continue
def command(self): while True: try: if self.accept(Whitespace) is not None: self.skipTo(EndOfCommand) continue self.skip(Comment, EndOfCommand) op = self.accept(Operator) if op == 'no': self.skipTo(EndOfCommand) continue cmd = self.expect(Keyword) if False: # just so all the real options can use elif... pass elif cmd == 'aaa': self.aaa() elif cmd == 'control-plane': self.skipToEndOfMode() elif cmd == 'controller': self.skipToEndOfMode() elif cmd == 'domain': self.domain() elif cmd == 'flow': self.skipToEndOfMode() elif cmd == 'hostname': t = taglib.tag("hostname", self.accept(String)) t.implied_by(taglib.env_tags.device, self.lineNum) self.expect(EndOfCommand) elif cmd == 'interface': self.interface() elif cmd == 'ipv4': self.skipToEndOfMode() elif cmd == 'line': self.skipToEndOfMode() elif cmd == "multicast-routing": self.multicast_routing() elif cmd == "ntp": self.ntp() elif cmd == 'radius-server': self.radius() elif cmd == 'router': self.router() elif cmd == "snmp-server": self.snmp_server() elif cmd == "ssh": self.ssh() elif cmd == "tacacs-server": self.tacacs_server() elif cmd == 'username': t = taglib.tag("user", self.accept(String)) t.implied_by(taglib.env_tags.device, self.lineNum) self.skipToEndOfMode() elif cmd == 'vrf': self.skipToEndOfMode() else: self.skipTo(EndOfCommand) except UnexpectedToken: self.skipTo(EndOfCommand)
def get_text(parentElem, xpath): elems = parentElem.xpath(xpath, nsmap) if elems: return elems[0].text def tag_attr(parentElem, xpath, kind, context): elems = parentElem.xpath(xpath, nsmap) if elems: t = taglib.tag(kind, elems[0].text) t.implied_by(context, elems[0].sourceline) return t chassisList = top.xpath("x:chassis", nsmap) for chassisElem in chassisList: nameElem = chassisElem.xpath("x:name", nsmap)[0] chassis_tag = taglib.tag("chassis","%s %s" % (taglib.env_tags.device.name, nameElem.text)) chassis_tag.implies(taglib.env_tags.device, nameElem.sourceline) tag_attr(chassisElem, "x:serial-number", "chassis serial number", chassis_tag) tag_attr(chassisElem, "x:description", "chassis description", chassis_tag) moduleList = chassisElem.xpath("x:chassis-module", nsmap) for moduleElem in moduleList: modNameElem = moduleElem.xpath("x:name", nsmap)[0] module_tag = taglib.tag("module", "%s %s %s" % (
def tag_attr(parentElem, xpath, kind, context): elems = parentElem.xpath(xpath, nsmap) if elems: t = taglib.tag(kind, elems[0].text) t.implied_by(context, elems[0].sourceline) return t
def get_text(parentElem, xpath): elems = parentElem.xpath(xpath, nsmap) if elems: return elems[0].text def tag_attrs(parentElem, xpath, kind, context): elems = parentElem.xpath(xpath, nsmap) for elem in elems: t = taglib.tag(kind, elem.text) t.implied_by(context, elem.sourceline) physicalElems = top.xpath("x:physical-interface", nsmap) for physicalElem in physicalElems: nameElems = physicalElem.xpath("x:name", nsmap) if not nameElems: continue nameElem = nameElems[0] physical_tag = taglib.tag( "physical interface", "%s %s" % (taglib.env_tags.device.name, nameElem.text)) physical_tag.implies(taglib.env_tags.device, nameElem.sourceline) tag_attrs(physicalElem, "x:speed", "speed", physical_tag) tag_attrs(physicalElem, "x:mtu", "MTU", physical_tag) tag_attrs(physicalElem, "x:if-type", "interface type", physical_tag) tag_attrs(physicalElem, "x:link-level-type", "link-level type", physical_tag) taglib.output_tagging_log()
def interface(self): ra_list = [] name = self.expect(Name) # TODO: make interface names sort in ascending numeric order if_tag = taglib.tag("interface", "%s %s" % (taglib.env_tags.device.name, name)) if_tag.implied_by(taglib.env_tags.snapshot, self.lineNum) if_tag.implies(taglib.env_tags.device, self.lineNum) if_tag.implies(taglib.tag("interface type", re.sub(r"[0-9/.]+$", "", name)), self.lineNum) m = re.match(r"(Dot11Radio[0-9]+)(\.)?([0-9]+)?$", name) if m: if not m.group(2): ssidsForInterface[m.group(1)] = [] if_tag.implies(taglib.tag("interface type", "wireless"), self.lineNum) if m.group(3): t = taglib.tag("VLAN ID", m.group(3), sort_name="%05d" % int(m.group(3))) t.implied_by(if_tag, self.lineNum) if ssidDict[m.group(3)] in ssidsForInterface[m.group(1)]: t = taglib.tag("SSID", ssidDict[m.group(3)]) t.implied_by(if_tag, ssidLineDict[m.group(3)]) self.expect(EndOfCommand) while True: if self.accept(Whitespace) is None: ra_suppress = False ra_prefix_line = None ra_if_prefix_line = None ra_prefix = [] if_prefix = [] # (ra_suppress, ra_line, if_prefix, ra_prefix) for ra in ra_list: ra_suppress |= ra[0] if ra[0]: admin = taglib.tag("admin disabled", "ND router advertisement server") admin.implied_by(if_tag, ra[1]) if len(ra[2]): if_prefix = ra[2] ra_if_prefix_line = ra[1] if len(ra[3]): ra_prefix = intra[3] ra_prefix_line = ra[1] if ra_prefix_line or ra_if_prefix_line: if not ra_suppress: rp = taglib.tag("routing protocol", "router advertisement") if ra_if_prefix_line: rp.used(ra_if_prefix_line) elif ra_prefix_line: rp.used(ra_prefix_line) ratag = taglib.tag("ND router advertisement server", if_tag.name) if len(ra_prefix): for p in ra_prefix: ratag.implies(taglib.ip_subnet_tag(p), ra_prefix_line) else: for p in if_prefix: ratag.implies(taglib.ip_subnet_tag(p), ra_if_prefix_line) return if self.accept(Comment) is not None: self.skipTo(EndOfCommand) continue try: op = self.accept(Operator) cmd = self.expect(Keyword) if False: pass elif cmd == "description": description = self.expect(String) t = taglib.tag("interface description", description) t.implied_by(if_tag, self.lineNum) self.expect(EndOfCommand) elif cmd == "ip": self.ip(if_tag=if_tag, if_name=name, version="IPv4") elif cmd == "ipv6": ra_list.append(self.ip(if_tag=if_tag, if_name=name, version="IPv6")) elif cmd == "ssid": if m and not m.group(2): ssidsForInterface[m.group(1)].append(self.expect(Literal)) self.expect(EndOfCommand) elif cmd == "tunnel": self.skipTo(EndOfCommand) else: self.skipTo(EndOfCommand) except UnexpectedToken: self.skipTo(EndOfCommand)
# # Copyright 2008 !j Incorporated # # This file is part of Canner. # # Canner is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # Canner is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with Canner. If not, see <http://www.gnu.org/licenses/>. # from canner import taglib release = open(taglib.default_filename).readline(1024).strip() if release: t = taglib.tag("OS version", release) t.implied_by(taglib.env_tags.snapshot, 1) t.implies(taglib.tag("OS", "Linux"), 1) taglib.output_tagging_log()
def command(self): while True: try: if self.accept(Whitespace) is not None: self.skipTo(EndOfCommand) continue self.skip(Comment, EndOfCommand) op = self.accept(Operator) if op == 'no': self.skipTo(EndOfCommand) continue cmd = self.expect(Keyword) if False: # just so all the real options can use elif... pass elif cmd == 'dot11': self.dot11() elif cmd == 'hostname': t = taglib.tag("hostname", self.accept(String)) t.implied_by(taglib.env_tags.device, self.lineNum) self.expect(EndOfCommand) elif cmd == 'interface': self.interface() elif cmd == 'ip': self.ip() elif cmd == "ipv6": self.ip(version="IPv6") elif cmd == "ntp": self.ntp() elif cmd == 'radius-server': self.radius() elif cmd == 'router': self.router() elif cmd == "snmp-server": self.snmp_server() elif cmd == 'tacacs-server': self.tacacs_server() elif cmd == 'username': t = taglib.tag("user", self.accept(String)) t.implied_by(taglib.env_tags.device, self.lineNum) self.skipTo(EndOfCommand) # elif cmd == 'version': # self.outputTag("version--" + self.accept(String)) # self.expect(EndOfCommand) else: self.skipTo(EndOfCommand) except UnexpectedToken: self.skipTo(EndOfCommand) if ipv6_addresses and ipv6_unicast_routing is None: t = taglib.tag("forwarding disabled", "IPv6 unicast") t.implied_by(device_tag, ipv6_unicast_routing)
# # Copyright 2008 !j Incorporated # # This file is part of Canner. # # Canner is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # Canner is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with Canner. If not, see <http://www.gnu.org/licenses/>. # import sys, os, re from canner import taglib data = open(taglib.default_filename).read(1024).strip('\n') m = re.search(r'Last configuration change at .*? by (.*)$', data, re.MULTILINE) if m: num = data[0:m.start(0)].count('\n') t = taglib.tag("config user", m.group(1)) t.implied_by(taglib.env_tags.snapshot, num) taglib.output_tagging_log()
def ip(self, if_tag=None, version=None, active=True): ra_line = None ra_suppress = False ra_prefix = [] if_prefix = [] cmd = self.expect(Keyword) if False: pass elif cmd == 'address': name = self.accept(String) ipaddress = self.accept(Literal) if ipaddress: self.accept( Punctuation) # allow detection of address/prefix length if name: ipaddress = IPy.intToIp( IPy.IP(ipaddress).int() | ipv6_general_prefixes[name].int(), 6) address = ipaddress + "/" + self.expect(Literal) if active: ifaddr_tag = taglib.ip_address_tag( address, kind="interface address") address_tag = taglib.ip_address_tag(address) subnet_tag = taglib.ip_subnet_tag(address) ifaddr_tag.implied_by(if_tag, self.lineNum) address_tag.implied_by(ifaddr_tag, self.lineNum) subnet_tag.implied_by(address_tag, self.lineNum) if version: version_tag = taglib.tag("IP version", version) version_tag.implied_by(if_tag, self.lineNum) # add router advertisement by default on multi-access networks if version == 'IPv6' and re.search( r"eth|srp", if_tag.name, re.I): if_prefix.append(subnet_tag.name) ra_line = self.lineNum self.skipTo(EndOfCommand) elif cmd == 'general-prefix': name = self.expect(String) ipaddress = self.expect(Literal) self.expect(Punctuation) address = ipaddress + "/" + self.expect(Literal) ipv6_general_prefixes[name] = IPy.IP(address) self.expect(EndOfCommand) elif cmd == 'helper-address': if self.accept(Keyword): self.expect(Literal) t = taglib.tag("BOOTP relay", self.expect(Literal)) t.implied_by(taglib.env_tags.device, self.lineNum) self.expect(EndOfCommand) elif cmd == 'http': nextCmd = self.expect(Keyword) if nextCmd == 'server': t = taglib.tag("service", "HTTP") t.implied_by(taglib.env_tags.device, self.lineNum) elif nextCmd == 'secure-server': t = taglib.tag("service", "HTTPS") t.implied_by(taglib.env_tags.device, self.lineNum) self.skipTo(EndOfCommand) elif cmd == 'nd': nextCmd = self.expect(Keyword) if nextCmd == 'prefix': nd_prefix = None default_prefix = self.accept(Keyword) if not default_prefix: nd_prefix = self.expect(Literal) + self.expect( Punctuation) + self.expect(Literal) keyword = self.accept(Keyword) if keyword is None or not 'no-ad' in keyword: if nd_prefix: ra_prefix.append(nd_prefix) elif nextCmd == 'suppress-ra': ra_suppress = True self.skipTo(EndOfCommand) elif cmd == 'scp': nextCmd = self.expect(Keyword) if nextCmd == 'server': nextCmd = self.expect(Keyword) if nextCmd == 'enable': t = taglib.tag("service", taglib.protocol_name(cmd)) t.implied_by(taglib.env_tags.device, self.lineNum) self.skipTo(EndOfCommand) else: self.skipTo(EndOfCommand) return (ra_suppress, ra_line, if_prefix, ra_prefix)
def interface(self): # preconfigure is inserted into the config before the interface name when the matching # hardware isn't found. for now, lets just ignore these interfaces. inactive = self.accept(Keyword) name = self.expect(Name) if_tag = None if not inactive: if_tag = taglib.tag("interface", "%s %s" % (taglib.env_tags.device.name, name)) if_tag.implied_by(taglib.env_tags.snapshot, self.lineNum) if_tag.implies(taglib.env_tags.device, self.lineNum) if_tag.implies( taglib.tag("interface type", re.sub(r"[0-9/.]+$", "", name)), self.lineNum) self.expect(EndOfCommand) while True: if self.accept(Token.EndOfMode) is not None: return if self.accept(Whitespace) is not None: continue if self.accept(Comment) is not None: self.skipTo(EndOfCommand) continue try: op = self.accept(Operator) cmd = self.expect(Keyword) if False: pass elif cmd == "description": description = self.expect(String) if not inactive: t = taglib.tag("interface description", description) t.implied_by(if_tag, self.lineNum) self.expect(EndOfCommand) elif cmd == "ipv4": self.ip(if_tag=if_tag, version="IPv4", active=not inactive) elif cmd == "ipv6": ra = self.ip(if_tag=if_tag, version="IPv6", active=not inactive) # (ra_suppress, ra_line, if_prefix, ra_prefix) if not ra[0] and ra[1]: ratag = taglib.tag("ra server", if_tag.name) if len(ra[3]): for p in ra[3]: ratag.implies(taglib.ip_subnet_tag(p), ra[1]) else: for p in ra[2]: ratag.implies(taglib.ip_subnet_tag(p), ra[1]) else: self.skipTo(EndOfCommand) except UnexpectedToken: self.skipTo(EndOfCommand)
def tag_protocols(top): protocol = "BGP" routing_options_local_as_list = top.xpath("routing-options/autonomous-system/as-number") routing_options_router_id_list = top.xpath("routing-options/router-id") protocol_elem_list = top.xpath("protocols/%s" % protocol.lower()) if protocol_elem_list: protocol_elem = protocol_elem_list[0] protocol_tag = taglib.tag("routing protocol", protocol) protocol_tag.used(protocol_elem.sourceline) for group_elem in protocol_elem.xpath("group"): name_elem = group_elem.xpath("name")[0] group_tag = taglib.tag("%s group" % protocol, "%s %s" % (device_tag.name, name_elem.text)) group_tag.implied_by(device_tag, name_elem.sourceline) for peer_name_elem in group_elem.xpath("neighbor/name"): local_address_elem_list = peer_name_elem.xpath("ancestor::*/local-address") if local_address_elem_list: local_address_elem = local_address_elem_list[0] elif routing_options_router_id_list: local_address_elem = routing_options_router_id_list[0] local_elem_list = peer_name_elem.xpath("ancestor::*/local-as/as-number") if local_elem_list: local_elem = local_elem_list[0] elif routing_options_local_as_list: local_elem = routing_options_local_as_list[0] if local_elem is not None: localasn_tag = taglib.as_number_tag(local_elem.text, "local AS") localasn_tag.implies(taglib.as_number_tag(local_elem.text), local_elem.sourceline) asn_elem_list = peer_name_elem.xpath("ancestor::*/peer-as") if asn_elem_list: asn_elem = asn_elem_list[0] else: asn_elem = local_elem asn_tag = taglib.as_number_tag(asn_elem.text, kind="remote AS") asn_tag.implies(taglib.as_number_tag(asn_elem.text), asn_elem.sourceline) if asn_elem.text == local_elem.text: peering_relationship = "iBGP" else: peering_relationship = "eBGP" address_tag = taglib.ip_address_tag(peer_name_elem.text) peering_tag = taglib.tag("%s peering" % peering_relationship, "%s %s" % (device_tag.name, peer_name_elem.text), sort_name="%s %s" % (device_tag.name, address_tag.sort_name)) peering_tag.implies(protocol_tag, peer_name_elem.sourceline) peering_tag.implied_by(device_tag, peer_name_elem.sourceline) peering_tag.implied_by(group_tag, peer_name_elem.sourceline) asn_tag.implied_by(peering_tag, asn_elem.sourceline) localasn_tag.implied_by(peering_tag, local_elem.sourceline) peer_tag = taglib.ip_address_tag(peer_name_elem.text, kind="%s peer" % protocol) peer_tag.implies(address_tag, peer_name_elem.sourceline) if local_address_elem is not None: peer2_tag = taglib.ip_address_tag(local_address_elem.text, kind="%s peer" % protocol.upper()) address2_tag = taglib.ip_address_tag(local_address_elem.text) peer2_tag.implies(address2_tag, local_address_elem.sourceline) local_peer_tag = taglib.ip_address_tag(local_address_elem.text, kind="local %s peer" % protocol) local_peer_tag.implied_by(peering_tag, local_address_elem.sourceline) local_peer_tag.implies(peer2_tag, peer_name_elem.sourceline) remote_peer_tag = taglib.ip_address_tag(peer_name_elem.text, kind="remote %s peer" % protocol) remote_peer_tag.implied_by(peering_tag, peer_name_elem.sourceline) remote_peer_tag.implies(peer_tag, peer_name_elem.sourceline) protocol = "MSDP" protocol_elem_list = top.xpath("protocols/%s" % protocol.lower()) if protocol_elem_list: protocol_elem = protocol_elem_list[0] protocol_tag = taglib.tag("routing protocol", protocol) protocol_tag.used(protocol_elem.sourceline) for group_elem in protocol_elem.xpath("group"): name_elem = group_elem.xpath("name")[0] group_tag = taglib.tag("%s group" % protocol, "%s %s" % (device_tag.name, name_elem.text)) group_tag.implied_by(device_tag, name_elem.sourceline) for peer_name_elem in group_elem.xpath("peer/name"): peer_tag = taglib.tag("%s peer" % protocol, peer_name_elem.text) address_tag = taglib.ip_address_tag(peer_name_elem.text) peer_tag.implies(address_tag, peer_name_elem.sourceline) peer_tag.implied_by(group_tag, peer_name_elem.sourceline) peer_tag.implies(protocol_tag, peer_name_elem.sourceline) ospf_version_dict = {'ospf':'OSPF', 'ospf3':'OSPFv3'} for protocol_key, protocol in ospf_version_dict.items(): protocol_elem_list = top.xpath("protocols/%s" % protocol_key) if not protocol_elem_list: continue protocol_elem = protocol_elem_list[0] protocol_tag = taglib.tag("routing protocol", protocol) protocol_tag.used(protocol_elem.sourceline) for area_elem in protocol_elem.xpath("area"): name_elem = area_elem.xpath("name")[0] area_tag = taglib.tag("%s area" % protocol, name_elem.text) area_tag.implies(protocol_tag, name_elem.sourceline) for interface_name_elem in area_elem.xpath("interface/name"): if interface_name_elem.text == "all": interface_tags = all_interface_tags else: interface_tags = [ taglib.tag("interface", "%s %s" % (device_tag.name, interface_name_elem.text))] for t in interface_tags: t.implies(area_tag, interface_name_elem.sourceline) protocol = "PIM" protocol_elem_list = top.xpath("protocols/%s" % protocol.lower()) if protocol_elem_list: protocol_elem = protocol_elem_list[0] protocol_tag = taglib.tag("routing protocol", protocol) protocol_tag.used(protocol_elem.sourceline) for interface_name_elem in protocol_elem.xpath("interface/name"): if interface_name_elem.text == "all": interface_tags = all_interface_tags else: interface_tags = [ taglib.tag("interface", "%s %s" % (device_tag.name, interface_name_elem.text))] for t in interface_tags: t.implies(protocol_tag, interface_name_elem.sourceline) for protocol in ("RIP", "RIPng"): protocol_elem_list = top.xpath("protocols/%s" % protocol.lower()) if not protocol_elem_list: continue protocol_elem = protocol_elem_list[0] protocol_tag = taglib.tag("routing protocol", protocol) protocol_tag.used(protocol_elem.sourceline) for group_elem in protocol_elem.xpath("group"): name_elem = group_elem.xpath("name")[0] group_tag = taglib.tag("%s group" % protocol,name_elem.text) group_tag.implies(protocol_tag, name_elem.sourceline) for interface_name_elem in group_elem.xpath("neighbor/name"): if interface_name_elem.text == "all": interface_tags = all_interface_tags else: interface_tags = [ taglib.tag("interface", "%s %s" % (device_tag.name, interface_name_elem.text))] for t in interface_tags: t.implies(group_tag, interface_name_elem.sourceline) protocol = "router-advertisement" protocol_elem_list = top.xpath("protocols/%s" % protocol.lower()) if protocol_elem_list: protocol_elem = protocol_elem_list[0] protocol_tag = taglib.tag("routing protocol", protocol) protocol_tag.used(protocol_elem.sourceline) for interface_elem in protocol_elem.xpath("interface"): interface_name_elem = interface_elem.xpath("name")[0] if interface_name_elem.text == "all": interface_tags = all_interface_tags else: interface_tags = [ taglib.tag("interface", "%s %s" % (device_tag.name, interface_name_elem.text))] for t in interface_tags: ratag = taglib.tag("ND router advertisement server", "%s %s" % (device_tag.name, interface_name_elem.text)) ratag.implied_by(t, interface_name_elem.sourceline); for prefix_name_elem in interface_elem.xpath("prefix/name"): ratag.implies(taglib.ip_subnet_tag(prefix_name_elem.text), prefix_name_elem.sourceline)
def tag_location(top): for prop in top.xpath("system/location/*"): t = taglib.tag("location %s" % prop.tag, prop.text) t.used(prop.sourceline)
def service_tag(e): if not e.tag.endswith("}comment"): t = taglib.tag("service", taglib.protocol_name(e.tag)) t.implied_by(taglib.env_tags.device, e.sourceline)
def bgp_neighbor(self, protocol_tag, peer, peer_tag, peer_lineNum, top_local_as, top_local_as_tag, top_local_as_lineNum): local_as = top_local_as local_as_tag = top_local_as_tag local_as_lineNum = top_local_as_lineNum while True: if self.accept(Token.EndOfMode) is not None: if peer_as == local_as: peering_relationship = "iBGP" else: peering_relationship = "eBGP" peering_tag = taglib.tag("%s peering" % peering_relationship, "%s %s" % (device_tag.name, peer), sort_name="%s %s" % (device_tag.name, peer_tag.sort_name)) peering_tag.implies(protocol_tag, peer_lineNum) peering_tag.implied_by(device_tag, peer_lineNum) local_as_tag.implied_by(peering_tag, peer_lineNum) peer_as_tag.implied_by(peering_tag, peer_lineNum) remote_peer_tag = taglib.ip_address_tag(peer, kind="remote %s peer" % protocol_tag.name) remote_peer_tag.implied_by(peering_tag, peer_lineNum) remote_peer_tag.implies(peer_tag, peer_lineNum) return if self.accept(Whitespace) is not None: continue if self.accept(Comment) is not None: self.skipTo(EndOfCommand) continue try: op = self.accept(Operator) if op: pass cmd = self.expect(Keyword) if False: pass elif cmd == "local-as": local_as = self.expect(Literal) local_as_lineNum = self.lineNum local_as_tag = taglib.as_number_tag(local_as, "local AS") local_as_tag.implies(taglib.as_number_tag(local_as), local_as_lineNum) self.expect(EndOfCommand) elif cmd == "remote-as": peer_as = self.expect(Literal) peer_as_lineNum = self.lineNum peer_as_tag = taglib.as_number_tag(peer_as, "remote AS") peer_as_tag.implies(taglib.as_number_tag(peer_as), peer_as_lineNum) self.expect(EndOfCommand) elif cmd == "use": subcmd = self.expect(Keyword) if subcmd == "neighbor-group": group = self.expect(Literal) group_local_as = bgp_group_local_as.get(group, None) if group_local_as is not None: local_as = group_local_as local_as_lineNum = bgp_group_local_as_lineNum.get( group, None) local_as_tag = taglib.as_number_tag( local_as, "local AS") local_as_tag.implies( taglib.as_number_tag(local_as), local_as_lineNum) group_peer_as = bgp_group_remote_as.get(group, None) if group_peer_as is not None: peer_as = group_peer_as peer_as_lineNum = bgp_group_remote_as_lineNum.get( group, None) peer_as_tag = taglib.as_number_tag( peer_as, "remote AS") peer_as_tag.implies(taglib.as_number_tag(peer_as), peer_as_lineNum) self.skipTo(EndOfCommand) elif cmd == "address-family": self.bgp_neighbor_address_family() else: self.skipTo(EndOfCommand) except UnexpectedToken: self.skipTo(EndOfCommand)
def tag_interfaces(top): for if_elem in top.xpath("interfaces/interface"): if_name_elem = if_elem.xpath("name")[0] if_tag = taglib.tag("physical interface", "%s %s" % (device_tag.name, if_name_elem.text)) if_tag.implies(device_tag, if_name_elem.sourceline) descr_elems = if_elem.xpath("description") if descr_elems: elem = descr_elems[0] t = taglib.tag("interface description", elem.text) t.implied_by(if_tag, elem.sourceline) m = re.match(r"[a-zA-Z]+", if_name_elem.text) if m: t = taglib.tag("interface type", m.group(0)) t.implied_by(if_tag, if_name_elem.sourceline) for unit_elem in if_elem.xpath("unit"): unit_name_elem = unit_elem.xpath("name")[0] unit_name = if_name_elem.text + "." + unit_name_elem.text unit_tag = taglib.tag("interface", "%s %s" % (device_tag.name, unit_name)) unit_tag.implied_by(snapshot_tag, unit_name_elem.sourceline) unit_tag.implies(if_tag, unit_name_elem.sourceline) all_interface_tags.append(unit_tag) vlan_id_list = unit_elem.xpath("vlan-id") if vlan_id_list: vlan_id = int(vlan_id_list[0].text) vlan_id_tag = taglib.tag("VLAN ID", str(vlan_id), sort_name="%05d" % vlan_id) vlan_id_tag.implied_by(unit_tag, vlan_id_list[0].sourceline) descr_elems = unit_elem.xpath("description") if descr_elems: elem = descr_elems[0] t = taglib.tag("interface description", elem.text) t.implied_by(unit_tag, elem.sourceline) def tag_addresses(family_elem): for address_elem in family_elem.xpath("address/name"): ifa_tag = taglib.ip_address_tag(address_elem.text, kind="interface address") ifa_tag.implied_by(unit_tag, address_elem.sourceline) t = taglib.ip_address_tag(address_elem.text) t.implied_by(ifa_tag, address_elem.sourceline) t.implies(taglib.ip_subnet_tag(address_elem.text), address_elem.sourceline) inet_list = unit_elem.xpath("family/inet") if inet_list: inet_elem = inet_list[0] tag_addresses(inet_elem) t = taglib.tag("IP version", "IPv4") t.implied_by(unit_tag, inet_elem.sourceline) inet6_list = unit_elem.xpath("family/inet6") if inet6_list: inet6_elem = inet6_list[0] tag_addresses(inet6_elem) t = taglib.tag("IP version", "IPv6") t.implied_by(unit_tag, inet6_elem.sourceline)
# This file is part of Canner. # # Canner is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # Canner is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with Canner. If not, see <http://www.gnu.org/licenses/>. # import sys, os, re from canner import taglib # verify /md5 (flash:c2801-advsecurityk9-mz.124-10a.bin) = 3f61cf7ee066f423f0411689080dc22b data = open(taglib.default_filename).read() m = re.search(r"verify.*\(.*[:/]([^:/]+?)(?:\.bin)?\) = (\w+)", data) if m: image, hash = m.groups() t = taglib.tag("system image signature","%s %s" % (image, hash)) t.implied_by(taglib.env_tags.device) t.implies(taglib.tag("system image", image)) taglib.output_tagging_log()
def tag_matches(top, path, kind, context): for e in top.xpath(path): t = taglib.tag(kind, e.text) t.implied_by(context, e.sourceline)
def ip(self, if_tag=None, if_name=None, version=None): ra_line = None ra_suppress = False ra_prefix = [] if_prefix = [] cmd = self.expect(Keyword) if False: pass elif cmd == 'address': name = self.accept(String) ipaddress = self.accept(Literal) if ipaddress: self.accept(Punctuation) # allow detection of address/prefix length if name: ipaddress = IPy.intToIp(IPy.IP(ipaddress).int() | ipv6_general_prefixes[name].int(), 6) address = ipaddress + "/" + self.expect(Literal) if if_name: if version == 'IPv4': if_addrs4.setdefault(if_name, ipaddress) elif version == 'IPv6': if_addrs6.setdefault(if_name, ipaddress) ifaddr_tag = taglib.ip_address_tag(address, kind="interface address") address_tag = taglib.ip_address_tag(address) subnet_tag = taglib.ip_subnet_tag(address) ifaddr_tag.implied_by(if_tag, self.lineNum) address_tag.implied_by(ifaddr_tag, self.lineNum) subnet_tag.implied_by(address_tag, self.lineNum) if version: version_tag = taglib.tag("IP version", version) version_tag.implied_by(if_tag, self.lineNum) # add router advertisement by default on multi-access networks if version == 'IPv6': ipv6_addresses = True if re.search(r"eth|fddi|port-channel", if_tag.name, re.I): if_prefix.append(subnet_tag.name) ra_line = self.lineNum self.skipTo(EndOfCommand) elif cmd == 'cef': if not version: version = "IPv4" distributed = self.accept(Keyword) if distributed == "distributed": cef = " ".join((version, distributed)) else: cef = version t = taglib.tag("Cisco express forwarding", cef) t.implied_by(device_tag, self.lineNum) self.skipTo(EndOfCommand) elif cmd == 'domain name' or cmd == 'domain-name': t = taglib.tag("domain name", self.expect(String)) t.implied_by(taglib.env_tags.device, self.lineNum) self.expect(EndOfCommand) elif cmd == 'general-prefix': name = self.expect(String) ipaddress = self.expect(Literal) self.expect(Punctuation) address = ipaddress + "/" + self.expect(Literal) ipv6_general_prefixes[name] = IPy.IP(address) self.expect(EndOfCommand) elif cmd == 'helper-address': t = taglib.tag("BOOTP relay", self.expect(Literal)) t.implied_by(taglib.env_tags.device, self.lineNum) self.expect(EndOfCommand) elif cmd == 'http': nextCmd = self.expect(Keyword) if nextCmd == 'server': t = taglib.tag("service", "HTTP") t.implied_by(taglib.env_tags.device, self.lineNum) elif nextCmd == 'secure-server': t = taglib.tag("service", "HTTPS") t.implied_by(taglib.env_tags.device, self.lineNum) self.skipTo(EndOfCommand) elif cmd == 'name-server': t = taglib.tag("name server", self.expect(Literal)) t.implied_by(taglib.env_tags.device, self.lineNum) self.expect(EndOfCommand) elif cmd == 'nd': nextCmd = self.expect(Keyword) if nextCmd == 'prefix': ra_line = self.lineNum nd_prefix = None default_prefix = self.accept(Keyword) if not default_prefix: nd_prefix = self.expect(Literal) + self.expect(Punctuation) + self.expect(Literal) keyword = self.accept(Keyword) if keyword is None or not 'no-ad' in keyword: if nd_prefix: ra_prefix.append(nd_prefix) elif nextCmd == 'suppress-ra': ra_line = self.lineNum ra_suppress = True self.skipTo(EndOfCommand) elif cmd == 'scp': nextCmd = self.expect(Keyword) if nextCmd == 'server': nextCmd = self.expect(Keyword) if nextCmd == 'enable': t = taglib.tag("service", taglib.protocol_name(cmd)) t.implied_by(taglib.env_tags.device, self.lineNum) self.skipTo(EndOfCommand) elif cmd == 'ssh': if self.sshEnabled == False: t = taglib.tag("service", taglib.protocol_name(cmd)) t.implied_by(taglib.env_tags.device, self.lineNum) self.sshEnabled = True nextCmd = self.expect(Keyword) if nextCmd == 'version': version = self.expect(Literal) if version == '2': t = taglib.tag("service", taglib.protocol_name(cmd)) t.implied_by(taglib.env_tags.device, self.lineNum) t = taglib.tag("service", "SSHv2") t.implied_by(taglib.env_tags.device, self.lineNum) elif version == '1': t = taglib.tag("service", taglib.protocol_name(cmd)) t.implied_by(taglib.env_tags.device, self.lineNum) t = taglib.tag("service", "SSHv1") t.implied_by(taglib.env_tags.device, self.lineNum) self.skipTo(EndOfCommand) elif cmd == 'unicast-routing': ipv6_unicast_routing = self.lineNum self.expect(EndOfCommand) else: self.skipTo(EndOfCommand) return (ra_suppress, ra_line, if_prefix, ra_prefix)
def test_tag_cache(self): t1 = taglib.tag("kind", "name") t2 = taglib.tag("kind", "other name") t3 = taglib.tag(qname="kind--name") assert t1 != t2 assert t1 is t3
# This file is part of Canner. # # Canner is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # Canner is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with Canner. If not, see <http://www.gnu.org/licenses/>. # import sys, os, re from canner import taglib # verify /md5 (flash:c2801-advsecurityk9-mz.124-10a.bin) = 3f61cf7ee066f423f0411689080dc22b data = open(taglib.default_filename).read() m = re.search(r"verify.*\(.*[:/]([^:/]+?)(?:\.bin)?\) = (\w+)", data) if m: image, hash = m.groups() t = taglib.tag("system image signature", "%s %s" % (image, hash)) t.implied_by(taglib.env_tags.device) t.implies(taglib.tag("system image", image)) taglib.output_tagging_log()
if elems: return elems[0].text def tag_attr(parentElem, xpath, kind, context): elems = parentElem.xpath(xpath, nsmap) if elems: t = taglib.tag(kind, elems[0].text) t.implied_by(context, elems[0].sourceline) return t chassisList = top.xpath("x:chassis", nsmap) for chassisElem in chassisList: nameElem = chassisElem.xpath("x:name", nsmap)[0] chassis_tag = taglib.tag( "chassis", "%s %s" % (taglib.env_tags.device.name, nameElem.text)) chassis_tag.implies(taglib.env_tags.device, nameElem.sourceline) tag_attr(chassisElem, "x:serial-number", "chassis serial number", chassis_tag) tag_attr(chassisElem, "x:description", "chassis description", chassis_tag) moduleList = chassisElem.xpath("x:chassis-module", nsmap) for moduleElem in moduleList: modNameElem = moduleElem.xpath("x:name", nsmap)[0] module_tag = taglib.tag( "module", "%s %s %s" % (taglib.env_tags.device.name, nameElem.text, modNameElem.text)) module_tag.implied_by(taglib.env_tags.snapshot, modNameElem.sourceline) module_tag.implies(chassis_tag, modNameElem.sourceline)
def main(filename): lines = open(filename, 'rU') n = 0 ssh_version = None ssh_enable = False for line in lines: n += 1 # hostname m = re.match(r'set hostname ([\w\d.-]+)', line) if m: host = m.group(1) taglib.tag("hostname", host).implied_by(taglib.env_tags.device, line=n) continue # time m = re.match(r'set ntp server( backup\d)? "?([\w\d.-]+)"?', line) if m: server = m.group(2) if not server == '0.0.0.0': taglib.tag("NTP server", server).implied_by(taglib.env_tags.device, line=n) continue # dns m = re.match(r'set domain ([\w\d.-]+)', line) if m: domain = m.group(1) taglib.tag("domain name", domain).implied_by(taglib.env_tags.device, line=n) continue m = re.match(r'set dns host dns\d ([\w\d.-]+)', line) if m: server = m.group(1) taglib.tag("name server", server).implied_by(taglib.env_tags.device, line=n) continue m = re.match(r'set xauth ([\w\d.-]+) dns\d ([\w\d.-]+)', line) if m: server = m.group(2) taglib.tag("name server", server).implied_by(taglib.env_tags.device, line=n) continue m = re.match(r'set l2tp dns\d ([\w\d.-]+)', line) if m: server = m.group(1) taglib.tag("name server", server).implied_by(taglib.env_tags.device, line=n) continue # interfaces m = re.match(r'set interface ([\w\d]+) ip ([\d.]+)/([\d.]+)( secondary)?', line) if m: name, ipaddress, plen, secondary = m.groups() address = ipaddress + "/" + plen ifaddr_tag = taglib.ip_address_tag(address, "interface address") address_tag = taglib.ip_address_tag(address) subnet_tag = taglib.ip_subnet_tag(address) name_tag = taglib.tag("interface", "%s %s" % (taglib.env_tags.device.name, name)) name_tag.implied_by(taglib.env_tags.snapshot, line=n) name_tag.implies(taglib.env_tags.device, line=n) name_tag.implies(ifaddr_tag, line=n) ifaddr_tag.implies(address_tag, line=n) address_tag.implies(subnet_tag, line=n) continue # accounts m = re.match(r'set admin user "?([\w\d.-]+)"?\s+.*', line) if m: account = m.group(1) taglib.tag("user", account).implied_by(taglib.env_tags.device, line=n) continue # services m = re.match(r'set ssh version ([\w\d]+)', line) if m: ssh_version = m.group(1) ssh_version_line = n continue m = re.match(r'set ssh enable', line) if m: ssh_enable = True taglib.tag("service", 'SSH').implied_by(taglib.env_tags.device, n) continue m = re.match(r'set scp enable', line) if m: taglib.tag("service", 'SCP').implied_by(taglib.env_tags.device, n) continue # post parse phase if ssh_enable: if ssh_version: taglib.tag("service", 'SSH' + ssh_version).implied_by(taglib.env_tags.device, ssh_version_line)
def tag_attrs(parentElem, xpath, kind, context): elems = parentElem.xpath(xpath, nsmap) for elem in elems: t = taglib.tag(kind, elem.text) t.implied_by(context, elem.sourceline)
def router(self): protocol = self.expect(Keyword) protocol_tag = taglib.tag("routing protocol", protocol.upper()) protocol_tag.implied_by(taglib.env_tags.device, self.lineNum) if protocol == "bgp": bgp_router_id = None bgp_router_id_lineNum = None local_as = self.expect(Literal) local_as_tag = taglib.as_number_tag(local_as, "local AS") local_as_lineNum = self.lineNum local_as_tag.implies(taglib.as_number_tag(local_as), local_as_lineNum) self.skipTo(EndOfCommand) while True: disable = False if self.accept(Whitespace) is None: for peer, peerdict in peer_dicts.items(): bgp_local_addr = None bgp_local_addr_lineNum = None peer_lineNum = peerdict.get("peer_lineNum", None) peer_group = peerdict.get("peer_group", None) if peer_group: peer_group_dict = peer_groups.get(peer_group, None) else: peer_group_dict = None peer_as = peerdict.get("peer_as", None) if peer_as is not None: peer_as_lineNum = peerdict.get("peer_as_lineNum", None) elif peer_group_dict: peer_as = peer_group_dict.get("peer_as", None); peer_as_lineNum = peer_group_dict.get("peer_as_lineNum", None) if peer_as is None: continue update_source = peerdict.get("update_source", None) update_source_lineNum = peerdict.get("update_source_lineNum", None) if update_source is None and peer_group_dict is not None: update_source = peer_group_dict.get("update_source", None) update_source_lineNum = peer_group_dict.get("update_source_lineNum", None) peer_ip = IPy.IP(peer) if update_source is None: if peer_ip.version() == 4 and bgp_router_id: bgp_local_addr = bgp_router_id bgp_local_addr_lineNum = bgp_router_id_lineNum else: update_source = 'Loopback0' update_source_lineNum = 0 if update_source is not None: bgp_local_addr_lineNum = update_source_lineNum if peer_ip.version() == 4: bgp_local_addr = if_addrs4.get(update_source) elif peer_ip.version() == 6: bgp_local_addr = if_addrs6.get(update_source) peer_tag = taglib.ip_address_tag(peer, kind="%s peer" % protocol.upper()) address_tag = taglib.ip_address_tag(peer) peer_tag.implies(address_tag, peer_lineNum) peer_as_tag = taglib.as_number_tag(peer_as, "remote AS") peer_as_tag.implies(taglib.as_number_tag(peer_as), peer_as_lineNum) if peer_as == local_as: peering_relationship = "iBGP" else: peering_relationship = "eBGP" peering_tag = taglib.tag("%s peering" % peering_relationship, "%s %s" % (device_tag.name, peer), sort_name="%s %s" % (device_tag.name, peer_tag.sort_name)) peering_tag.implies(protocol_tag, peer_lineNum) peering_tag.implied_by(device_tag, peer_lineNum) local_as_tag.implied_by(peering_tag, peer_lineNum) peer_as_tag.implied_by(peering_tag, peer_lineNum) if bgp_local_addr is not None: peer2_tag = taglib.ip_address_tag(bgp_local_addr, kind="%s peer" % protocol.upper()) address2_tag = taglib.ip_address_tag(bgp_local_addr) peer2_tag.implies(address2_tag, bgp_local_addr_lineNum) local_peer_tag = taglib.ip_address_tag(bgp_local_addr, kind="local %s peer" % protocol.upper()) local_peer_tag.implied_by(peering_tag, bgp_local_addr_lineNum) local_peer_tag.implies(peer2_tag, bgp_local_addr_lineNum) remote_peer_tag = taglib.ip_address_tag(peer, kind="remote %s peer" % protocol.upper()) remote_peer_tag.implied_by(peering_tag, peer_lineNum) remote_peer_tag.implies(peer_tag, peer_lineNum) return if self.accept(Comment) is not None: self.skipTo(EndOfCommand) continue try: op = self.accept(Operator) if op and op == "no": disable = True cmd = self.expect(Keyword) if cmd == "neighbor": peer_group = None peer = self.accept(Number) if peer is None: # if no address, then its probably a peer group peer_group = self.expect(String) peerdict = peer_groups.get(peer_group, None) if peerdict is None: peerdict = {} peer_groups[peer_group] = peerdict else: peerdict = peer_dicts.get(peer, None) if peerdict is None: peerdict = {} peerdict["peer"] = peer peerdict["peer_lineNum"] = self.lineNum peerdict["disable"] = disable peer_dicts[peer] = peerdict subcmd = self.expect(Keyword) if subcmd == "remote-as": peerdict["peer_as"] = self.expect(Literal) peerdict["peer_as_lineNum"] = self.lineNum elif subcmd == "peer-group": if peer is not None: peerdict["peer_group"] = self.accept(Literal) elif subcmd == "update-source": peerdict["update_source"] = self.expect(Literal) peerdict["update_source_lineNum"] = self.lineNum elif cmd == "bgp": subcmd = self.expect(Keyword) if subcmd == "router-id": bgp_router_id = self.accept(Literal) bgp_router_id_lineNum = self.lineNum else: self.skipTo(EndOfCommand) elif cmd == "router-id": bgp_router_id = self.accept(Literal) bgp_router_id_lineNum = self.lineNum self.skipTo(EndOfCommand) except UnexpectedToken: self.skipTo(EndOfCommand) self.skipTo(EndOfCommand)