DEV Community

Vimrichie
Vimrichie

Posted on

MAC Address lookup using Python ( and an API )

Overview

As a network engineer, I sometimes have to verify what type of device is connected on a port or what is being registered on a firewall via ARP. Having a MAC address handy can be useful for many reasons but in my case, having one is great for network troubleshooting.

I'm going to walk you through my Python script that sends a MAC address to an API and the response will be the vendor of that address. So let's create the script so we don't have to open a browser and search for it on the web.🧑‍💻

 

What's a MAC address?

MAC addresses are registered with the IEEE (Institute of Electrical and Electronics Engineers). The prefix of a MAC address is a number that uniquely identifies a vendor, manufacturer, or other organization.

Wikipedia:

A media access control address (MAC address) is a unique identifier assigned to a network interface controller (NIC) for use as a network address in communications within a network segment. MAC addresses are primarily assigned by device manufacturers, and are therefore often referred to as the burned-in address, or as an Ethernet hardware address, hardware address, or physical address.

So if you have a device that connects to the internet, the networking interface card is registered with IEEE.

Here is a list of the vendors.
http://standards-oui.ieee.org/oui/oui.txt

 

Requirements

  • Python 3
  • Requests - A Python library
  • An API - Macvendors.com - This API is free and requires no sign up.

 

Let’s code!

Let’s start by importing our libraries.



import sys

from requests import get


Enter fullscreen mode Exit fullscreen mode

Now let’s create a class because object oriented programming is cool. 😎



class Macpyoui:
    def __init__(self, api):
        self.api = api


site = "https://api.macvendors.com/"
data = Macpyoui(site)
macaddress = input("Please enter the MAC address: ")


Enter fullscreen mode Exit fullscreen mode

Let’s break down the class and see what we did:

  • Created a class called Macpyoui. This is going to allow us to call our information.
  • Created a method __init__ and added the arguments self and api.
  • Assigned self.api as api.

You will also see that I added some variables.

  • site - This is the website that we will be sending the MAC address.
  • data - Here we create a variable called data and assign the argument site to the class Macpyoui. This will allow me to call it back in a function.
  • macaddress - We create a variable here called macaddress and we ask the person running the script what the MAC address is with the input function and we add the string inside it asking about the MAC address.

Now let's define our function:



def searchmac():
    macsend = data.api + macaddress
    vendorsearch = get(macsend).text
    if "Not Found" in vendorsearch:
        print("MAC address not found.")
    elif len(sys.argv) == 1:
        print("No MAC address entered.")
    else:
        print(vendorsearch)


Enter fullscreen mode Exit fullscreen mode

Breaking down the function.



def searchmac():


Enter fullscreen mode Exit fullscreen mode
  • Here we create our function searchmac ```python

macsend = data.api + macaddress

- Then we create a variable called `macsend`. 
- We assign `data.api + macaddress`. 
- In the assignment we use `data.api` which is asking for the website. 
- The `data` in `data.api` is the class `Macpyoui` being called and the `.api` is asking for the website in the class. As for `macaddress`, this is what the user provides and we combine then with the `+`. So written out it would look like *`https://api.macvendors.com/`* + *`mac address`*

```python


vendorsearch = get(macsend).text


Enter fullscreen mode Exit fullscreen mode
  • Here we assign vendorsearch to use the get request and send the MAC address under the argument macsend. We make sure to convert it into simple text with .text at the end.


if "Not Found" in vendorsearch:
        print("MAC address not found.")
    elif len(sys.argv) == 1:
        print("No MAC address entered.")
    else:
        print(vendorsearch)


Enter fullscreen mode Exit fullscreen mode
  • For the last part of the function we do a few if statements to see what information is entered.


if "Not Found" in vendorsearch:
        print("MAC address not found.") 


Enter fullscreen mode Exit fullscreen mode
  • If the API responds with Not Found, we print out MAC address not found. ```python

elif len(sys.argv) == 1:
print("No MAC address entered.")

-If the user enters no MAC address, we print out *`No MAC address entered`*.

```python


else:
        print(vendorsearch)


Enter fullscreen mode Exit fullscreen mode
  • If there is a response from the website that is not one of the first two if statements, it prints the response.

Finally, let's add our main method at the bottom. This makes us a 10x coder.



if __name__ == "__main__":
    searchmac()


Enter fullscreen mode Exit fullscreen mode

That's it! We don't need much to run our simple check.

Image description

Let’s test it out!

Successful search



~ ❯ python3 macpy.py                                                                                                                                   
Please enter the MAC address: F4BD9E
Cisco Systems, Inc


Enter fullscreen mode Exit fullscreen mode

Unsuccessful Search



~ ❯ python3 macpy.py
Please enter the MAC address:
MAC address not found.


Enter fullscreen mode Exit fullscreen mode

Missing entry



~ ❯ python3 macpy.py                                                                                                                                   
Please enter the MAC address:
No MAC address entered


Enter fullscreen mode Exit fullscreen mode

Here is the full code:



import sys

from requests import get

__version__ = "1.0"


class Macpyoui:
    def __init__(self, api):
        self.api = api


site = "https://api.macvendors.com/"
data = Macpyoui(site)
macaddress = input("Please enter the MAC address: ")


def searchmac():
    macsend = data.api + macaddress
    vendorsearch = get(macsend).text
    if "Not Found" in vendorsearch:
        print("MAC address not found.")
    elif len(sys.argv) == 1:
        print("No MAC address entered.")
    else:
        print(vendorsearch)

if __name__ == "__main__":
    searchmac()


Enter fullscreen mode Exit fullscreen mode

Check out the code on GitHub.
https://github.com/applericky/macpy

Thanks for reading!
Feedback or questions? Let me know!

Top comments (1)

Collapse
 
harry_brook_657c38801ef6d profile image
Harry Brook

Performing a MAC address lookup using Python and an API can be a game-changer for network troubleshooting and device identification. By leveraging a service like Allmacworld , you can quickly obtain details about manufacturers and device types associated with specific MAC addresses. This can be especially helpful for network administrators who need to manage large networks or detect unauthorized devices. With Python’s easy-to-use libraries and a reliable API, MAC address lookups are straightforward, making network management more efficient and secure!

Some comments may only be visible to logged-in visitors. Sign in to view all comments.