Last active
May 16, 2018 18:30
-
-
Save tylerkerr/b8d70e3d5b3c75bb4dc1684014bc94e0 to your computer and use it in GitHub Desktop.
quick MAC address formatter. it's really annoying to manually insert/remove colons
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import sys | |
import argparse | |
import subprocess | |
import re | |
def cleanmac(mac): | |
return re.sub('[^0-9a-f]', '', mac.lower()) | |
def checkmac(mac): | |
try: | |
assert len(mac) == 12 | |
except: | |
print("[!] invalid MAC address length") | |
sys.exit(1) | |
try: | |
for c in mac: | |
assert c in '0123456789abcdef' | |
except: | |
print("[!] invalid characters in MAC address") | |
sys.exit(1) | |
return True | |
def makecisco(mac): | |
return mac[:4] + '.' + mac[4:8] + '.' + mac[8:] | |
def makecolon(mac): | |
return mac[:2] + ':' + mac[2:4] + ':' + mac[4:6] + ':' + mac[6:8] + ':' + mac[8:10] + ':' + mac[10:12] | |
def main(): | |
parser = argparse.ArgumentParser() | |
parser.add_argument("mac", help="MAC address", action="store", type=str) | |
parser.add_argument("-a", "--ad", help="active directory format, no punctuation", action="store_true") | |
parser.add_argument("-s", "--standard", help="messy colon format", action="store_true") | |
parser.add_argument("-c", "--cisco", help="cisco-style groups of four chars", action="store_true") | |
args = parser.parse_args() | |
mac = cleanmac(args.mac) | |
checkmac(mac) | |
if args.ad: | |
print(mac + subprocess.getoutput('echo "$(tput bold) (copied) $(tput sgr0)"')) | |
subprocess.getoutput('echo ' + mac + ' | pbcopy') | |
elif args.standard: | |
print(makecolon(mac) + subprocess.getoutput('echo "$(tput bold) (copied) $(tput sgr0)"')) | |
subprocess.getoutput('echo ' + makecolon(mac) + ' | pbcopy') | |
elif args.cisco: | |
print(makecisco(mac) + subprocess.getoutput('echo "$(tput bold) (copied) $(tput sgr0)"')) | |
subprocess.getoutput('echo ' + makecisco(mac) + ' | pbcopy') | |
else: | |
print(mac) | |
print(makecisco(mac) + subprocess.getoutput('echo "$(tput bold) (copied) $(tput sgr0)"')) | |
subprocess.getoutput('echo ' + makecisco(mac) + ' | pbcopy') | |
print(makecolon(mac)) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment