MAC Format – Part 2 (Menu)

MAC Format – Part 2 (Menu)

Previously I wrote a quick post on formatting MAC Addresses using Python and a csv file. In this post, I used Pandas and MS Excel (since this data ends up in Excel most of the time). In my previous post, I used the “join” function; but Keith Miller suggested I should look into the macaddress module.

This module gives lots of different options for MAC Address formatting and eliminates some of the manual process.

Initially, I simply wrote a script by defining functions, but decided to use a menu driven script that will allow different options. I can customize it for my needs and also add additional menu options. Multiple options here and it is not the most efficient way, but it is a start. I’ll work on making it more efficient next. It has been a great learning experience so far; so many possibilities.

Here is my full script using a menu:

import csv
import pandas as pd
import macaddress

def print_menu():
    print(20 * "-", "Format MAC Addresses", 20 * "-")
    print("1. Format M:A:C CSV")
    print("2. Format M-A-C CSV")
    print("3. Format M:A:C XLSX")
    print("4. Format M-A-C XLSX")
    print("5. Exit")
    print(62 * "-")

loop = True

while loop:  ## While loop which will keep going until loop = False
    print_menu()
    choice = input("Enter your choice [1-5]: ")
    choice = int(choice)

    if choice == 1:
        print('Converts from xxxxxxxxxxxx to xx:xx:xx:xx:xx:xx and lower case')
        file_name = input('Enter File Name:' )+'.csv'
        with open(file_name, 'r', encoding='utf-8-sig') as f:
            csv_reader = csv.reader(f, delimiter=',')
            next(csv_reader)
            for rows in csv_reader:
                data = (rows[0])
                mac_data = macaddress.MAC(data)
                col_format = str(mac_data).lower().replace('-', ':')
                print(col_format)
    elif choice == 2:
        print('Converts from xxxxxxxxxxxx to xx-xx-xx-xx-xx-xx and lower case')
        file_name = input('Enter File Name:' ) +'.csv'
        with open(file_name, 'r', encoding='utf-8-sig') as f:
            csv_reader = csv.reader(f, delimiter=',')
            next(csv_reader)
            for rows in csv_reader:
                data = (rows[0])
                mac_data = macaddress.MAC(data)
                print(mac_data)
    elif choice == 3:
        print('Converts from xxxxxxxxxxxx to xx:xx:xx:xx:xx:xx and lower case - EXCEL')
        file_name = input('Enter File Name:') + '.xlsx'
        df = pd.read_excel('ap_macs.xlsx', usecols=['apmac'], index_col=False)
        df_str = pd.DataFrame.to_string(df, index=0, header=None)
        df_data = df_str.splitlines()
        for i in df_data:
            data = (i)
            mac_data = macaddress.MAC(data.strip())
            # print(mac_data)
            col_format = str(mac_data).lower().replace('-', ':')
            print(col_format)
    elif choice == 4:
        print('Converts from xxxxxxxxxxxx to xx-xx-xx-xx-xx-xx and lower case - EXCEL')
        file_name = input('Enter File Name:') + '.xlsx'
        df = pd.read_excel('ap_macs.xlsx', usecols=['apmac'], index_col=False)
        df_str = pd.DataFrame.to_string(df, index=0, header=None)
        df_data = df_str.splitlines()
        for i in df_data:
            data = (i)
            mac_data = macaddress.MAC(data.strip())
            print(mac_data)
    elif choice == 5:
        print('Menu5')
        loop = False  # This will make the while loop to end as not value of loop is set to False
    else:
        # Any integer inputs other than values 1-5 we print an error message
        print("Wrong option selection. Enter any key to try again..")

Feel free to share your feedback and any improvements you’d like to suggest so this can help the whole community.

NOTE: Always remember to check the script in a lab if you copy and paste it from the web, and use it at your own risk.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

WordPress SEO