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
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: ")
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 argumentsself
andapi
. - Assigned
self.api
asapi
.
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 argumentsite
to the classMacpyoui
. This will allow me to call it back in a function. -
macaddress
- We create a variable here calledmacaddress
and we ask the person running the script what the MAC address is with theinput
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)
Breaking down the function.
def searchmac():
- 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
- Here we assign
vendorsearch
to use theget
request and send the MAC address under the argumentmacsend
. 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)
- 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.")
- If the API responds with
Not Found
, we print outMAC 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)
- 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()
That's it! We don't need much to run our simple check.
Let’s test it out!
Successful search
~ ❯ python3 macpy.py
Please enter the MAC address: F4BD9E
Cisco Systems, Inc
Unsuccessful Search
~ ❯ python3 macpy.py
Please enter the MAC address:
MAC address not found.
Missing entry
~ ❯ python3 macpy.py
Please enter the MAC address:
No MAC address entered
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()
Check out the code on GitHub.
https://github.com/applericky/macpy
Thanks for reading!
Feedback or questions? Let me know!
Top comments (1)
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.