示例#1
0
    def findmac_on_switches(self, pattern, mac, vlan):
        """find <mac> on switches like <pattern> in <vlan>"""
        from switches import Zabbix
        from findmac import Switch
        result = dict()
        try:
            zabbix = Zabbix(conf = 'db.conf')
            switches = zabbix.switchlist(pattern)
        except Exception as e:
            result['status'] = False
            result['data'] = (('error:', e.message), )
            return result

        data = list()
        for ip, comm in switches:
            sw = Switch(host = ip, getmodel = True)
            sw.proto = 'snmp'
            sw.vlan = vlan
            #sw.model = 'A3100'
            sw.community = comm

            port = sw.find_mac(mac)

            data.append((sw.host, port))

        result['status'] = True
        result['result'] = True if len(data) > 0 else False
        result['mac'] = mac
        result['vlan'] = sw.vlan
        result['data'] = data
        
        return result
示例#2
0
def main():
    # Обработка аргументов коммандной строки
    import argparse
    from findmac import Switch
    parser = argparse.ArgumentParser(description="""Список свичей""")
    parser.add_argument('-c',
                        '--conf',
                        metavar='FILE',
                        help='Фаил конфигурации')
    parser.add_argument('-p',
                        '--pattern',
                        metavar='PATTERN',
                        help='Mysql-шаблон ip-адреса')
    parser.add_argument('-m',
                        '--mac',
                        metavar='MAC',
                        help='Mac-адрес для поиска')
    parser.add_argument('-v',
                        '--vlan',
                        metavar='VLAN',
                        help='Vlan для поиска mac-адреса')
    params = parser.parse_args()

    # Инициализация
    zabbix = Zabbix(conf=params.conf)

    switches = zabbix.switchlist(params.pattern)

    if not params.mac:
        for ip, comm in switches:
            print "switch ip: %s, community %s" % (ip, comm)
    else:
        for ip, comm in switches:
            sw = Switch(host=ip)
            sw.proto = 'snmp'
            sw.vlan = params.vlan
            sw.model = 'A3100'
            sw.community = comm

            port = sw.find_mac(params.mac)

            if port:
                print "MAC '%s' found on %s port %s" % (params.mac, ip, port)
            else:
                print "MAC '%s' not found on %s vlan %s" % (params.mac, ip,
                                                            sw.vlan)
示例#3
0
def main():
    # Обработка аргументов коммандной строки
    import argparse
    from findmac import Switch
    parser = argparse.ArgumentParser(
        description="""Список свичей""")
    parser.add_argument('-c', '--conf', 
        metavar = 'FILE',
        help = 'Фаил конфигурации')
    parser.add_argument('-p', '--pattern', 
        metavar = 'PATTERN',
        help = 'Mysql-шаблон ip-адреса')
    parser.add_argument('-m', '--mac',
        metavar = 'MAC',
        help = 'Mac-адрес для поиска')
    parser.add_argument('-v', '--vlan',
        metavar = 'VLAN',
        help = 'Vlan для поиска mac-адреса')
    params = parser.parse_args()

    # Инициализация
    zabbix = Zabbix(conf = params.conf)
    
    switches = zabbix.switchlist(params.pattern)

    if not params.mac:
        for ip, comm in switches:
            print "switch ip: %s, community %s" % (ip, comm)
    else:
        for ip, comm in switches:
            sw = Switch(host = ip)
            sw.proto = 'snmp'
            sw.vlan = params.vlan
            sw.model = 'A3100'
            sw.community = comm

            port = sw.find_mac(params.mac)

            if port:
                print "MAC '%s' found on %s port %s" % (params.mac, ip, port)
            else:
                print "MAC '%s' not found on %s vlan %s" % (params.mac, ip, sw.vlan)