Python Get MAC Address to allow Application only for specific Users

The main purpose of this post is how to allow only a specific user to use your CLI tool, and in this post, we’ll use a computer MAC address that provides a unique id of your computer hardware. Actually, it’s easy to achieve this because you only need three lines of code below to read your MAC address with Python:

The Code #1:

import uuid

your_mac_address = hex(uuid.getnode())

print('Your MAC Address: {}'.format(your_mac_address))

The Output:

Your MAC Address: 0xfc327824c8e9

In this case, my MAC address is 0xfc327824c8e9, you can check your computer MAC address with the above code because later on, we’ll use this MAC address to insert into the list of the Code #2.

Let’s implement the above code in a small and simple project which we call “name”, this CLI tool will request your input for your first, middle, and last name, and then printed it on the command prompt. The example below is the result of this tool:

Example #1, If the computer MAC addresses are not found in your list (in the CLI tool) then the output should be:

mac_address_not_allowed

Example #2: if your MAC address is already listed in the CLI tool then the output should be:

cli_tool_name_henry_ford

Example #3: If you want to see the help of your CLI tool, you can type this command “name -h”, and the result should be:

cli_tool_help

Enough for the examples, let’s start the step on how to implement the above project:

  • Write the code
  • Convert *.ipynb file to *.py (This post uses Jupiter Notebook to implement the code)
  • Convert *.py to *.exe file
  • Testing

You can check my previous post on how to convert the *.ipynb file to *.exe file, and how to work with a virtual environment, in more detail here: Convert Jupyter file (.ipynb) to Python file (.py) and Python file (.py) to executable file (.exe)

Write the Code

Let’s see the code below:

import argparse
import uuid

macs = ['0xfc327824c8e9']
your_mac_address = hex(uuid.getnode())

if your_mac_address in macs: 
    parser = argparse.ArgumentParser(description='What is your name?')
    
    parser.add_argument('-f', '--first', action='store', dest='first', help='Your First Name')
    parser.add_argument('-m', '--middle', action='store', dest='middle', help='Your Middle Name')
    parser.add_argument('-l', '--last', action='store', dest='last', help='Your Last Name')
    
    args = parser.parse_args()
    first_name = args.first if args.first else ''
    middle_name = args.middle if args.middle else ''
    last_name = args.last if args.last else ''
    
    print('Hello, {} {} {}!!!'.format(first_name, middle_name, last_name))
    
else: 
    print('Your MAC Address not allowed!!!')

Below is the explanation of the code:

  • argparse is the “recommended command-line parsing module in the Python standard library”. It’s what you use to get command line arguments into your program, and
  • import uuid is a python library that helps you to get your computer hardware MAC address
  • 0xfc327824c8e9, is my MAC address. You can change it to your computer MAC address, you can check your MAC address by running The Code #1 above, and then add it to the macs list (macs = [‘0xfc327824c8e9’, ‘your-mac-address’])

Convert *.ipynb file to *.py file

The next step is to convert your *.ipynb file to a *.py file with the below command:

ipython nbconvert --to script name.ipynb

The below picture on how to convert the *.ipynb file to *.py file in command prompt:

convert_ipynb_to_py

Convert *.py file to *.exe file

The next step is to convert your *.py file to a *.exe file with the below command:

pyinstaller --onefile --console name.py

The below picture on how to convert the *.py file to *.exe file in command prompt:

convert_py_to_exe

Testing

Your *.exe file will be in “Your-Working-Directory\dist\name.exe”, let’s test the below command to see the result:

name --first=John --middle=Wesley --last=Hardin
cli_name_john_wesley_hardin

But if the CLI tool runs on another computer with a different MAC address then your CLI tool will return a notification that your MAC address is not allowed:

mac_address_not_allowed

Conclusion

In this post, you have to learn how to create a simple CLI tool and dedicate the CLI tool only to some specific user or computer by identifying the computer’s MAC address.

And also you learn how to convert *.ipynb to *.py, and to *.exe file, but if you are eager to know more you can learn from my previous post: Convert Jupyter file (.ipynb) to Python file (.py) and Python file (.py) to executable file (.exe)

Thank you for following this post and see you in the next post.

Leave a comment