Beispiel #1
0
def _snmp_modular(ip, community, parent):
    oid = (1, 3, 6, 1, 4, 1, 343, 2, 19, 1, 2, 10, 12, 0) # Max blades
    message = snmp_command(ip, community, oid, attempts=1, timeout=0.5)
    max_blades = int(message[0][1])
    blades_macs = {}
    for blade_no in range(1, max_blades + 1):
        oid = (1, 3, 6, 1, 4, 1, 343, 2, 19, 1, 2, 10, 202, 3, 1, 1, blade_no)
        blades_macs[blade_no] =  set(snmp_macs(ip, community, oid,
                                                       attempts=1, timeout=0.5))
    for i, macs in blades_macs.iteritems():
        unique_macs = macs
        for j, other_macs in blades_macs.iteritems():
            if i == j:
                continue
            unique_macs -= other_macs
        ethernets = [Eth('Intel Modular MAC', mac, speed=None) for mac in
            unique_macs]
        if ethernets:
            dev = Device.create(
                    name='Intel Modular Blade',
                    model_name='Intel Modular Blade',
                    model_type=DeviceType.blade_server,
                    ethernets=ethernets,
                    management=parent.management,
                    chassis_position=i,
                    position = str(i),
                    parent=parent,
                )
            dev.save(update_last_seen=True)
Beispiel #2
0
def _snmp_modular_macs(ip_address, ip_address_is_management, snmp_community):
    oid = (1, 3, 6, 1, 4, 1, 343, 2, 19, 1, 2, 10, 12, 0)  # Max blades
    message = snmp_command(
        ip_address, snmp_community, oid, attempts=1, timeout=0.5,
    )
    max_blades = int(message[0][1])
    blades_macs = {}
    for blade_no in range(1, max_blades + 1):
        oid = (1, 3, 6, 1, 4, 1, 343, 2, 19, 1, 2, 10, 202, 3, 1, 1, blade_no)
        blades_macs[blade_no] = set(
            snmp_macs(
                ip_address, snmp_community, oid, attempts=1, timeout=0.5,
            ),
        )
    results = []
    management_ip_addresses = []
    if ip_address_is_management:
        management_ip_addresses.append(ip_address)
    for i, macs in blades_macs.iteritems():
        unique_macs = macs
        for j, other_macs in blades_macs.iteritems():
            if i == j:
                continue
            unique_macs -= other_macs
        if unique_macs:
            results.append({
                'type': unicode(DeviceType.blade_server),
                'model_name': 'Intel Modular Blade',
                'mac_addresses': list(unique_macs),
                'management_ip_addresses': management_ip_addresses,
                'chassis_position': i,
            })
    return results
Beispiel #3
0
def _snmp_modular(ip, community, parent):
    oid = (1, 3, 6, 1, 4, 1, 343, 2, 19, 1, 2, 10, 12, 0) # Max blades
    message = snmp_command(ip, community, oid, attempts=1, timeout=0.5)
    max_blades = int(message[0][1])
    blades_macs = {}
    for blade_no in range(1, max_blades + 1):
        oid = (1, 3, 6, 1, 4, 1, 343, 2, 19, 1, 2, 10, 202, 3, 1, 1, blade_no)
        blades_macs[blade_no] =  set(snmp_macs(ip, community, oid,
                                                       attempts=1, timeout=0.5))
    for i, macs in blades_macs.iteritems():
        unique_macs = macs
        for j, other_macs in blades_macs.iteritems():
            if i == j:
                continue
            unique_macs -= other_macs
        ethernets = [Eth('Intel Modular MAC', mac, speed=None) for mac in
            unique_macs]
        if ethernets:
            dev = Device.create(
                    name='Intel Modular Blade',
                    model_name='Intel Modular Blade',
                    model_type=DeviceType.blade_server,
                    ethernets=ethernets,
                    management=parent.management,
                    chassis_position=i,
                    position = str(i),
                    parent=parent,
                )
            dev.save(update_last_seen=True, priority=SAVE_PRIORITY)
Beispiel #4
0
def _snmp_modular_macs(ip_address, ip_address_is_management, snmp_community):
    oid = (1, 3, 6, 1, 4, 1, 343, 2, 19, 1, 2, 10, 12, 0)  # Max blades
    message = snmp_command(
        ip_address, snmp_community, oid, attempts=1, timeout=0.5,
    )
    max_blades = int(message[0][1])
    blades_macs = {}
    for blade_no in range(1, max_blades + 1):
        oid = (1, 3, 6, 1, 4, 1, 343, 2, 19, 1, 2, 10, 202, 3, 1, 1, blade_no)
        blades_macs[blade_no] = set(
            snmp_macs(
                ip_address, snmp_community, oid, attempts=1, timeout=0.5,
            ),
        )
    results = []
    management_ip_addresses = []
    if ip_address_is_management:
        management_ip_addresses.append(ip_address)
    for i, macs in blades_macs.iteritems():
        unique_macs = macs
        for j, other_macs in blades_macs.iteritems():
            if i == j:
                continue
            unique_macs -= other_macs
        if unique_macs:
            results.append({
                'type': unicode(DeviceType.blade_server),
                'model_name': 'Intel Modular Blade',
                'mac_addresses': [
                    MACAddressField.normalize(mac) for mac in unique_macs
                ],
                'management_ip_addresses': management_ip_addresses,
                'chassis_position': i,
            })
    return results
Beispiel #5
0
def snmp_vmware(parent, ipaddr, **kwargs):
    ip = str(kwargs['ip'])
    community = str(kwargs['community'])
    oid = (1,3,6,1,4,1,6876,2,4,1,7)
    snmp_version = 1
    for mac in snmp_macs(ip, community, oid, attempts=2,
                                 timeout=3, snmp_version=snmp_version):
        dev = Device.create(parent=parent, management=ipaddr,
                ethernets=[Eth(mac=mac, label='Virtual MAC', speed=0)],
                model_name='VMware ESX virtual server',
                model_type=DeviceType.virtual_server)
Beispiel #6
0
def snmp_vmware(parent, ipaddr, **kwargs):
    ip = str(kwargs['ip'])
    community = str(kwargs['community'])
    oid = (1,3,6,1,4,1,6876,2,4,1,7)
    snmp_version = 1
    for mac in snmp_macs(ip, community, oid, attempts=2,
                                 timeout=3, snmp_version=snmp_version):
        dev = Device.create(parent=parent, management=ipaddr,
                ethernets=[Eth(mac=mac, label='Virtual MAC', speed=0)],
                model_name='VMware ESX virtual server',
                model_type=DeviceType.virtual_server)
Beispiel #7
0
def _snmp_vmware_macs(ip_address, snmp_community):
    oid = (1, 3, 6, 1, 4, 1, 6876, 2, 4, 1, 7)
    snmp_version = 1
    results = []
    for mac in snmp_macs(ip_address, snmp_community, oid, attempts=2,
                         timeout=3, snmp_version=snmp_version):
        results.append({
            'type': unicode(DeviceType.virtual_server),
            'model_name': 'VMware ESX virtual server',
            'mac_addresses': [mac],
            'management_ip_addresses': [ip_address],
        })
    return results
Beispiel #8
0
def _snmp_vmware_macs(ip_address, snmp_community):
    oid = (1, 3, 6, 1, 4, 1, 6876, 2, 4, 1, 7)
    snmp_version = 1
    results = []
    for mac in snmp_macs(ip_address, snmp_community, oid, attempts=2,
                         timeout=3, snmp_version=snmp_version):
        results.append({
            'type': unicode(DeviceType.virtual_server),
            'model_name': 'VMware ESX virtual server',
            'mac_addresses': [MACAddressField.normalize(mac)],
            'management_ip_addresses': [ip_address],
        })
    return results
Beispiel #9
0
def _snmp_mac_from_ipv6IfPhysicalAddress(ip_address, snmp_name, snmp_community,
                                         snmp_version):
    mac_addresses = set()
    for mac in snmp_macs(
            ip_address,
            snmp_community,
        (1, 3, 6, 1, 2, 1, 55, 1, 5, 1, 8),  # ipv6IfPhysicalAddress
            attempts=2,
            timeout=3,
            snmp_version=snmp_version,
    ):
        mac_addresses.add(MACAddressField.normalize(mac))
    return mac_addresses
Beispiel #10
0
def _snmp_mac_from_ipv6IfPhysicalAddress(
    ip_address, snmp_name, snmp_community, snmp_version
):
    mac_addresses = set()
    for mac in snmp_macs(
        ip_address,
        snmp_community,
        (1, 3, 6, 1, 2, 1, 55, 1, 5, 1, 8),  # ipv6IfPhysicalAddress
        attempts=2,
        timeout=3,
        snmp_version=snmp_version,
    ):
        mac_addresses.add(MACAddressField.normalize(mac))
    return mac_addresses
Beispiel #11
0
def do_snmp_mac(snmp_name, community, snmp_version, ip, kwargs):
    oid = (1, 3, 6, 1, 2, 1, 2, 2, 1, 6)
    sn = None
    is_management = False
    if snmp_name.lower().startswith('sunos'):
        model_name = 'SunOs'
        model_type = DeviceType.unknown
    elif snmp_name.lower().startswith('hardware:') and 'Windows' in snmp_name:
        model_name = 'Windows'
        model_type = DeviceType.unknown
    elif snmp_name.lower().startswith('vmware esx'):
        model_name = 'VMware ESX'
        model_type = DeviceType.unknown
        oid = (1, 3, 6, 1, 2, 1, 2, 2, 1, 6)
        snmp_version = 1
    elif snmp_name.startswith('IronPort'):
        parts = snmp_name.split(',')
        model_name = parts[0].strip()
        model_type = DeviceType.smtp_gateway
        is_management = True
    elif snmp_name.startswith('Intel Modular'):
        model_type = DeviceType.blade_system
        model_name = 'Intel Modular Blade System'
        is_management = True
    elif snmp_name.startswith('IBM PowerPC CHRP Computer'):
        model_type = DeviceType.unknown
        model_name = 'IBM pSeries'
    elif 'Software:UCOS' in snmp_name:
        model_name = 'Cisco UCOS'
        model_type = DeviceType.appliance
        is_management = True
    elif snmp_name.startswith('Codian'):
        model_name = snmp_name
        model_type = DeviceType.appliance
        is_management = True
    elif snmp_name.startswith('APC'):
        m = re.search(r'\sMN:\s*(\S+)', snmp_name)
        model_name = m.group(1) if m else 'APC'
        m = re.search(r'\sSN:\s*(\S+)', snmp_name)
        sn = m.group(1) if m else None
        is_management = True
        model_type = DeviceType.power_distribution_unit
    elif 'fibre channel switch' in snmp_name.lower() or 'san switch module' in snmp_name.lower():
        model_name = snmp_name
        model_type = DeviceType.fibre_channel_switch
        is_management = True
    elif 'ethernet switch module' in snmp_name.lower() or snmp_name.startswith('ProCurve'):
        model_name= snmp_name
        if ',' in model_name:
            model_name, trash = model_name.split(',', 1)
        model_type = DeviceType.switch
        is_management = True
    elif '.f5app' in snmp_name:
        model_name, sn = snmp_f5(**kwargs)
        model_type = DeviceType.load_balancer
    elif 'StorageWorks' in snmp_name:
        model_name = snmp_name
        model_type = DeviceType.storage
    elif 'linux' in snmp_name.lower():
        model_name = 'Linux'
        model_type = DeviceType.unknown
    else:
        model_name = 'Unknown'
        model_type = DeviceType.unknown
        raise Error('no match.')
    ethernets = []
    for mac in snmp_macs(ip, community, oid, attempts=2,
                                 timeout=3, snmp_version=snmp_version):
        # Skip virtual devices
        if mac[0:6] in MAC_PREFIX_BLACKLIST:
            continue
        if snmp_name.startswith('Brocade') and not mac.startswith('00051E'):
            # Only use the first right mac of the Brocade switches, the rest is trash.
            continue
        if model_name == 'Windows' and mac.startswith('000C29'):
            # Skip VMWare interfaces on Windows
            continue
        if mac.startswith('0001D7') and model_type != DeviceType.load_balancer:
            # This is an F5
            model_name, sn = snmp_f5(**kwargs)
            model_type = DeviceType.load_balancer
        ethernets.append(Eth('SNMP MAC', mac, speed=None))
    if model_type == DeviceType.load_balancer:
        # For F5, macs that start with 02 are the maqueraded macs
        ethernets = [e for e in ethernets if not e.mac.startswith('02')]
    if not ethernets and not sn:
        raise Error('no MAC.')
    name = snmp_name
    dev = Device.create(ethernets=ethernets, model_name=model_name,
            model_type=model_type, name=name, sn=sn)
    ip_address = IPAddress.objects.get(address=str(ip))
    ip_address.device = dev
    ip_address.is_management = is_management
    if is_management:
        dev.management = ip_address
    if model_name == 'VMware ESX':
        snmp_vmware(dev, ip_address, **kwargs)
    ip_address.save()
    if model_name.startswith('IronPort'):
        pairs = dict((k.strip(), v.strip()) for (k, v) in
                        (part.split(':') for part in parts if ':' in part))
        dev.boot_firmware = 'AsyncOS %s %s' % (
                pairs.get('AsyncOS Version'), pairs.get('Build Date'))
        dev.sn = pairs.get('Serial #')
        dev.save(update_last_seen=True)
    elif model_name == 'Intel Modular Blade System':
        _snmp_modular(ip, community, dev)
    if not dev.operatingsystem_set.exists():
        if model_name == 'Linux':
            OperatingSystem.create(dev, os_name=snmp_name,
                                   family='Linux').save()
        elif model_name == 'SunOs':
            OperatingSystem.create(dev, os_name=snmp_name,
                                   family='Sun').save()
    return ethernets
Beispiel #12
0
def do_snmp_mac(snmp_name, community, snmp_version, ip, kwargs):
    oid = (1, 3, 6, 1, 2, 1, 2, 2, 1, 6)
    sn = None
    is_management = False
    if snmp_name.lower().startswith('sunos'):
        model_name = 'SunOs'
        model_type = DeviceType.unknown
    elif snmp_name.lower().startswith('hardware:') and 'Windows' in snmp_name:
        model_name = 'Windows'
        model_type = DeviceType.unknown
    elif snmp_name.lower().startswith('vmware esx'):
        model_name = 'VMware ESX'
        model_type = DeviceType.unknown
        oid = (1, 3, 6, 1, 2, 1, 2, 2, 1, 6)
        snmp_version = 1
    elif snmp_name.startswith('IronPort'):
        parts = snmp_name.split(',')
        model_name = parts[0].strip()
        model_type = DeviceType.smtp_gateway
        is_management = True
    elif snmp_name.startswith('Intel Modular'):
        model_type = DeviceType.blade_system
        model_name = 'Intel Modular Blade System'
        is_management = True
    elif snmp_name.startswith('IBM PowerPC CHRP Computer'):
        model_type = DeviceType.unknown
        model_name = 'IBM pSeries'
    elif 'Software:UCOS' in snmp_name:
        model_name = 'Cisco UCOS'
        model_type = DeviceType.appliance
        is_management = True
    elif snmp_name.startswith('Codian'):
        model_name = snmp_name
        model_type = DeviceType.appliance
        is_management = True
    elif snmp_name.startswith('APC'):
        m = re.search(r'\sMN:\s*(\S+)', snmp_name)
        model_name = m.group(1) if m else 'APC'
        m = re.search(r'\sSN:\s*(\S+)', snmp_name)
        sn = m.group(1) if m else None
        is_management = True
        model_type = DeviceType.power_distribution_unit
    elif 'fibre channel switch' in snmp_name.lower() or 'san switch module' in snmp_name.lower():
        model_name = snmp_name
        model_type = DeviceType.fibre_channel_switch
        is_management = True
    elif 'ethernet switch module' in snmp_name.lower() or snmp_name.startswith('ProCurve'):
        model_name= snmp_name
        if ',' in model_name:
            model_name, trash = model_name.split(',', 1)
        model_type = DeviceType.switch
        is_management = True
    elif '.f5app' in snmp_name:
        model_name, sn = snmp_f5(**kwargs)
        model_type = DeviceType.load_balancer
    elif 'StorageWorks' in snmp_name:
        model_name = snmp_name
        model_type = DeviceType.storage
    elif 'linux' in snmp_name.lower():
        model_name = 'Linux'
        model_type = DeviceType.unknown
    else:
        model_name = 'Unknown'
        model_type = DeviceType.unknown
        raise Error('no match.')
    ethernets = []
    for mac in snmp_macs(ip, community, oid, attempts=2,
                                 timeout=3, snmp_version=snmp_version):
        # Skip virtual devices
        if mac[0:6] in MAC_PREFIX_BLACKLIST:
            continue
        if snmp_name.startswith('Brocade') and not mac.startswith('00051E'):
            # Only use the first right mac of the Brocade switches, the rest is trash.
            continue
        if model_name == 'Windows' and mac.startswith('000C29'):
            # Skip VMWare interfaces on Windows
            continue
        if mac.startswith('0001D7') and model_type != DeviceType.load_balancer:
            # This is an F5
            model_name, sn = snmp_f5(**kwargs)
            model_type = DeviceType.load_balancer
        ethernets.append(Eth('SNMP MAC', mac, speed=None))
    if model_type == DeviceType.load_balancer:
        # For F5, macs that start with 02 are the maqueraded macs
        ethernets = [e for e in ethernets if not e.mac.startswith('02')]
    if not ethernets and not sn:
        raise Error('no MAC.')
    name = snmp_name
    dev = Device.create(ethernets=ethernets, model_name=model_name,
            model_type=model_type, name=name, sn=sn)
    ip_address = IPAddress.objects.get(address=str(ip))
    ip_address.device = dev
    ip_address.is_management = is_management
    if is_management:
        dev.management = ip_address
    if model_name == 'VMware ESX':
        snmp_vmware(dev, ip_address, **kwargs)
    ip_address.save()
    if model_name.startswith('IronPort'):
        pairs = dict((k.strip(), v.strip()) for (k, v) in
                        (part.split(':') for part in parts if ':' in part))
        dev.boot_firmware = 'AsyncOS %s %s' % (
                pairs.get('AsyncOS Version'), pairs.get('Build Date'))
        dev.sn = pairs.get('Serial #')
        dev.save(update_last_seen=True, priority=SAVE_PRIORITY)
    elif model_name == 'Intel Modular Blade System':
        _snmp_modular(ip, community, dev)
    if not dev.operatingsystem_set.exists():
        if model_name in ('Linux', 'SunOs'):
            family = 'Linux' if model_name == 'Linux' else 'Sun'
            OperatingSystem.create(dev, os_name=snmp_name, family=family).save()
    return ethernets
Beispiel #13
0
def _snmp_mac(ip_address, snmp_name, snmp_community, snmp_version,
              messages=[]):
    oid = (1, 3, 6, 1, 2, 1, 2, 2, 1, 6)
    sn = None
    if not snmp_name or not snmp_community:
        raise Error(
            "Empty SNMP name or community. "
            "Please perform an autoscan of this address first."
        )
        return
    model_name, model_type, is_management = _get_model_info(snmp_name)
    if snmp_name.startswith('IronPort'):
        parts = snmp_name.split(',')
        pairs = dict(
            (k.strip(), v.strip()) for (k, v) in (
                part.split(':') for part in parts if ':' in part
            )
        )
        sn = pairs.get('Serial #')
    if snmp_name.startswith('APC'):
        m = re.search(r'\sSN:\s*(\S+)', snmp_name)
        if m:
            sn = m.group(1)
    mac_addresses = set()
    for mac in snmp_macs(
        ip_address,
        snmp_community,
        oid,
        attempts=2,
        timeout=3,
        snmp_version=snmp_version,
    ):
        # Skip virtual devices
        if mac[0:6] in MAC_PREFIX_BLACKLIST:
            continue
        if snmp_name.startswith('Brocade') and not mac.startswith('00051E'):
            # Only use the first right mac of the Brocade switches,
            # the rest is trash.
            continue
        if model_name == 'Windows' and mac.startswith('000C29'):
            # Skip VMWare interfaces on Windows
            continue
        if mac.startswith('0001D7') and model_type != DeviceType.load_balancer:
            raise Error("This is an F5.")
        mac_addresses.add(mac)
    if not mac_addresses:
        raise Error("No valid MAC addresses in the SNMP response.")

    result = {
        'type': str(model_type),
        'model_name': model_name,
        'mac_addresses': list(mac_addresses),
    }
    if sn:
        result.update(sn=sn)
    if is_management:
        result['management_ip_addresses'] = [ip_address]
        if model_name.lower() == 'linux':
            result['system_family'] = "Linux"
        elif model_name.lower() == 'sunos':
            result['system_family'] = "Sun"
    else:
        result['system_ip_addresses'] = [ip_address]

    return result
Beispiel #14
0
def do_snmp_mac(snmp_name, community, snmp_version, ip, kwargs):
    oid = (1, 3, 6, 1, 2, 1, 2, 2, 1, 6)
    sn = None
    is_management = False
    if snmp_name.lower().startswith("sunos"):
        model_name = "SunOs"
        model_type = DeviceType.unknown
    elif snmp_name.lower().startswith("hardware:") and "Windows" in snmp_name:
        model_name = "Windows"
        model_type = DeviceType.unknown
    elif snmp_name.lower().startswith("vmware esx"):
        model_name = "VMware ESX"
        model_type = DeviceType.unknown
        oid = (1, 3, 6, 1, 2, 1, 2, 2, 1, 6)
        snmp_version = 1
    elif snmp_name.startswith("IronPort"):
        parts = snmp_name.split(",")
        model_name = parts[0].strip()
        model_type = DeviceType.smtp_gateway
        is_management = True
    elif snmp_name.startswith("Intel Modular"):
        model_type = DeviceType.blade_system
        model_name = "Intel Modular Blade System"
        is_management = True
    elif snmp_name.startswith("IBM PowerPC CHRP Computer"):
        model_type = DeviceType.unknown
        model_name = "IBM pSeries"
    elif "Software:UCOS" in snmp_name:
        model_name = "Cisco UCOS"
        model_type = DeviceType.appliance
        is_management = True
    elif snmp_name.startswith("Codian"):
        model_name = snmp_name
        model_type = DeviceType.appliance
        is_management = True
    elif snmp_name.startswith("APC"):
        m = re.search(r"\sMN:\s*(\S+)", snmp_name)
        model_name = m.group(1) if m else "APC"
        m = re.search(r"\sSN:\s*(\S+)", snmp_name)
        sn = m.group(1) if m else None
        is_management = True
        model_type = DeviceType.power_distribution_unit
    elif "fibre channel switch" in snmp_name.lower() or "san switch module" in snmp_name.lower():
        model_name = snmp_name
        model_type = DeviceType.fibre_channel_switch
        is_management = True
    elif "ethernet switch module" in snmp_name.lower() or snmp_name.startswith("ProCurve"):
        model_name = snmp_name
        if "," in model_name:
            model_name, trash = model_name.split(",", 1)
        model_type = DeviceType.switch
        is_management = True
    elif ".f5app" in snmp_name:
        model_name, sn = snmp_f5(**kwargs)
        model_type = DeviceType.load_balancer
    elif "StorageWorks" in snmp_name:
        model_name = snmp_name
        model_type = DeviceType.storage
    elif "linux" in snmp_name.lower():
        model_name = "Linux"
        model_type = DeviceType.unknown
    else:
        model_name = "Unknown"
        model_type = DeviceType.unknown
        raise Error("no match.")
    ethernets = []
    for mac in snmp_macs(ip, community, oid, attempts=2, timeout=3, snmp_version=snmp_version):
        # Skip virtual devices
        if mac[0:6] in MAC_PREFIX_BLACKLIST:
            continue
        if snmp_name.startswith("Brocade") and not mac.startswith("00051E"):
            # Only use the first right mac of the Brocade switches, the rest is trash.
            continue
        if model_name == "Windows" and mac.startswith("000C29"):
            # Skip VMWare interfaces on Windows
            continue
        if mac.startswith("0001D7") and model_type != DeviceType.load_balancer:
            # This is an F5
            model_name, sn = snmp_f5(**kwargs)
            model_type = DeviceType.load_balancer
        ethernets.append(Eth("SNMP MAC", mac, speed=None))
    if model_type == DeviceType.load_balancer:
        # For F5, macs that start with 02 are the maqueraded macs
        ethernets = [e for e in ethernets if not e.mac.startswith("02")]
    if not ethernets and not sn:
        raise Error("no MAC.")
    name = snmp_name
    dev = Device.create(ethernets=ethernets, model_name=model_name, model_type=model_type, name=name, sn=sn)
    ip_address = IPAddress.objects.get(address=str(ip))
    ip_address.device = dev
    ip_address.is_management = is_management
    if is_management:
        dev.management = ip_address
    if model_name == "VMware ESX":
        snmp_vmware(dev, ip_address, **kwargs)
    ip_address.save()
    if model_name.startswith("IronPort"):
        pairs = dict((k.strip(), v.strip()) for (k, v) in (part.split(":") for part in parts if ":" in part))
        dev.boot_firmware = "AsyncOS %s %s" % (pairs.get("AsyncOS Version"), pairs.get("Build Date"))
        dev.sn = pairs.get("Serial #")
        dev.save(update_last_seen=True)
    elif model_name == "Intel Modular Blade System":
        _snmp_modular(ip, community, dev)
    if not dev.operatingsystem_set.exists():
        if model_name == "Linux":
            OperatingSystem.create(dev, os_name=snmp_name, family="Linux").save()
        elif model_name == "SunOs":
            OperatingSystem.create(dev, os_name=snmp_name, family="Sun").save()
    return ethernets
Beispiel #15
0
def _snmp_mac(ip_address,
              snmp_name,
              snmp_community,
              snmp_version,
              messages=[]):
    oid = (1, 3, 6, 1, 2, 1, 2, 2, 1, 6)
    sn = None
    if not snmp_name or not snmp_community:
        raise Error("Empty SNMP name or community. "
                    "Please perform an autoscan of this address first.")
    model_name, model_type, is_management = _get_model_info(snmp_name)
    if snmp_name.lower().startswith('vmware esx'):
        oid = (1, 3, 6, 1, 2, 1, 2, 2, 1, 6)
        snmp_version = 1
    elif snmp_name.startswith('IronPort'):
        parts = snmp_name.split(',')
        pairs = dict((k.strip(), v.strip()) for (k, v) in (part.split(':')
                                                           for part in parts
                                                           if ':' in part))
        sn = pairs.get('Serial #')
    elif snmp_name.startswith('APC'):
        m = re.search(r'\sSN:\s*(\S+)', snmp_name)
        if m:
            sn = m.group(1)
    mac_addresses = set()
    for mac in snmp_macs(
            ip_address,
            snmp_community,
            oid,
            attempts=2,
            timeout=3,
            snmp_version=snmp_version,
    ):
        # Skip virtual devices
        if mac[0:6] in MAC_PREFIX_BLACKLIST:
            continue
        if snmp_name.startswith('Brocade') and not mac.startswith('00051E'):
            # Only use the first right mac of the Brocade switches,
            # the rest is trash.
            continue
        if model_name == 'Windows' and mac.startswith('000C29'):
            # Skip VMWare interfaces on Windows
            continue
        if mac.startswith('0001D7') and model_type != DeviceType.load_balancer:
            # This is an F5
            model_name = 'F5'
            model_type = DeviceType.load_balancer
        mac_addresses.add(mac)
    if model_type == DeviceType.load_balancer:
        # For F5, macs that start with 02 are the masqueraded macs
        mac_addresses = set(
            [mac for mac in mac_addresses if not mac.startswith('02')])
    if not mac_addresses:
        raise Error("No valid MAC addresses in the SNMP response.")
    result = {
        'type': str(model_type),
        'model_name': model_name,
        'mac_addresses': list(mac_addresses),
    }
    if sn:
        result.update(sn=sn)
    if is_management:
        result['management_ip_addresses'] = [ip_address]
        if model_name.lower() == 'linux':
            result['system_family'] = "Linux"
        elif model_name.lower() == 'sunos':
            result['system_family'] = "Sun"
    else:
        result['system_ip_addresses'] = [ip_address]
    subdevices = []
    if model_name == 'VMware ESX':
        subdevices.extend(_snmp_vmware_macs(ip_address, snmp_community))
    if model_name == 'Intel Modular Blade System':
        subdevices.extend(
            _snmp_modular_macs(ip_address, is_management, snmp_community), )
    if subdevices:
        result['subdevices'] = subdevices
    return result
Beispiel #16
0
def _snmp_mac(ip_address, snmp_name, snmp_community, snmp_version,
              messages=[]):
    oid = (1, 3, 6, 1, 2, 1, 2, 2, 1, 6)
    sn = None
    if not snmp_name or not snmp_community:
        raise Error(
            "Empty SNMP name or community. "
            "Please perform an autoscan of this address first."
        )
    model_name, model_type, is_management = _get_model_info(snmp_name)
    if snmp_name.lower().startswith('vmware esx'):
        oid = (1, 3, 6, 1, 2, 1, 2, 2, 1, 6)
        snmp_version = 1
    elif snmp_name.startswith('IronPort'):
        parts = snmp_name.split(',')
        pairs = dict(
            (k.strip(), v.strip()) for (k, v) in (
                part.split(':') for part in parts if ':' in part
            )
        )
        sn = pairs.get('Serial #')
    elif snmp_name.startswith('APC'):
        m = re.search(r'\sSN:\s*(\S+)', snmp_name)
        if m:
            sn = m.group(1)
    mac_addresses = set()
    ipv6if_mac_addresses = _snmp_mac_from_ipv6IfPhysicalAddress(
        ip_address=ip_address,
        snmp_name=snmp_name,
        snmp_community=snmp_community,
        snmp_version=snmp_version,
    )
    for mac in snmp_macs(
        ip_address,
        snmp_community,
        oid,
        attempts=2,
        timeout=3,
        snmp_version=snmp_version,
    ):
        # cloud hypervisor can return all VMs mac addresses...
        if ipv6if_mac_addresses and mac not in ipv6if_mac_addresses:
            continue
        # Skip virtual devices
        if mac[0:6] in MAC_PREFIX_BLACKLIST:
            continue
        if snmp_name.startswith('Brocade') and not mac.startswith('00051E'):
            # Only use the first right mac of the Brocade switches,
            # the rest is trash.
            continue
        if model_name == 'Windows' and mac.startswith('000C29'):
            # Skip VMWare interfaces on Windows
            continue
        if mac.startswith('0001D7') and model_type != DeviceType.load_balancer:
            # This is an F5
            model_name = 'F5'
            model_type = DeviceType.load_balancer
        mac_addresses.add(mac)
    if model_type == DeviceType.load_balancer:
        # For F5, macs that start with 02 are the masqueraded macs
        mac_addresses = set([
            mac for mac in mac_addresses if not mac.startswith('02')
        ])
    if not mac_addresses:
        raise Error("No valid MAC addresses in the SNMP response.")
    result = {
        'type': str(model_type),
        'model_name': model_name,
        'mac_addresses': [
            MACAddressField.normalize(mac) for mac in mac_addresses
        ],
    }
    if sn:
        result.update(sn=sn)
    if is_management:
        result['management_ip_addresses'] = [ip_address]
        if model_name.lower() == 'linux':
            result['system_family'] = "Linux"
        elif model_name.lower() == 'sunos':
            result['system_family'] = "Sun"
    else:
        result['system_ip_addresses'] = [ip_address]
    subdevices = []
    if model_name == 'VMware ESX':
        subdevices.extend(_snmp_vmware_macs(ip_address, snmp_community))
    if model_name == 'Intel Modular Blade System':
        subdevices.extend(
            _snmp_modular_macs(ip_address, is_management, snmp_community),
        )
    if subdevices:
        result['subdevices'] = subdevices
    return result