Python script to get device vendor name from MAC Address

In the world of networking, MAC addresses play a crucial role in identifying devices connected to a network. A MAC (Media Access Control) address is a unique identifier assigned to each network interface card (NIC). It consists of six groups of two hexadecimal digits, separated by colons or hyphens. This article explores how to create a Python script to retrieve the device vendor name from a given MAC address using two approaches: API-based lookup and local database lookup.

Understanding MAC Addresses

A MAC address consists of 48 bits (6 bytes) represented in hexadecimal notation. It is typically written as six groups of two hexadecimal digits separated by colons or hyphens. For example, 00:1A:2B:3C:4D:5E or 00-1A-2B-3C-4D-5E. The first three groups represent the Organizationally Unique Identifier (OUI) and identify the device vendor or manufacturer.

MAC addresses are essential for several networking tasks:

  • Address Resolution Protocol (ARP) ? MAC addresses map IP addresses to physical addresses on local networks.

  • Switching and Bridging ? Network switches use MAC addresses to determine packet destinations.

  • DHCP ? Dynamic Host Configuration Protocol servers use MAC addresses to assign IP addresses.

  • Network Security ? MAC addresses enable access control based on device identification.

Method 1: Using MAC Address API

The first approach uses an online API to query a MAC address vendor database. We'll use the requests library to make HTTP requests ?

import requests

def get_vendor_from_api(mac_address):
    # Using macvendors.co API (free, no key required)
    url = f"https://api.macvendors.com/{mac_address}"
    
    try:
        response = requests.get(url, timeout=5)
        if response.status_code == 200:
            return response.text.strip()
        else:
            return "Unknown"
    except requests.RequestException:
        return "API Error"

# Example usage
mac = "00:1A:2B:3C:4D:5E"
vendor = get_vendor_from_api(mac)
print(f"MAC: {mac}")
print(f"Vendor: {vendor}")
MAC: 00:1A:2B:3C:4D:5E
Vendor: Unknown

Method 2: Using Local OUI Database

The second approach uses a local database file containing MAC vendor information. This method works offline and doesn't require API calls ?

def create_sample_oui_database():
    # Sample OUI database with common vendors
    oui_data = {
        "00:1A:2B": "Apple Inc",
        "00:50:56": "VMware Inc",
        "08:00:27": "Oracle Corporation",
        "52:54:00": "QEMU Virtual NIC",
        "00:0C:29": "VMware Inc",
        "00:16:3E": "Xensource Inc"
    }
    return oui_data

def get_vendor_from_local_db(mac_address, oui_database):
    # Extract the OUI (first 3 octets)
    oui = mac_address[:8].upper().replace(":", "")
    oui_formatted = f"{oui[:2]}:{oui[2:4]}:{oui[4:6]}"
    
    return oui_database.get(oui_formatted, "Unknown")

# Create sample database and test
oui_db = create_sample_oui_database()
mac = "08:00:27:12:34:56"
vendor = get_vendor_from_local_db(mac, oui_db)

print(f"MAC: {mac}")
print(f"OUI: {mac[:8]}")
print(f"Vendor: {vendor}")
MAC: 08:00:27:12:34:56
OUI: 08:00:27
Vendor: Oracle Corporation

Complete Script with Both Methods

Here's a comprehensive script that combines both approaches with error handling ?

import requests
import re

class MACVendorLookup:
    def __init__(self):
        self.oui_database = {
            "00:1A:2B": "Apple Inc",
            "00:50:56": "VMware Inc", 
            "08:00:27": "Oracle Corporation",
            "52:54:00": "QEMU Virtual NIC",
            "00:0C:29": "VMware Inc",
            "00:16:3E": "Xensource Inc",
            "00:1B:63": "Apple Inc"
        }
    
    def validate_mac(self, mac_address):
        # Validate MAC address format
        pattern = r'^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$'
        return bool(re.match(pattern, mac_address))
    
    def get_vendor_api(self, mac_address):
        if not self.validate_mac(mac_address):
            return "Invalid MAC format"
        
        url = f"https://api.macvendors.com/{mac_address}"
        try:
            response = requests.get(url, timeout=5)
            if response.status_code == 200:
                return response.text.strip()
            else:
                return "Unknown (API)"
        except requests.RequestException:
            return "API Error"
    
    def get_vendor_local(self, mac_address):
        if not self.validate_mac(mac_address):
            return "Invalid MAC format"
        
        # Extract OUI (first 3 octets)
        oui = mac_address[:8].upper().replace("-", ":")
        return self.oui_database.get(oui, "Unknown (Local)")
    
    def lookup_vendor(self, mac_address, use_api=False):
        print(f"Looking up vendor for MAC: {mac_address}")
        
        if use_api:
            vendor = self.get_vendor_api(mac_address)
            print(f"API Result: {vendor}")
        
        local_vendor = self.get_vendor_local(mac_address)
        print(f"Local DB Result: {local_vendor}")
        
        return local_vendor

# Example usage
lookup = MACVendorLookup()

test_macs = [
    "08:00:27:12:34:56",
    "00:1A:2B:AA:BB:CC", 
    "00:50:56:11:22:33"
]

for mac in test_macs:
    print(f"\n{'-' * 40}")
    vendor = lookup.lookup_vendor(mac)
    print(f"Final Result: {vendor}")
----------------------------------------
Looking up vendor for MAC: 08:00:27:12:34:56
Local DB Result: Oracle Corporation
Final Result: Oracle Corporation

----------------------------------------
Looking up vendor for MAC: 00:1A:2B:AA:BB:CC
Local DB Result: Apple Inc
Final Result: Apple Inc

----------------------------------------
Looking up vendor for MAC: 00:50:56:11:22:33
Local DB Result: VMware Inc
Final Result: VMware Inc

Key Features

  • MAC Address Validation ? Uses regular expressions to ensure proper MAC format

  • Dual Lookup Methods ? Supports both API and local database approaches

  • Error Handling ? Gracefully handles network errors and invalid inputs

  • Flexible Format ? Accepts MAC addresses with colons or hyphens

Comparison

Method Requires Internet Database Updates Response Time
API Lookup Yes Automatic Slower
Local Database No Manual Faster

Conclusion

This tutorial demonstrated two methods for retrieving device vendor names from MAC addresses using Python. The API approach provides up-to-date information but requires internet connectivity, while the local database method offers faster offline lookups. Choose the method that best fits your application's requirements for network dependency and performance needs.

Updated on: 2026-03-27T12:08:38+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements