How to Use Mac Vendor Lookup API with Python

Today, we are diving into a simple DIY tool – Mac Vendor Lookup API with Python. Imagine you've got a bunch of MAC addresses and you want to find out who made the hardware. Sure, if you've only got a few, you could just pop over to their website and check them manually. But what if you've got hundreds? That's where a bit of simple scripting can save the day.

We're all familiar with the website macvendors.com to find the vendor based on MAC addresses. But did you know they also offer an API for doing this programmatically? That's right! You can automate the whole process, and the best part? You don't need to set up any API key or go through complicated steps to use their free plan.

💡
Please note that their free plan lets us make 1000 requests a day.

Python Function

import requests

def mac_vendor(mac):
    try:
        vendor = requests.get(url=f'https://api.macvendors.com/{mac}')
        return vendor.text
    except:
        return 'N/A'
  1. Input - You give it a MAC address.
  2. Process - The function uses Python's requests library to reach out to the macvendors.com API with the MAC address you provided.
  3. Output - If everything goes smoothly, it returns the name of the manufacturer for that MAC address.
  4. Error Handling - If something goes wrong (like a network issue or an invalid MAC address), it doesn't crash. Instead, it simply returns 'N/A'.

Example

Here is a simple example to find out the vendor for a specific Mac address.

import requests

def mac_vendor(mac):
    try:
        vendor = requests.get(url=f'https://api.macvendors.com/{mac}')
        return vendor.text
    except:
        return 'N/A'

vendor = mac_vendor('c465.169a.3ba2')
print(vendor)
#output
Hewlett Packard