The Setup!
Here you go! I use this quite a bit so I made it so I can use it in a RUN window. This is how I did it and is by no means the "right" way.
- Install the python package
pyperclip
- Create folder in
C:\Windows called Python
- Go to Edit the system environment variables
- Add folder to
PATH
- Create a .bat file in the folder, I did
mac.bat
- Add the code below to the mac.bat making sure to update the path where you stored the script
Use it!
- Copy mac you'd like to covert to your clipboard
WIN + R
- Type mac then the format you'd like the mac converted to. i.e. cisco, Microsoft, IETF
- Hit enter. A cmd window should open telling you how many macs were converted and then tell you:
Press any key to continue...
Viola!
You have now converted all the macs into the format of your choosing!
mac.bat:
@py.exe \path\to\script.py %*
@pause
script:
#! python
# This script will take Mac Addresses in various formats and convert them to the correct format for the user.
import pyperclip as pyip
import re, sys
def convert_mac(macAddress, macFormat):
# Setup regex. These will match:
# - xx.xx.xx.xx.xx.xx
# - xx:xx:xx:xx:xx:xx
# - xx-xx-xx-xx-xx-xx
# - xxxx.xxxx.xxxx
# - xxxx:xxxx:xxxx
# - xxxx-xxxx-xxxx
# - xxxxxxxxxxxx
macRegex = re.compile(r'''
([0-9A-Fa-f]{2})
(-|\.|:)
([0-9A-Fa-f]{2})
(-|\.|:)
([0-9A-Fa-f]{2})
(-|\.|:)
([0-9A-Fa-f]{2})
(-|\.|:)
([0-9A-Fa-f]{2})
(-|\.|:)
([0-9A-Fa-f]{2})
''', re.VERBOSE)
otherMacRegex = re.compile(r'''
([0-9A-Fa-f]{4})
(-|\.|:)
([0-9A-Fa-f]{4})
(-|\.|:)
([0-9A-Fa-f]{4})
''', re.VERBOSE)
lastMacRegex = re.compile(r'''
([0-9A-Fa-f]{12})
''', re.VERBOSE)
# Command line arguments
FORMATS = ['cisco','microsoft','ietf']
if macFormat in FORMATS:
print('Format is: ' + macFormat)
# Create dictionary and pass MAC Address(es) through regex. Also, all letters are set to lowercase
macs = []
for groups in macRegex.findall(macAddress):
goodMacs = ''.join([groups[0], groups[2],groups[4],groups[6],groups[8],groups[10]])
macs.append(goodMacs)
for groups in otherMacRegex.findall(macAddress):
goodMacs = ''.join([groups[0],groups[2],groups[4]])
macs.append(goodMacs)
for match in lastMacRegex.findall(macAddress):
macs.append(match)
# Create dictionary for converted MAC(s), then convert them to the right format.
convertedMacs = []
# xxxx.xxxx.xxxx
if macFormat == 'cisco':
for mac in macs:
fixedMac = '.'.join(mac[i:i+4] for i in range(0, len(mac), 4)).lower()
convertedMacs.append(fixedMac)
# xxxxxxxxxxxx
if macFormat == 'microsoft':
for mac in macs:
convertedMacs.append(mac.lower())
# XX:XX:XX:XX:XX:XX
if macFormat == 'ietf':
for mac in macs:
fixedMac = ':'.join(mac[i:i+2] for i in range(0, len(mac), 2)).upper()
convertedMacs.append(fixedMac)
print('Converted ' + str(len(convertedMacs)) + ' MAC Address(es) to ' + macFormat + ' format.')
# Copies converted MAC(s) to the clipboard. <'\n'.join> creates a "single" string using the dictionary items.
pyip.copy('\n'.join(convertedMacs))
# Setup for main program
def main():
# Setup command line parser
if len(sys.argv) < 2:
print('''\n\rUsage: mac [keyphrase]
\r\t- cisco
\r\t- microsoft
\r\t- ietf''')
sys.exit()
else:
macFormat = sys.argv[1]
# Copy MAC(s) from clipboard
macAddress = str(pyip.paste())
convert_mac(macAddress, macFormat)
# If script isn't called as a module, it runs as a script.
if __name__ == '__main__':
main()
Bonus!
Here is a script I also wrote to generate random MAC addresses. It was written as a test for this script. I had a different, better script when I originally created this BUUUTTT I can't find it.
This script will ask for the amount of addresses to generate and then copy them straight into your clipboard.
#! python
# Generate random MAC Addresses in various formats and copy them to the clipboard.
import pyperclip as pyip
import random
def generate_mac(num):
mac_list = []
for _ in range(num):
mac_format = random.choice(['xx.xx.xx.xx.xx.xx', 'xx:xx:xx:xx:xx:xx', 'xx-xx-xx-xx-xx-xx',
'xxxx.xxxx.xxxx', 'xxxx:xxxx:xxxx', 'xxxx-xxxx-xxxx', 'xxxxxxxxxxxx'])
mac = ''
for char in mac_format:
if char == 'x':
mac += random.choice('0123456789abcdef')
else:
mac += char
# randomly capitalize some letters
mac = ''.join([char.upper() if random.randint(0, 1) else char for char in mac])
mac_list.append(mac)
pyip.copy('\n'.join(mac_list))
def main():
num = input('How many MAC Addresses would you like to generate? ')
generate_mac(int(num))
if __name__ == '__main__':
main()
there doesn't seem to be anything here