Skip to content
Related Articles

Related Articles

Improve Article
Python script to get device vendor name from MAC Address
  • Last Updated : 29 Dec, 2020

Prerequisite: Python Requests

A MAC address is a unique identity of a network interface that is used to address the device in a Computer Network.

What does MAC Address consist of?

A MAC address is made up of 12 hexadecimal digits, 6 octets separated by ‘:’. There are 6 octets in a MAC address. In the first half, the manufacturer info is stored.

MAC-Address

How do we get the Manufacturer details?

For this article, we will be using an API that will fetch the MAC Address for us. We will use a Python script to automate the fetching process so that we can use it later in the Softwares and websites that may require this functionality.



We will use requests module to work with this API.

Below is the implementation.

Python3




import requests
import argparse
  
print("[*] Welcome")
  
# Function to get the interface name
def get_arguments():
    
    # This will give user a neat CLI
    parser = argparse.ArgumentParser()
      
    # We need the MAC address
    parser.add_argument("-m", "--macaddress",
                        dest="mac_address",
                        help="MAC Address of the device. "
                        )
    options = parser.parse_args()
      
    # Check if address was given
    if options.mac_address:
        return options.mac_address
    else:
        parser.error("[!] Invalid Syntax. "
                     "Use --help for more details.")
  
  
def get_mac_details(mac_address):
      
    # We will use an API to get the vendor details
      
    # Use get method to fetch details
    response = requests.get(url+mac_address)
    if response.status_code != 200:
        raise Exception("[!] Invalid MAC Address!")
    return response.content.decode()
  
# Driver Code
if __name__ == "__main__":
    mac_address = get_arguments()
    print("[+] Checking Details...")
      
    try:
        vendor_name = get_mac_details(mac_address)
        print("[+] Device vendor is "+vendor_name)
    except:
        
        # Incase something goes wrong
        print("[!] An error occured. Check "
              "your Internet connection.")

Save this code as macdetails.py. We can use ‘-h’ or ‘–help’ to see the help on how to run this script from the terminal.

Output : 

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course




My Personal Notes arrow_drop_up
Recommended Articles
Page :