Exemplo n.º 1
0
    def __init__(self):
        self.contents = {}
        self.db = DbConnect('pidb')
        self.db.connect()

        if 'nmspy_Russel' in os.getcwd():
            conf_file = os.getcwd() + "/portinv/bsportutil/bs-port_util.conf"
        else:
            conf_file = os.getcwd(
            ) + "/nmspy_Russel/portinv/bsportutil/bs-port_util.conf"

        self.contents = self.parseConf(conf_file)
Exemplo n.º 2
0
    def __init__(self):
        self.db = DbConnect('pidb')
        self.db.connect()

        # Data files..
        self.elist = dict()
        self.plist = dict()
        self.mask = dict()
        self.dmem = dict()
        self.hpna = dict()
        self.loop = dict()
        self.type = dict()
        self.divs = dict()

        #if 'nmspy' in os.getcwd():
        self.data_dir = os.getcwd() + "/data"
Exemplo n.º 3
0
class ArpInvShow:
    def __init__(self):
        self.db = DbConnect('pidb')
        self.db.connect()

    def getStoreSubnet(self):
        self.StoreSubnets = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
        return self.StoreSubnets

    def mainPage(self, formField):
        siteGroupValues = []
        siteGroupValues.append([self.getStoreSubnet()])

        genObj = PageGenerator('arpinvshow.tmpl')
        html = genObj.pageGenerate('', siteGroupValues)
        return html
Exemplo n.º 4
0
class ArpInvShow:
    def __init__(self):
        self.db = DbConnect('pidb')
        self.db.connect()

    def getSiteGroupValues(self):
        self.values = self.db.select_query(
            """select base,name from basexref where name <> 'unassigned';""")
        return self.values

    def mainPage(self, formField):
        #       siteGroupValues = self.getSiteGroupValues()

        genObj = PageGenerator('arpinvshow.tmpl')
        html = genObj.pageGenerate()
        return html
Exemplo n.º 5
0
    def __init__(self):
        self.msg = '' 

        self.snmp_dict = dict()
        self.site_dict = dict()
        self.arpt_dict = dict()
        self.neig_dict = dict()
        self.mact_dict = dict()
        self.port_dict = dict()
        self.hist_dict = dict()
        self.hits_dict = dict()
        self.vlan_dict = dict()
        self.caps_dict = dict()
        self.base_dict = dict()
        self.ping_dict = dict()
        self.snmpconfig = dict()
        self.descr_dict = dict()

        snmp_obj = SNMPConfig()
        self.snmpconfig = snmp_obj.getconfig()

        self.qping = "/appl/nms/bin/qping"
        self.db = DbConnect('pidb')
        self.db.connect()
Exemplo n.º 6
0
class BsPortUtil:
    def __init__(self):
        self.contents = {}
        self.db = DbConnect('pidb')
        self.db.connect()

        if 'nmspy_Russel' in os.getcwd():
            conf_file = os.getcwd() + "/portinv/bsportutil/bs-port_util.conf"
        else:
            conf_file = os.getcwd(
            ) + "/nmspy_Russel/portinv/bsportutil/bs-port_util.conf"

        self.contents = self.parseConf(conf_file)

    def parseConf(self, conf_file):
        yamlParse = yamlParser(conf_file)
        return yamlParse.parseFile()

    def getResultValues(self, sql):
        self.values = self.db.select_query(sql)
        return self.values

    def mainPage(self, formField):
        res, restmpl, error, srchval = (None, ) * 4
        query = ''
        params = {}

        if 'Sql' in formField:
            val = formField['Sql'].value
            error = 'Missing criteria for "{}"'.format(val)
            type = self.contents[val]['type']
            params['Sql'] = val
            query = self.contents[val]['sql']

            if type == 'find':
                if 'srch' in formField:
                    srchval = formField['srch'].value
                    params['srch'] = srchval

                    if srchval is not None:
                        error = None
                        sql = self.contents[val]['sql']
                        query = sql % (srchval)
            elif type == 'else':
                error = None

            if error:
                res = "<font color='red'>{}</font><br/>".format(error)
            else:
                if 'select' in query and 'limit' not in query:
                    query = query + ' limit 5000'
                res = self.getResultValues(query)
                params['header'] = self.contents[val]['fields'].split(",")

            restmpl = 'tabletmpl.tmpl'

        genObj = PageGenerator('bsportutil.tmpl')
        html = genObj.pageGenerate(restmpl,
                                   values=self.contents,
                                   resPage=res,
                                   params=params)

        return html
Exemplo n.º 7
0
class HpnaAclAsaCsv:
    def __init__(self):
        self.db = DbConnect('pidb')
        self.db.connect()

        # Data files..
        self.elist = dict()
        self.plist = dict()
        self.mask = dict()
        self.dmem = dict()
        self.hpna = dict()
        self.loop = dict()
        self.type = dict()
        self.divs = dict()

        #if 'nmspy' in os.getcwd():
        self.data_dir = os.getcwd() + "/data"
        #else:
        #    self.data_dir = os.getcwd() + "/nmspy/data"

    def getResultValues(self, sql):
        values = self.db.select_query(sql)
        return values

    def mainPage(self, formField=''):
        res = dict()
        params = dict()

        hpna, list, tftp = ('', ) * 3

        if 'hpnagrp' in formField:
            hpna = formField['hpnagrp'].value
            params['hpna'] = hpna

        if 'list' in formField:
            list = formField['list'].value
            params['list'] = list

        if 'tftp' in formField:
            tftp = formField['tftp'].value
            params['tftp'] = tftp

        targ = list

        pat = re.compile("\d+")
        mat = pat.match(list)
        if mat is None:
            targ = hpna

        if 'Counts' in targ:
            params['count'] = self.mask
        elif not hpna:
            params['count'] = self.mask

        self.load_elist()
        self.load_plist()
        self.load_hpna()

        params['hpnagrp'] = self.divs

        genObj = PageGenerator('hpnaaclasacsv.tmpl')
        html = genObj.pageGenerate(None,
                                   values=res,
                                   resPage=res,
                                   params=params)

        return html

    def load_elist(self):
        pat = re.compile("^#")
        pat1 = re.compile("(\d{4})")

        filename = self.data_dir + "/excp-list.txt"
        if os.path.exists(filename):
            fh = open(filename, "r")

            for line in fh:
                mat = pat.match(line)
                if mat:
                    continue

                arr = line.split(",")
                self.elist[arr[0]] = arr[1]

                mat1 = pat1.match(arr[0])
                if mat1:
                    site = mat1.group()
                    host = "fw%04da" % site
                    self.elist[host] = arr[1]

            fh.close()

    def load_plist(self):
        pat = re.compile("^#")

        filename = self.data_dir + "/pilot_stores"
        if os.path.exists(filename):
            fh = open(filename, "r")

            for line in fh:
                line = line.replace("\n", "")
                line = re.sub("\s+", " ", line)

                mat = pat.match(line)
                if mat:
                    continue

                arr = line.split(" ")
                if arr:
                    loc = arr[0]
                    del arr[0]

                    if loc in self.plist:
                        self.plist[loc].extend(arr)
                    elif loc:
                        self.plist[loc] = arr

            fh.close()

    def load_hpna(self):
        pat = re.compile('Dominicks')
        pat1 = re.compile('.*Retail,fw\d.*')
        pat2 = re.compile('Firewalls')
        pat3 = re.compile('^(\w{3})')

        filename = self.data_dir + "/hpna-group-mems.csv"
        if os.path.exists(filename):
            fh = open(filename, "r")

            for line in fh:
                line = line.strip()

                mat = pat.match(line)
                if mat:
                    continue

                mat1 = pat1.match(line)
                if mat1 is None:
                    continue

                mat2 = pat2.match(line)
                if mat2:
                    continue

                arr = line.split(",")
                mat3 = pat3.match(arr[0])
                if not (mat3 is None):
                    self.divs[arr[0]] = str(mat3.group()).upper()

                if arr[0] in self.mask:
                    self.mask[arr[0]] = self.mask[arr[0]] + 1
                else:
                    self.mask[arr[0]] = 1
                self.dmem[arr[1]] = arr[0]

            self.divs["    -- All -- "] = "ALL"
            self.divs["   Primary Pilots"] = "PRI-P"
            self.divs[" Extended Pilots"] = "EXP-P"
            self.divs[" Labs"] = "LABS"
            self.divs["    Counts"] = 1

            fh.close()

        pat = re.compile("^fw[0-9]\d{3}[ab],")

        filename = self.data_dir + "/hpna-inv.csv"
        if os.path.exists(filename):
            fh = open(filename, "r")

            for line in fh:
                line = line.strip()

                if 'Inactive' in line:
                    continue

                mat = pat.match(line)
                if mat is None:
                    continue

                arr = line.split(",")
                self.hpna[arr[0]] = arr[1]
                self.type[arr[0]] = 0
                self.loop[arr[0]] = arr[1]

            fh.close()
Exemplo n.º 8
0
 def __init__(self):
     self.db = DbConnect('pidb')
     self.db.connect()
Exemplo n.º 9
0
class ARPInventory:
    def __init__(self):
        self.db = DbConnect('pidb')
        self.db.connect()

    def getDivisionValues(self):
        self.values = self.db.select_query(
            """select host, fac from sitexref where host rlike '^fw[0-4]';""")
        self.divisions = []
        for value in self.values:
            self.divisions.append(value[1])

        return list(set(self.divisions))

    def getVlan(self):
        self.vlan_items = open(
            "/appl/nms/SNMSpidb/misc/st-vlanlist.dat").read().splitlines()
        self.vlan_filtered = []

        for value in self.vlan_items:
            if re.findall("""10\.x\.(\w)\.(\d+):10\.x\.\w\.(\d+)""",
                          str(value)):
                if '#' not in value:
                    self.vlan_filtered.append(value)

        #concatenated in one
        for value in self.vlan_items:
            if re.findall("""10\.x\.(\w)\.(\d+)""", str(value)) or re.findall(
                    """\S+\.(\d+)1""", str(value)):
                if '#' not in value:
                    self.vlan_filtered.append(value)

        return self.vlan_filtered

    def scan_arp(self, dnam=None):
        sql = """select a.addr from arp_data a, sitexref b, portinfo c
                    where c.host rlike '^[rs][0-4]|^fw[0-4]' and c.host = b.host
                     and a.sidx = c.sidx and b.fac rlike '{}'""".format(
            str(dnam))
        self.sql_query = self.db.select_query(sql)
        list = []
        for value in sql:
            if re.findall("""\.safeway\.com""", str(value)):
                list.append(value)

        return list

    def mainPage(self, formField):
        siteGroupValues = []
        siteGroupValues.append([self.getVlan()])
        siteGroupValues.append([self.getDivisionValues()])
        restmpl = 'tabletmpl.tmpl'
        division = ''
        results = ''
        params = {'vlan': None, 'division': None}
        if 'vlan' in formField:
            vlan = formField['vlan'].value
            params['vlan'] = vlan

        if 'division' in formField:
            division = formField['division'].value
            params['division'] = division
        else:
            division = 'Retail - Denver'
        results = self.scan_arp(division)
        genObj = PageGenerator('arpinventory.tmpl')
        html = genObj.pageGenerate(restmpl, siteGroupValues, results, params)
        return html
Exemplo n.º 10
0
class ArpInvShow:
    def __init__(self):
        self.db = DbConnect('pidb')
        self.db.connect()

    def getSiteGroupValues(self):
        self.values = self.db.select_query(
            """select distinct fac from sitexref where
                                              host rlike '^r[0-4]' and fac > ' ' or
                                              host rlike '^r[a-z]' and fac > ' ';"""
        )
        return self.values

    def getResultValues(self, sql):
        self.values = self.db.select_query(sql)
        return self.values

    def mainPage(self, formField):
        resPage = ''
        siteGroupValues = self.getSiteGroupValues()
        site, mask, vlan, restmpl = (None, ) * 4
        params = {'site': None, 'mask': '', 'vlan': ''}

        if 'site' in formField:
            site = formField['site'].value
            params['site'] = site

        if 'mask' in formField:
            mask = formField['mask'].value
            params['mask'] = mask

        if 'vlan' in formField:
            vlan = formField['vlan'].value
            params['vlan'] = vlan

        if 'site' in formField:
            if 'Retail' in formField['site'].value:
                resPage = self.show_retail(site, mask)
                restmpl = 'tabletmpl.tmpl'
            else:
                resPage = self.do_query(site, mask, vlan)
                restmpl = 'nontabletmpl.tmpl'

        show = ''
        if 'show' in formField:
            show = formField['show'].value

        if ('host' in formField
                or 'mask' in formField) and 'site' not in formField:
            if 'host' in formField:
                host = formField['host'].value

                restmpl = 'resultPage.tmpl'
                hd_det = {'hostname': host}

                return_val = self.load_xref(host, 0)

                if isinstance(return_val, dict):
                    res = self.view_host(host, vlan, return_val)
                    det, loc = ('', ) * 2

                    if host in return_val['inv']:
                        det = " ".join(return_val['inv'][host][0])

                    if host in return_val['location']:
                        loc = return_val['location'][host]

                    hd_det = {'hostname': host, 'details': det, 'loc': loc}
                else:
                    res = return_val

                genObj = PageGenerator(restmpl)
                html = genObj.resPageGenerate(restmpl, res, hd_det)

                return html
            elif 'mask' in formField:
                restmpl = 'maskResult.tmpl'

                mask = formField['mask'].value
                return_val = self.load_xref(mask, 1)
                res = self.view_mask(mask, show, vlan, return_val)

                hd_det = {}

                genObj = PageGenerator(restmpl)
                html = genObj.resPageGenerate(restmpl, res, hd_det)

                return html
            else:
                restmpl = 'usage.tmpl'
                genObj = PageGenerator('usage.tmpl')

                return (genObj.resPageGenerate(restmpl))

        genObj = PageGenerator('arpinvshow.tmpl')
        html = genObj.pageGenerate(restmpl, siteGroupValues, resPage, params)

        return html

    def show_retail(self, site=None, mask=None):
        filt = '^r[0-4]'
        sql = None

        if mask is not None:
            filt = mask

        if 'Any' in site:
            sql = """select host from hostinfo where host rlike '{}' order by host""".format(
                filt)
        else:
            sql = """select b.host, b.divna, b.dstno, b.street, b.city, b.st, b.zip
                     from sitexref a, sitelist b where a.host = b.host 
                     and a.fac = '{}' and a.host rlike '{}'
                     order by a.host""".format(site, filt)

        values = self.getResultValues(sql)
        return values

    def do_query(self, site=None, mask=None, vlan=None):
        sql = None
        filter = None

        if mask:
            filter = mask
        else:
            if 'Retail' in site:
                filter = "^r[0-4]"
            else:
                filter = "^[rs][a-z]"
            vlan = None

        if vlan is not None:
            sql = """select host from portinfo where vlan = '{}'
                     and host rlike '{}' order by host""".format(vlan, filter)
        elif 'Any' in site:
            sql = """select host from hostinfo where host rlike '{}'
                     order by host""".format(filter)
        else:
            sql = """select host from sitexref where fac = '{}'
                     and host rlike '{}' order by host""".format(site, filter)

        values = self.getResultValues(sql)
        return values

    def load_xref(self, host=None, type=0):
        vend = {}
        # Disable encoding for database.
        self.db.connect(False)

        sql = "select * from oui_xref;"
        values = self.getResultValues(sql)
        self.db.connect()

        pat = re.compile(r"[\"\']")
        pat_arr = [
            "\s*internat\S+", "\s*communica\S+", "\s*tech\S*", "\s*corp\S*",
            "\s*inc\S*"
        ]

        for tup in values:
            oui = str(tup[0])
            oui = re.sub('b\'|\'', '', oui)
            vendor = str(tup[1])
            vendor = re.sub('b\'|\'', '', vendor)

            vendor = pat.sub("", vendor)
            for regex in pat_arr:
                pat_sub = re.compile(regex, re.I)
                vendor = pat_sub.sub("", vendor)
            vend[oui] = vendor

        if type == 0:
            sql = """select count(*) from hostinfo where host='{}'""".format(
                host)
            res = self.getResultValues(sql)
            host_cnt = res[0][0]

            if not host_cnt:
                return "No matches found for host '{}'".format(host)

        if type > 0:
            mask = host

        base_dict = {}

        if re.match(r"(\d{4})", host):
            mask = re.match(r"(\d{4})", host).group()

            sql = """select base, name from basexref"""
            values = self.getResultValues(sql)

            for tup in values:
                base = tup[0]
                name = tup[1]
                base_dict[base] = name.upper()
        else:
            mask = '^[a-z][a-z][a-z]|^d2'  # Culpeper or d2 fix

        sql = """select a.host, a.addr, a.oid, a.loc, b.serialnumber
                 from hostinfo a
                 left join hpna.hpna b on a.host=b.name
                 where a.host rlike '{}'""".format(mask)

        values = self.getResultValues(sql)

        inv = {}
        mod = {}
        location = {}

        for tup in values:
            string = None

            for val in tup:
                if val is None:
                    val = ' '

                if string is None:
                    string = val
                else:
                    string = string + '::::' + val

            host, addr, oid, loc, snum = string.split("::::")

            oid = re.sub(".+MIB\.", "", str(oid))

            if re.match(r"\w+", snum):
                oid = oid + " -- SN: {}".format(snum)

            location[host] = loc

            if oid not in "Unknown":
                mod[host] = oid

            if host in inv:
                inv[host].append((addr, oid))
            else:
                inv[host] = [(addr, oid)]

        if re.match(r"\d{4}", host):
            sql = """select base, name from basexref"""
            values = self.getResultValues(sql)

            for tup in values:
                base = tup[0]
                name = tup[1]
                base_dict[base] = name.upper()

        return_dict = {
            'location': location,
            'inv': inv,
            'mod': mod,
            'base': base_dict,
            'vend': vend
        }
        return return_dict

    def view_host(self, host=None, Vlan=None, value_dict=None):
        inv = value_dict['inv']
        location = value_dict['location']
        mod = value_dict['mod']
        Base = value_dict['base']
        vnd = value_dict['vend']

        sql = "select sidx,pidx from pgrpinfo where sidx rlike '{}'".format(
            host)
        values = self.getResultValues(sql)

        pgrp = {}
        res = []

        for tup in values:
            sidx = tup[0]
            pidx = tup[1]

            pgrp[sidx] = pidx
            if pidx in pgrp:
                pgrp[pidx].append(sidx)
            else:
                pgrp[pidx] = [sidx]

        afix = {}
        pat = re.compile(r"(\d{4})")
        mat = pat.match(host)

        if mat is not None:
            val = re.match(r"(\d{4})", host).group()
            gate = "fw%sa" % (val)
            sql = """select a.mac,a.addr,b.host,a.base
                     from arp_data a left join dns_xref b on a.addr = b.addr
                     where a.sidx rlike '{}'""".format(gate)
            values = self.getResultValues(sql)

            for tup in values:
                mac = tup[0]
                addr = tup[1]
                host = tup[2]
                base = tup[3]

                if mac in afix:
                    afix[mac].append((addr, host, base))
                else:
                    afix[mac] = [(addr, host, base)]

        sql = """select a.sidx, a.port, a.vlan, b.mac, c.addr, d.host, c.base,
                 a.name as dscr, date(b.date)
                 from portinfo a
                 left join fdb_macs b on a.sidx=b.sidx
                 left join arp_data c on b.mac=c.mac
                 left join dns_xref d on c.addr=d.addr
                 where a.host = '{0}' and a.port not rlike 'vlan|trk[1-9]|snet'
                 and a.sidx not in (select sidx from cdp_neig)
                 and a.sidx not in (select sidx from portaddr)
                 # append CDP neighbors
                 union
                 select a.sidx,a.port,a.vlan,null as mac,b.addr,b.host as host,
                 null as base,null as dscr,date(b.date)
                 from portinfo a, cdp_neig b
                 where a.host = '{0}' and a.sidx = b.sidx
                 # get addressed ports
                 union
                 select a.sidx, a.port, a.vlan,null as mac, b.addr, null as host, null as base,
                 a.name as dscr, date(b.date)
                 from portinfo a, portaddr b
                 where a.host = '{0}' and a.sidx=b.sidx""".format(host)

        values = self.getResultValues(sql)

        pat_arr = [
            "^lo\d", "^eo\d", "^as\d", "^n[ue]\d", "^snet", "^trk[1-9]",
            "^inband", "^s[cl]\d", "^vlan/"
        ]
        port_pat = re.compile("^vl\d", re.I)
        host_pat = re.compile("^s\d{4}")
        port_1_pat = re.compile("^po\d", re.I)
        host_1_pat = re.compile("^s\d{4}[a-vxyz]")

        show = {}
        neig = {}
        pkey_dict = {}

        for tup in values:
            string = None
            for val in tup:
                if string is None:
                    string = val
                else:
                    string = string + "::::" + str(val)
            string = re.sub("NULL", " ", string)

            pkey, port, vlan, mac, addr, host_name, base, name, date = string.split(
                '::::')

            regex_match = 0
            for regex in pat_arr:
                desc_pat = re.compile(regex, re.I)
                if desc_pat.match(port) is not None:
                    regex_match = 1

            if regex_match == 1:
                continue

            if port_pat.match(port) is not None and host_pat.match(
                    host_name) is not None:
                continue

            if port_1_pat.match(port) is not None and host_1_pat.match(
                    host_name) is not None:
                continue

            kval = 0
            skey = None

            pat = re.compile(r":(\d+)")
            mat = pat.match(pkey)

            if mat:
                kval = mat.group(1)

            patt = re.compile(r"([A-Z])(\d+)")
            port1_pat = re.compile(r"\D*(\d+)\D(\d+)")

            mat0 = patt.match(port)
            mat1 = port1_pat.match(port)

            if mat0 is not None:
                snum = mat0.group(1)
                pnum = mat0.group(2)
                skey = "%-3s%03d.%05d." % (str(snum), int(pnum), int(kval))
            elif mat1 is not None:
                snum = mat1.group(1)
                pnum = mat1.group(2)
                skey = "%03i%03i%s%05i%s" % (int(snum), int(pnum), '.',
                                             int(kval), '.')
            else:
                val = 0
                if re.match(r"\D*(\d+)", port):
                    val = re.match(r"\D*(\d+)", port).group(1)
                skey = "%d%d.%d." % (999, int(val), int(kval))

            skey = skey + mac

            show[skey] = (pkey, port, vlan, mac, addr, host_name, base, name,
                          date)
            pkey_dict[pkey] = port

            if host_name in inv:
                neig[skey] = host_name

        bgcolor_dict = {}
        for key in show:
            hyperlink = 1
            pkey, descr, vlan, mac, addr, hostname, base, desc, date = show[
                key]

            if len(bgcolor_dict) == 0:
                bgcolor_dict[vlan] = "ffffff"
            elif vlan not in bgcolor_dict:
                md = hashlib.md5()
                md.update(str(vlan).encode('utf-8'))
                bgcolor = md.hexdigest()[1:7]
                bgcolor_dict[vlan] = bgcolor

            if mac == 'None':
                mac = ''

            if addr == 'None':
                addr = ''

            if hostname == 'None':
                hyperlink = 0
                hostname = ''

            if mac in afix:
                hyperlink = 0
                addr, hostname, base = afix[mac]

            patt = re.compile(r"\w+")
            mat = patt.match(descr)
            if mat is None:
                continue

            host, val = pkey.split(":")

            pmem = []
            if pkey in pgrp:
                for value in pgrp[pkey]:
                    if value in pkey_dict:
                        pmem.append(pkey_dict[value])
                desc = "%s -- Port Group: %s" % (desc, ", ".join(pmem))

            patt = re.compile(r"^00-07-85")
            mat = patt.match(mac)
            if mat:
                hostname = "IP Phone"
                hyperlink = 0

            if Vlan is not None:
                if vlan != Vlan:
                    continue

            slot, Port = (0, ) * 2
            patt = re.compile(r"^([A-Z])(\d+)")
            mat = patt.match(descr)

            pat1 = re.compile(r"^\s*\d+\s*$")
            mat1 = pat1.match(descr)

            pat2 = re.compile(r"[A-Za-z]*(\d+)\/(\d+)")
            mat2 = pat2.match(descr)

            if mat is not None:
                slot = (ord(mat.group(1)) - ord('A')) + 1
                Port = mat.group(2)
            elif mat1:
                slot = 0
                Port = descr
            elif mat2:
                slot = mat2.group(1)
                Port = mat2.group(2)

            if desc in inv:
                hostname = desc

            if key in neig:
                hostname = neig[key]
                hyperlink = 1

            oui = None
            vend = ''

            if mac:
                oui = mac[0:8]

            if oui is not None:
                if oui in vnd:
                    vend = str(vnd[oui])
                else:
                    vend = ''

            if hostname in inv:
                if hostname in mod:
                    vend = mod[hostname]
                    desc = "uplink to {}".format(host)
            else:
                if not hostname and base in Base:
                    hostname = Base[base]
                    hyperlink = 0

            if base in Base:
                hostname = "%s %s" % (base, hostname)
                hyperlink = 0

            if desc is not None and hostname == '':
                hostname = desc
                hyperlink = 0

            if host == hostname:
                continue

            if date != 'None':
                pat_date = re.compile("^\d+-(\d+)-(\d+)")
                mat_date = pat_date.match(date)

                if mat_date:
                    date = "%02d/%02d" % (int(
                        mat_date.group(1)), int(mat_date.group(2)))
            else:
                date = ''

            res.append([
                slot, Port, vlan, descr, mac, addr, hostname, vend, date,
                hyperlink, bgcolor_dict[vlan]
            ])

        return res

    def view_mask(self, mask=None, Show=None, Vlan=None, value_dict=None):
        inv = value_dict['inv']
        location = value_dict['location']
        mod = value_dict['mod']
        Base = value_dict['base']
        vnd = value_dict['vend']

        sql = "select sidx,pidx from pgrpinfo where sidx rlike '{}'".format(
            mask)
        values = self.getResultValues(sql)

        pgrp = {}
        res = []

        for tup in values:
            sidx = tup[0]
            pidx = tup[1]

            pgrp[sidx] = pidx
            if pidx in pgrp:
                pgrp[pidx].append(sidx)
            else:
                pgrp[pidx] = [sidx]

        afix = {}
        pat = re.compile(r"(\d{4})")
        mat = pat.match(mask)

        if mat is not None:
            val = re.match(r"(\d{4})", mask).group()
            gate = "fw%04da" % (int(val))
            sql = """select a.mac,a.addr,b.host,a.base
                     from arp_data a left join dns_xref b on a.addr = b.addr
                     where a.sidx rlike '{}'""".format(gate)
            values = self.getResultValues(sql)

            for tup in values:
                mac = tup[0]
                addr = tup[1]
                host = tup[2]
                base = tup[3]

                if mac in afix:
                    afix[mac].append((addr, host, base))
                else:
                    afix[mac] = [(addr, host, base)]

        sql = """select a.sidx, a.port, a.vlan, b.mac, c.addr, d.host, c.base,
                 a.name as dscr,b.mac is not null as pcnt, date(b.date)
                 from portinfo a
                 left join fdb_macs b on a.sidx=b.sidx
                 left join arp_data c on b.mac=c.mac
                 left join dns_xref d on c.addr=d.addr
                 where a.host rlike '{0}' and a.port not rlike 'vlan|trk[1-9]|snet'
                 and a.sidx not in (select sidx from cdp_neig)
                 # append CDP neighbors
                 union
                 select a.sidx,a.port,a.vlan,null as mac,b.addr,b.host as host,
                 null as base,null as dscr, b.addr is not null as pcnt, date(b.date)
                 from portinfo a, cdp_neig b
                 where a.host rlike '{0}' and a.sidx = b.sidx""".format(mask)

        values = self.getResultValues(sql)

        desc_pat = re.compile(
            "^lo\d|^eo\d|^as\d|^n[ue]\d|^snet|^trk[1-9]|^inband|^s[cl]\d|^vlan/",
            re.I)
        port_pat = re.compile("^vl\d", re.I)
        host_pat = re.compile("^s\d{4}")
        port_1_pat = re.compile("^po\d", re.I)
        host_1_pat = re.compile("^s\d{4}[a-vxyz]")

        show = {}
        neig = {}
        pkey_dict = {}

        for tup in values:
            string = None
            for val in tup:
                if string:
                    string = string + "::::" + str(val)
                else:
                    string = str(val)
            string = re.sub("None", "", string)

            pkey, port, vlan, mac, addr, hostname, base, name, pcnt, date = string.split(
                '::::')

            if desc_pat.match(port) is not None:
                continue

            if port_pat.match(port) is not None and host_pat.match(
                    hostname) is not None:
                continue

            if port_1_pat.match(port) is not None and host_1_pat.match(
                    hostname) is not None:
                continue

            kval = 0
            skey = None

            pat = re.compile(r":(\d+)")
            mat = pat.match(pkey)

            if mat:
                kval = mat.group(1)

            patt = re.compile(r"([A-Z])(\d+)")
            port1_pat = re.compile(r"\D*(\d+)\D(\d+)")

            mat0 = patt.match(port)
            mat1 = port1_pat.match(port)

            if mat0 is not None:
                snum = mat0.group(1)
                pnum = mat0.group(2)
                skey = "%s:%-3s%03d.%05d." % (host, str(snum), int(pnum),
                                              int(kval))
            elif mat1 is not None:
                snum = mat1.group(1)
                pnum = mat1.group(2)
                skey = "%s:%03d%03d.%05d." % (host, int(snum), int(pnum),
                                              int(kval))
            else:
                val = 0
                if re.match(r"\D*(\d+)", port):
                    val = re.match(r"\D*(\d+)", port).group(1)
                skey = "%s:%03d%03d.%05d." % (host, 999, int(val), int(kval))

            pat = re.compile(r"\d+:")
            mat = pat.match(skey)
            if mat:
                val = "%03d:" % (int(mat.group()))
                skey = re.sub("\d+:", val, skey)

            skey = skey + mac
            skey = re.sub(" ", "0", skey)

            show[skey] = (pkey, port, vlan, mac, addr, hostname, base, name,
                          date, pcnt)
            pkey_dict[pkey] = port

            if hostname in inv:
                neig[skey] = hostname

        res = []
        tooltip = ''

        for skey in show:
            pkey, descr, vlan, mac, addr, hostname, base, desc, date, pcnt = show[
                skey]

            if mac == 'None':
                mac = ''

            if addr == 'None':
                addr = ''

            if hostname == 'None':
                hostname = ''

            if mac in afix:
                addr, host, base = afix[mac][0]

            if Show == 'open' and int(pcnt) > 0:
                continue

            if Show == 'used' and pcnt == 1:
                continue

            if Show == 'cdp':
                if hostname not in inv:
                    continue

            if Vlan is not None:
                if vlan != Vlan:
                    continue

            arr = pkey.split(":")
            host = arr[0]
            Port = ":".join((host, descr))

            pmem = []
            if pkey in pgrp:
                for val in pgrp[pkey]:
                    if val in pkey_dict:
                        pmem.append(pkey_dict[val])
                desc = "%s -- Port Group: %s" % (desc, ", ".join(pmem))

            patt = re.compile(r"^00-07-85")
            mat = patt.match(mac)
            if mat is not None:
                hostname = "IP Phone"

            oui = None
            vend = ''

            if mac:
                oui = mac[0:8]

            if oui is not None:
                if oui in vnd:
                    vend = str(vnd[oui])
                else:
                    vend = ''

            if hostname in mod:
                hostname = mod[hostname]

            if skey in neig:
                hostname = neig[skey]

            if 'Po1' in descr:
                if skey in neig:
                    if neig[skey] is None:
                        continue

            if desc in inv:
                host = desc

            if skey in neig:
                hostname = neig[skey]

            if hostname in inv:
                if hostname in mod:
                    vend = mod[hostname]
                    tooltip = "Browse to neighbor"
            else:
                if not hostname and base in Base:
                    hostname = Base[base]

            if base in Base:
                tooltip = base

            port = "%s:%s" % (host, descr)

            if host == hostname:
                continue

            if date != 'None':
                pat_date = re.compile("^\d+-(\d+)-(\d+)")
                mat_date = pat_date.match(date)

                if mat_date:
                    date = "%02d/%02d" % (int(
                        mat_date.group(1)), int(mat_date.group(2)))
            else:
                date = ''

            res.append([port, vlan, mac, addr, hostname, vend, date, tooltip])

        return res
Exemplo n.º 11
0
class ArpSiteComp:
    def __init__(self):
        self.msg = '' 

        self.snmp_dict = dict()
        self.site_dict = dict()
        self.arpt_dict = dict()
        self.neig_dict = dict()
        self.mact_dict = dict()
        self.port_dict = dict()
        self.hist_dict = dict()
        self.hits_dict = dict()
        self.vlan_dict = dict()
        self.caps_dict = dict()
        self.base_dict = dict()
        self.ping_dict = dict()
        self.snmpconfig = dict()
        self.descr_dict = dict()

        snmp_obj = SNMPConfig()
        self.snmpconfig = snmp_obj.getconfig()

        self.qping = "/appl/nms/bin/qping"
        self.db = DbConnect('pidb')
        self.db.connect()

    def getResultValues(self, sql):
        values = self.db.select_query(sql)
        return values

    def mainPage(self, formField=''):
        res = dict()
        params = dict()
        boot_dict = dict()

        site, ping, boot, filt = (None,) * 4

        if 'site' in formField:
            site = int(formField['site'].value)
            params['site'] = site
        #site = 107

        if 'ping' in formField:
            ping = formField['ping'].value
            params['ping'] = ping
        #ping = 'Poll'

        if 'boot' in formField:
            boot = formField['boot'].value
            params['boot'] = boot
        #boot = 'boot'

        if 'filt' in formField:
            filt = formField['filt'].value
            params['filt'] = filt
        #filt = 'none'

        res_list = list()
        if site:
            t1 = time.time()
            self.load_name()

            if boot:
                boot_dict = self.load_boot(site)

            self.load_real(site, ping)
            if filt:
                self.load_hist(site, filt)
            res_list = self.show_comp(ping, filt)

            t2 = time.time()
            t3 = t2-t1
            self.msg = self.msg + \
                       "<br/><br/>Total time: %d seconds" % t3

        res = {'boot': boot_dict,
               'msg': self.msg,
               'site': self.site_dict,
               'arp': self.arpt_dict,
               'res': res_list}

        genObj = PageGenerator('arpsitecomp.tmpl')
        html = genObj.pageGenerate(None,
                                   values=res,
                                   resPage=res,
                                   params=params)

        return html

    def load_name(self):
        sql = "select base, name from basexref"

        values = self.getResultValues(sql)
        for value in values:
            base = value[0]
            name = value[1]
            self.base_dict[base] = name

    def load_boot(self, site):
        poll = {}
        show = {}

        sql = """select host, addr, uptime from hostinfo
                 where mid(host, 2, 4) = {}""".format(site)

        values = self.getResultValues(sql)

        for value in values:
            host = value[0]
            addr = value[1]
            uptime = value[2]

            poll[host] = addr
            show[host] = ['unknown']

        pat = re.compile(".*sysUpTimeInstance (.*)")
        for host in poll:
            addr = poll[host]

            msg = """Current Network Device Uptimes for Store {}""" \
                .format(site)

            cmd = "{} snmpget {} sysUpTime.0 2>&1"\
                .format(shellcmd, addr)

            output = str(runSNMPCmd(cmd))
            output = output.replace('b\'', '')
            output = output.replace('\\r\\n\'', '')

            mat = pat.match(output)
            if mat is not None:
                uptime = mat.group(1)
                arr = uptime.split(":")
                show[host] = arr
        return show

    def load_real(self, site=None, ping=None):
        addr = '' 
        host = "r%04d" % (site)

        t1 = time.time()

        try:
            addr = str(socket.gethostbyname(host))
        except Exception as err:
            pass

        if re.match("^10\.\d+\.\d+\.\d+", addr) is None:
            self.msg = self.msg +\
                       "<br/><br/>Unable to resolve router address for store {}" \
                .format(site)
            return

        self.site_ping(site)
        t2 = time.time()
        t3 = t2 - t1

        self.msg = self.msg + \
                   ". . . Completed site ping in %d seconds . . ."\
                   % (int(t3))

        if ping:
            pat = re.compile("poll", re.I)
            mat = pat.match(ping)
            if mat is None:
                return

            self.msg = self.msg + "<br/><br/>Repolling FDB . . . "
            t1 = time.time()
            self.process() 
            t2 = time.time()
            t4 = t2 - t1
            self.msg = self.msg + \
                       "Completed repolling from device in %d seconds . . ."\
                       % (int(t4))

    def site_ping(self, site):
        sneta, snete, addr = (None,) * 3
        gate = "r%04d" % (site)

        try:
            addr = str(socket.gethostbyname(gate))
        except Exception as err:
            pass

        self.site_dict[gate] = addr
        pat = re.compile("10\.(\d+)\.(\d+)\.\d+")
        mat = pat.match(addr)

        if pat.match(addr):
            oct = int(mat.group(1))
            oct1 = int(mat.group(2))

            sneta = "10.%d.%d.0/22" % (oct, oct1)
            snete = "10.%d.%d.0/22" % (int(oct) - 64, oct1)

        self.msg = "Pinging {} {} and {}".format(site,
                                                 sneta,
                                                 snete)
        data = ''

        qping = self.qping

        cmd_snete = "{} {} -A -a -g {} 2>&1".format(shellcmd,
                                                    qping,
                                                    snete)
        cmd_sneta = "{} {} -A -a -g {} 2>&1".format(shellcmd,
                                                    qping,
                                                    sneta)

        q1 = Queue()
        q2 = Queue()
        q3 = Queue()

        p1 = Process(target=runSNMPCmd, args=(cmd_sneta, q1))
        p2 = Process(target=runSNMPCmd, args=(cmd_snete, q2))
        p3 = Process(target=func_gate, args=(gate, q3))

        p1.start()
        p2.start()
        p3.start()

        data = data + str(q1.get())
        data = data.replace("b'", "")
        data = data.replace("'", "")
        p1.join()

        data1 = str(q2.get())
        data1 = data1.replace("b'", "")
        data1 = data1.replace("'", "")
        data = data + data1
        p2.join()

        self.arpt_dict = q3.get()
        p3.join()

        arr = data.split("\\r\\n")
        for value in arr:
            pat = re.compile("^(1\S+)$")
            mat = pat.match(value)

            if mat:
                addr = mat.group()
                base = cal_base(addr)

                if addr in self.ping_dict:
                    self.ping_dict[addr] =\
                        self.ping_dict[addr] + 1
                else:
                    self.ping_dict[addr] = 1

                pat1 = re.compile("10.x.a.([34][0-9])")
                mat1 = pat1.match(base)

                if mat1:
                    offs = int(mat1.group(1))
                    if offs == 34:
                        host = "s%04dwl" % (int(site))
                        self.site_dict[host] = addr
                    elif offs > 34:
                        host = "s%04d%s" % (site, chr(97 + offs - 35))
                        self.site_dict[host] = addr

                if base == "10.x.a.196":
                    host = "fw%04da" % (site)
                    self.site_dict[host] = addr

    def process(self):
        q1 = Queue()
        q2 = Queue()
        q3 = Queue()

        for host in self.site_dict:
            descr_dict = dict()
            port_dict = dict()
            neig_dict = dict()
            arpt_dict = dict()

            ver = self.test_snmp(host)

            p1 = Process(target=slot_xref, args=(q1, host, ver,))
            p1.start()

            p2 = Process(target=dump_cdp, args=(q2, host, ver,))
            p2.start()

            p3 = Process(target=dump_arp, args=(q3, host, ver,))
            p3.start()

            arr = q1.get()
            if arr[0]:
                port_dict = arr[0]

            if arr[1]:
                descr_dict = arr[1]
            p1.join()

            neig_dict = q2.get()
            p2.join()

            arpt_dict = q3.get()
            p3.join()

            self.port_dict.update(port_dict)
            self.descr_dict.update(descr_dict)
            self.neig_dict.update(neig_dict)
            self.arpt_dict.update(arpt_dict)

            if re.match("^s", host):
                self.walk_vlans(host, ver,
                                port_dict,
                                descr_dict,
                                neig_dict,
                                arpt_dict)

    def test_snmp(self, host):
        self.snmp_dict[host] = self.snmpconfig['v']

        cmd = "{} snmpget -v {} {} system.sysName.0 2>&1" \
            .format(shellcmd, self.snmp_dict[host], host)
        resp = runSNMPCmd(cmd)

        pat = re.compile("sysName")
        mat = pat.match(str(resp))

        if mat is None:
            if self.snmpconfig['v'] == "3":
                self.snmp_dict[host] = '2c'
            else:
                self.snmp_dict[host] = '3'

            resp = "{} snmpget -v {} {} system.sysName.0 2>&1" \
                .format(shellcmd, self.snmp_dict[host], host)

        ver = "-v %s" % (self.snmp_dict[host])

        return ver

    def load_hist(self, site=None, filter=None):
        site_name = "%04d" % (site)
        gate = "r%04d" % (site)

        addr = '' 
        snetA, snetE = (None,) * 2

        try:
            addr = str(socket.gethostbyname(gate))
        except Exception as err:
            pass

        pat = re.compile("10\.(\d+)\.(\d+)\.\d+")
        mat = pat.match(addr)

        if mat is not None:
            oct2 = int(mat.group(1))
            oct3 = int(mat.group(2))
            snetA = "10.%d.%d.0" % (oct2, oct3)
            oct2 = oct2 - 64
            snetE = "10.%d.%d.0" % (oct2, oct3)

        qry = """select addr, host from cdp_else 
                 where host rlike '{}'""".format(site)
        values = self.getResultValues(qry)

        for value in values:
            ip_addr = value[0]
            hostname = value[1]
            self.caps_dict[ip_addr] = hostname

        qry = """select a.host, b.mac, a.port, a.vlan,
                   c.addr, d.host, c.base, b.date
                  from portinfo a
                  left join fdb_macs b on a.sidx=b.sidx
                  left join arp_data c on b.mac=c.mac
                  left join dns_xref d on c.addr=d.addr
                  where a.host rlike '{}'
                  and a.port not rlike 'vlan|trk[1-9]|snet'
                  and a.sidx not in (select sidx from cdp_neig
                     where host not rlike '^ap[0-9]')
                  union
                  select a.host, null as mac, a.port,
                    a.vlan, b.addr, b.host as host,
                    null as base, b.date
                  from portinfo a, cdp_neig b
                  where a.host rlike '{}' and a.sidx = b.sidx
                  and b.host not rlike '^ap[0-9]'""".format(site,
                                                            site)

        values = self.getResultValues(qry)

        pat = re.compile("^s\d{4}[a-vxyz]")
        pat1 = re.compile("^po\d", re.I)
        pat2 = re.compile("^\d+")
        pat3 = re.compile("^s\d+")
        pat4 = re.compile("\w+")
        pat5 = re.compile("^" + re.escape(filter))
        pat6 = re.compile("^([A-Za-z]+)\d+")

        test_dict = dict()
        pcnt_dict = dict()

        patt = None 
        if snetA and snetE:
            stri = re.escape(snetA) + r"|" + re.escape(snetE)
            patt = re.compile(stri)

        for value in values:
            host = value[0]
            mac = str(value[1])
            port = value[2]
            ip_addr = value[4]
            base = value[6]

            mat = pat.match(host)
            mat1 = pat1.match(port)

            if mat is not None and mat1 is not None:
                continue

            if ip_addr:
                mat2 = pat2.match(ip_addr)
                if mat2 is not None:
                    nettoa1 = ip_addr.split(".") 
                    nettoa2 = str("255.255.252.0").split(".")

                    oct0 = int(nettoa1[0]) & int(nettoa2[0])
                    oct1 = int(nettoa1[1]) & int(nettoa2[1])
                    oct2 = int(nettoa1[2]) & int(nettoa2[2])
                    oct3 = int(nettoa1[3]) & int(nettoa2[3])

                    soc_value = "%d.%d.%d.%d" % (oct0, oct1,
                                                 oct2, oct3)

                if patt: 
                    matt = patt.match(soc_value)
                    if matt is None:
                        continue

            mat3 = pat3.match(host)
            if mat3 is None:
                continue

            mat4 = pat4.match(mac)
            if mat4 is None:
                continue

            pkey = ''
            if port:
                arr = re.split("\D+", port)

                mat6 = pat6.match(port)
                if mat6:
                    arr[0] = mat6.group(1)

                if len(arr) == 4:
                    pkey = "%s:%s:%02d:%02d:%02d" % (host,
                                                     str(arr[0]),
                                                     int(arr[1]),
                                                     int(arr[2]),
                                                     int(arr[3]))
                elif len(arr) == 3:
                    pkey = "%s:%s:%02d:%02d" % (host,
                                                str(arr[0]),
                                                int(arr[1]),
                                                int(arr[2])) 

            if ip_addr:
                arr1 = ip_addr.split("\.")
                if len(arr1) >= 4:
                    ip_addr = "%03d.%03d.%03d.%03d" % (arr1[0],
                                                       arr1[1],
                                                       arr1[2],
                                                       arr1[3])

            # Test this line of code
            if base:
                mat5 = pat5.match(base)
                if filter not in 'none' \
                        and mat5 is None:
                    continue

            hkey = ''
            if ip_addr:
                stri = ''
                hkey = pkey + ',' + ip_addr
                for val in value:
                    stri = stri + "," + str(val)
                self.hist_dict[hkey] = pkey + stri

            if ip_addr in self.hits_dict:
                self.hits_dict[ip_addr] = \
                    self.hits_dict[ip_addr] + 1
            else:
                self.hits_dict[ip_addr] = 1

            test_dict[pkey] = hkey

            if pkey in pcnt_dict:
                pcnt_dict[pkey] =\
                    pcnt_dict[pkey] + 1
            else:
                pcnt_dict[pkey] = 1

        for hkey in self.hist_dict:
            arr = hkey.split(",")
            if arr[0] in pcnt_dict and arr[0] != '':
                if pcnt_dict[arr[0]] > 1:
                    self.hist_dict[hkey] = \
                        self.hist_dict[hkey] + ",o"

    def show_comp(self, ping=None, filt=None):
        neig_dict = self.neig_dict
        upl = dict()
        old = dict()

        for value in neig_dict:
            host = value.split(":")
            neig = neig_dict[value][0][0]
            addr = neig_dict[value][0][1]
            hkey = ''

            if value in self.descr_dict:
                hkey = host[0] + ":" + \
                       self.descr_dict[value]

            if hkey not in upl:
                upl[hkey] = neig

            if hkey not in self.hist_dict: 
                base = cal_base(addr)
                #self.hist_dict[hkey] = "{},{},{},,{},,{},{},{},,"\
                #    .format(host[0], self.descr_dict[value],
                #            addr, neig, base)

        for value in self.hist_dict:
            arr = self.hist_dict[value].split(",")
            if len(arr) >= 3:
                mac = arr[3]
                if mac in self.mact_dict:
                    if mac in old:
                        old[mac] = old[mac] + 1
                    else:
                        old[mac] = 1

        pat = re.compile(r"^s(\w+):\D+(\d+).(\d+)")

        for mac in self.mact_dict:
            if mac in old:
                if old[mac] >= 1:
                    mat = pat.match(self.mact_dict[mac])
                    if not (mat is None):
                        host = mat.group(1)
                        slot = int(mat.group(2))
                        port = int(mat.group(3))
                        pkey = "%s:%02d:%07.4f" % (host, slot, port)

                        key = pkey + "," + mac 
                        if pkey in self.vlan_dict:
                            if mac in self.arpt_dict:
                                val = "{},{},{},unknown,{},{},,{},,n"\
                                    .format(pkey, host, mac,
                                            self.vlan_dict[pkey],
                                            self.arpt_dict[mac][0],
                                            self.arpt_dict[mac][1])
                                self.hist_dict[key] = val 

        pat = re.compile("^s\d{4}(\w)")
        date_pat = re.compile("^\d{,4}-(.*):\d{,2}$")

        res = list()
        for value in self.hist_dict:
            bg = 'white'
            name = ''
            prev = ''
            curr = ''
            name_descr = ''

            arr = self.hist_dict[value].split(",")
            if arr[0] is None or arr[0] == '':
                continue

            addr = arr[5]
            base = arr[7]
            descr = arr[3]

            #if addr > "10" and base <= ' ':
            #    continue

            if base in self.base_dict:
                if 'Handheld' in self.base_dict[base]:
                    continue
                name_descr = self.base_dict[base]
   
            if 'none' not in filt: 
                pat1 = re.compile(re.escape(filt))
                mat1 = pat1.match(base)
                if mat1 is None:
                    continue

            if addr in self.caps_dict:
                name = self.caps_dict[addr]

            if name is None or name == '':
                name = arr[6]
                if name is None:
                    name = ''

            if ',o' in self.hist_dict[value]:
                bg = 'yellow'

            if ',n' in self.hist_dict[value]:
                bg = 'cyan'
                prev = descr
            else:
                prev = arr[1] + ':' + descr

            if arr[2] in self.mact_dict:
                if self.mact_dict[arr[2]] not in prev:
                    curr = self.mact_dict[arr[2]]

            if prev in upl:
                if upl[prev] not in name:
                    curr = upl[prev]

            mat = pat.match(name)
            if mat:
                suf = ord(mat.group(1))
                if suf == 119:
                    base = "10.x.a.%d" % (34)
                else:
                    suf = suf - 97 + 35 
                    base = "10.x.a.%d" % (int(suf))

            date_mat = date_pat.match(arr[8])
            if date_mat:
                date = date_mat.group(1)

            # arr[2] is the Mac address
            mac =  arr[2]
            fontcolor = 'black'

            if ping and addr not in self.ping_dict:
                fontcolor = 'red'
                bg = 'lightgray'

            res.append([addr, mac, prev, curr,
                        arr[4], name, name_descr,
                        date, bg, fontcolor])

        self.msg = self.msg + "<br/><br/> Total records: {}"\
            .format(str(len(res)))

        return res

    def walk_vlans(self, host=None, ver=None,
                   port_dict=None, descr_dict=None,
                   neig_dict=None, arpt_dict=None):
        pas = '' 
        pnum = list()

        vnum_dict = dict()
        xfdb_dict = dict()
        mact_dict = dict()

        cmd_1 = "{} snmpbulkwalk {} {} CISCO-VLAN-MEMBERSHIP-MIB::vmVlan 2>&1" \
            .format(shellcmd, ver, host)

        cmd_2 = "{} snmpbulkwalk {} {} ENTITY-MIB::entLogicalContextName 2>&1" \
            .format(shellcmd, ver, host)

        cmd_3 = "{} snmpbulkwalk {} {} BRIDGE-MIB::dot1dBasePortIfIndex 2>&1" \
            .format(shellcmd, ver, host)

        q1 = Queue()
        q2 = Queue()
        q3 = Queue()

        p1 = Process(target=runSNMPCmd, args=(cmd_1, q1,))
        p2 = Process(target=runSNMPCmd, args=(cmd_2, q2,))
        p3 = Process(target=runSNMPCmd, args=(cmd_3, q3,))

        p1.start()
        p2.start()
        p3.start()

        data_1 = str(q1.get())
        p1.join()

        data_2 = str(q2.get())
        p2.join()

        data_3 = str(q3.get())
        p3.join()

        pat = re.compile(".*::vmVlan\.(\d+) (\d+)")
        pat1 = re.compile("\D+")

        data_1 = data_1.replace("b'", "")
        data_1 = data_1.replace("'", "")

        arr = data_1.split("\\r\\n")
        for value in arr:
            pnum = list()

            mat = pat.match(value)
            if mat:
                sidx = mat.group(1)
                vlan = mat.group(2)

                if vlan in vnum_dict:
                    vnum_dict[vlan] = \
                        vnum_dict[vlan] + 1
                else:
                    vnum_dict[vlan] = 1

                if sidx in self.port_dict:
                    mat1 = pat1.match(port_dict[sidx])
                    if mat1:
                        pnum = port_dict[sidx].replace(mat1.group(), "") \
                            .split("/")

                    if len(pnum) >= 3:
                        pkey = "%s:%02d:%02d:%02d" % (host,
                                                      int(pnum[0]),
                                                      int(pnum[1]),
                                                      int(pnum[2]))
                        self.vlan_dict[pkey] = vlan

        pat = re.compile(".*::entLogicalContextName.+ vlan-(\d+)")

        data_2 = data_2.replace("b'", "")
        data_2 = data_2.replace("'", "")
        arr = data_2.split("\\r\\n")

        for value in arr:
            mat = pat.match(value)
            if mat is not None:
                if mat.group(1) in vnum_dict:
                    vnum_dict[mat.group(1)] = \
                        vnum_dict[mat.group(1)] + 1
                else:
                    vnum_dict[mat.group(1)] = 1

        pat = re.compile(".*dot1dBasePortIfIndex\.(\d+) (\d+)")
        pat1 = re.compile("^Po")

        data_3 = data_3.replace("b'", "")
        data_3 = data_3.replace("'", "")
        arr = data_3.split("\\r\\n")

        for value in arr:
            mat = pat.match(value)
            if mat is not None:
                sidx = mat.group(1)
                port = mat.group(2)

                if port in port_dict:
                    mat1 = pat1.match(port_dict[port])
                    if not (mat1 is None):
                        xfdb_dict[sidx] = port

        if vnum_dict:
            for vlan in vnum_dict:
                if host in self.snmp_dict:
                    if self.snmp_dict[host] == 3:
                        pas = "******" % (vlan)
                    else:
                        if 'c' in self.snmpconfig:
                            pas = "******" % \
                                  (self.snmpconfig['c'], int(vlan))
                            mact_dict = dump_ports(host, ver, pas,
                                                   xfdb_dict, descr_dict,
                                                   port_dict, neig_dict)
        else:
            mact_dict = dump_ports(host, ver, pas,
                                   xfdb_dict, descr_dict,
                                   port_dict, neig_dict)

        self.mact_dict.update(mact_dict)
Exemplo n.º 12
0
class MacPidbSrch:
    def __init__(self):
        self.contents = {}
        self.db = DbConnect('pidb')
        self.db.connect()

    def getResultValues(self, sql):
        self.values = self.db.select_query(sql)
        return self.values

    def mainPage(self, formField):
        arr = []
        res = {}
        rad = None
        params = {}

        if 'type' in formField:
            rad = formField['type'].value
            params['type'] = rad

        if 'srch' in formField:
            data = formField['srch'].value
            arr = data.split("\n")
            params['srch'] = data

        if 'file' in formField:
            datafile = formField['file']
            if datafile.file:
                params['file'] = datafile.file
                while 1:
                    line = str(datafile.file.readline(), 'utf-8')
                    if not line:
                        break
                    else:
                        line = re.sub("^\s+", "", line)
                        line = re.sub("\s+$", "", line)
                        arr.append(line)

        arr_str = ",".join(arr)
        srch_dict = self.prse_raw(arr_str)

        if len(srch_dict):
            base_dict, vend = self.load_xref()
            srch = copy.deepcopy(srch_dict)

            for seg in srch_dict:
                for type in srch_dict[seg]:
                    if type not in 'all':
                        if len(srch_dict[seg][type]):
                            result = None
                            if rad == 'ARP':
                                result = self.srch_arp(seg, type, srch,
                                                       base_dict, vend)

                            if rad == 'MAC':
                                result = self.srch_fdb(seg, type, srch,
                                                       base_dict, vend)

                            if result:
                                if res:
                                    res.update(result)
                                else:
                                    res = result

        res = self.checkDuplicates(res)

        genObj = PageGenerator('macaddrsrch.tmpl')
        html = genObj.pageGenerate(None,
                                   values=self.contents,
                                   resPage=res,
                                   params=params)

        return html

    def checkDuplicates(self, res=None):
        res_dict = {}
        arr = list(res.keys())
        res_values = set(res.values())

        for index, value in enumerate(res_values):
            key = arr[index]
            res_dict[key] = value

        return res_dict

    def prse_raw(self, data):
        segs, hits = (0, ) * 2
        hostname = None
        srch = {}
        arr = data.split(",")

        for val in arr:
            if val is None or val == '':
                continue

            inner_dict = {'all': {}, 'mac': {}, 'addr': {}, 'name': {}}
            if val in inner_dict:
                inner_dict['all'][val] = inner_dict['all'][val] + 1
            else:
                inner_dict['all'][val] = 1

            pat = re.compile("^\s+|\s+$")
            val = pat.sub("", val)

            if val is None or val is '':
                continue

            if val in inner_dict['all']:
                inner_dict['all'][val] = inner_dict['all'][val] + 1
            else:
                inner_dict['all'][val] = 1

            pat = re.compile(
                "(\w{2})[:,-](\w{2})[:,-](\w{2})[:,-](\w{2})[:,-](\w{2})[:,-](\w{2})"
            )
            pat1 = re.compile("(\w{2})(\w{2})\.(\w{2})(\w{2})\.(\w{2})(\w{2})")
            pat2 = re.compile("^(\w{2})[:,-](\w{2})[:,-](\w{2})$")
            pat3 = re.compile("^(\w{2})(\w{2})\.(\w{2})$")
            ip_pat = re.compile("(\d+\.\d+\.\d+\.\d+)")

            mat = pat.match(val)
            mat1 = pat1.match(val)
            mat2 = pat2.match(val)
            mat3 = pat3.match(val)
            ip_mat = ip_pat.match(val)
            host_mat = re.match("([\w-]+)", val)

            if mat is not None:
                mac = "%02s-%02s-%02s-%02s-%02s-%02s" %\
                      (mat.group(1), mat.group(2), mat.group(3),
                       mat.group(4), mat.group(5), mat.group(6))

                if mac in inner_dict['mac']:
                    inner_dict['mac'][mac] = inner_dict['mac'][mac] + 1
                else:
                    inner_dict['mac'][mac] = 1

                if mac in inner_dict['all']:
                    inner_dict['all'][mac] = inner_dict['all'][mac] + 1
                else:
                    inner_dict['all'][mac] = 1
                hits = hits + 1
            elif mat1 is not None:
                mac = "%02s-%02s-%02s-%02s-%02s-%02s" %\
                      (mat1.group(1), mat1.group(2), mat1.group(3),
                       mat1.group(4), mat1.group(5), mat1.group(6))
                if mac in inner_dict['mac']:
                    inner_dict['mac'][mac] = inner_dict['mac'][mac] + 1
                else:
                    inner_dict['mac'][mac] = 1

                if mac in inner_dict['all']:
                    inner_dict['all'][mac] = inner_dict['all'][mac] + 1
                else:
                    inner_dict['all'][mac] = 1
                hits = hits + 1
            elif mat2 is not None:
                mac = "%02s-%02s-%02s" % \
                      (mat2.group(1), mat2.group(2), mat2.group(3))
                if mac in inner_dict['oui']:
                    inner_dict['oui'][mac] = inner_dict['oui'][mac] + 1
                else:
                    inner_dict['oui'][mac] = 1

                if mac in inner_dict['all']:
                    inner_dict['all'][mac] = inner_dict['all'][mac] + 1
                else:
                    inner_dict['all'][mac] = 1
                hits = hits + 1
            elif mat3 is not None:
                mac = "%02s-%02s-%02s" % \
                      (mat3.group(1), mat3.group(2), mat3.group(3))
                if mac in inner_dict['oui']:
                    inner_dict['oui'][mac] = inner_dict['oui'][mac] + 1
                else:
                    inner_dict['oui'][mac] = 1

                if mac in inner_dict['all']:
                    inner_dict['all'][mac] = inner_dict['all'][mac] + 1
                else:
                    inner_dict['all'][mac] = 1
                hits = hits + 1
            elif ip_mat is not None:
                addr = ip_mat.group(1)
                try:
                    value = socket.gethostbyaddr(addr)
                    hostname = value[0]
                except Exception as err:
                    hostname = 'Unknown Host'

                if 'safeway.com' in hostname:
                    hostname = hostname.replace('safeway.com', '')
                inner_dict['addr'][addr] = hostname
                inner_dict['all'][addr] = hostname
                hits = hits + 1

                if hostname:
                    inner_dict['name'][hostname] = addr
            elif host_mat is not None:
                hostaddr = None
                hostname = host_mat.group(1)
                try:
                    hostaddr = socket.gethostbyname(hostname)
                except Exception as err:
                    pass

                if hostaddr:
                    mat = re.match("\d+\.\d+\.\d+\.\d+", hostaddr)
                    if mat:
                        inner_dict['name'][hostname] = hostaddr
                        inner_dict['addr'][hostaddr] = hostname
                        inner_dict['all'][hostaddr] = hostname
                    else:
                        inner_dict['name'][hostname] = hostname
                        inner_dict['all'][hostname] = hostname

            if inner_dict['all']:
                if segs in srch:
                    srch[segs]['all'].update(inner_dict['all'])
                    srch[segs]['mac'].update(inner_dict['mac'])
                    srch[segs]['name'].update(inner_dict['name'])
                    srch[segs]['addr'].update(inner_dict['addr'])
                else:
                    srch[segs] = inner_dict

                if hits >= 15:
                    hits = 0
                    segs = segs + 1

        return srch

    def load_xref(self):
        vend = {}
        sql = """select * from oui_xref"""

        pat_arr = [
            "(\w+\s+\w+)\s+.+", "\s*internat\S+", "\s*communica\S+",
            "\s*tech\S*", "\s*corp\S*", "\s*inc\S*", "\w*ltd"
        ]
        self.db.connect(False)
        values = self.getResultValues(sql)
        self.db.connect()

        for tup in values:
            oui = tup[0]
            vendor = tup[1]
            pat = re.compile(r"[\"\']")

            oui = str(tup[0])
            oui = re.sub('b\'|\'', '', oui)
            vendor = str(tup[1])
            vendor = re.sub('b\'|\'', '', vendor)
            vendor = pat.sub("", vendor)

            for regex in pat_arr:
                pat_sub = re.compile(regex, re.I)
                vendor = pat_sub.sub("", vendor)
                vend[oui] = vendor

        vend['00-00-5E-00-01'] = "VRRP MAC Address"
        sql = """select base, name from basexref"""
        values = self.getResultValues(sql)

        base_dict = {}
        for tup in values:
            base = tup[0]
            name = tup[1]
            base_dict[base] = name

        return base_dict, vend

    def type_mac(self, srch_dict):
        mac_list, sql = (None, ) * 2
        seg = srch_dict['seg']

        for val in srch_dict[seg]['mac']:
            if mac_list is None:
                mac_list = val
            else:
                mac_list = mac_list + "','" + val

            sql = """select b.host, b.port, b.vlan, a.mac, c.addr,
                     d.host, b.name, date(b.date), c.base
                     from fdb_macs a
                     left join portinfo b on a.sidx=b.sidx
                     left join arp_data c on a.mac=c.mac
                     left join dns_xref d on c.addr=d.addr
                     where a.mac in ('{}')""".format(mac_list)

        return sql

    def type_oui(self, srch_dict):
        oui_list, sql = (None, ) * 2
        seg = srch_dict['seg']

        for val in srch_dict[seg]['oui']:
            if oui_list is None:
                oui_list = val
            else:
                oui_list = oui_list + "|^" + val

            sql = """select b.host, b.port, b.vlan, a.mac, c.addr,
                     d.host, b.name, date(b.date), c.base
                     from fdb_macs a
                     left join portinfo b on a.sidx=b.sidx
                     left join arp_data c on a.mac=c.mac
                     left join dns_xref d on c.addr=d.addr
                     where a.mac rlike '^{}'""".format(oui_list)

        return sql

    def type_addr(self, chg, srch_dict):
        addr_list, sql = (None, ) * 2
        seg = srch_dict['seg']

        for val in srch_dict[seg]['addr']:
            if addr_list is None:
                addr_list = val
            else:
                addr_list = addr_list + "','" + val

            if chg == '1':
                sql = """select b.host, b.port, b.vlan, a.mac,
                         c.addr, d.host, b.name, date(b.date), c.base
                         from arp_data c
                         left join fdb_macs a on a.mac=c.mac
                         left join portinfo b on a.sidx=b.sidx
                         left join dns_xref d on c.addr=d.addr
                         where c.addr in ('{}')""".format(addr_list)
            else:
                sql = """select b.host, b.port, b.vlan, c.mac, c.addr,
                        d.host, b.name, date(b.date), c.base
                        from arp_data c
                        left join portinfo b on b.sidx=c.sidx
                        left join dns_xref d on c.addr=d.addr
                        where c.addr in ('{}')""".format(addr_list)

        return sql

    def type_name(self, srch_dict):
        nlist, hlist, sql = (None, ) * 3
        seg = srch_dict['seg']

        for val in srch_dict[seg]['name']:
            if nlist:
                nlist = nlist + "','" + val
                hlist = hlist + "|" + val
            else:
                nlist = val
                hlist = val

        sql = """select b.host, b.port, b.vlan, c.mac, c.addr,
                   d.host, b.name, date(b.date), c.base
                   from dns_xref d
                   left join arp_data c on d.addr=c.addr
                   left join fdb_macs a on c.mac=a.mac
                   left join portinfo b on a.sidx=b.sidx
                   where d.host in ('{}')
                   union
                   select b.host,b.port,b.vlan,b.mac,b.addr,
                   b.name,a.name,b.base,date(b.date)
                   from portinfo a, snapshot b
                   where a.name rlike '{}'
                   and a.sidx=b.sidx""".format(nlist, hlist)

        return sql

    def compute(self, method='fdb', values=None, srch_dict=None, vend=None):
        data = {}
        hits = 0

        if srch_dict:
            seg = srch_dict['seg']

        if not values:
            return data

        pat = re.compile("^(\w{2}-\w{2}-\w{2})")
        pat1 = re.compile("^00-00-5e-00-01-(\w+)", re.I)
        pat2 = re.compile("\w+")

        for val in values:
            manf = ''
            hits = hits + 1
            string = ",".join(str(value) for value in val)
            host, port, vlan, mac, addr, name, desc, date, base = string.split(
                ",")

            mat = pat.match(mac)
            if mat is not None:
                oui = mat.group()
                if oui in vend:
                    manf = vend[oui]

            mat = pat1.match(mac)
            if mat is not None:
                vid = mat.group()
                manf = "VRRP ID %s MAC" % (vid)

            mat = pat2.match(name)
            if mat is None:
                if base in base_dict:
                    name = base_dict[base]

            if ":" in host:
                host, unknown = host.split(":")

            key = "a%03d-%s" % (hits, mac)
            value = "\t".join(
                (mac, host, port, vlan, addr, name, desc, manf, date))

            if method == 'fdb':
                data[mac] = value
            else:
                data[key] = value

            srch_dict[seg]['all'].pop(mac, None)
            srch_dict[seg]['all'].pop(addr, None)
            srch_dict[seg]['all'].pop(name, None)

        return data

    def srch_arp(self, seg, type, srch_dict, base_dict, vend):
        if type == 'name':
            return

        sql = ''

        if type == 'mac':
            srch_dict['seg'] = seg
            sql = self.type_mac(srch_dict)

        if type == 'addr':
            srch_dict['seg'] = seg
            sql = self.type_addr('0', srch_dict)

        if type == 'oui':
            srch_dict['seg'] = seg
            sql = self.type_oui(srch_dict)

        values = self.getResultValues(sql)
        data = self.compute('arp', values, srch_dict, vend)

        return data

    def srch_fdb(self, seg, type, srch_dict, base_dict, vend):
        sql = ''

        if type == 'mac':
            srch_dict['seg'] = seg
            sql = self.type_mac(srch_dict)

        if type == 'name':
            srch_dict['seg'] = seg
            sql = self.type_name(srch_dict)

        if type == 'oui':
            srch_dict['seg'] = seg
            sql = self.type_oui(srch_dict)

        if type == 'addr':
            srch_dict['seg'] = seg
            sql = self.type_addr('1', srch_dict)

        values = self.getResultValues(sql)
        data = self.compute('fdb', values, srch_dict, vend)

        return data