Blog |Follow Nick on Twitter| About
 

In the last few days I've started to learn python and gone a bit python mad with it, as a result I have a bunch of test scripts where I have been learning to do stuff. This is one of my first ones, basically I've taken this guy craig's normalisation technique and used to to create outputs.

The basic premise is you can input a MAC address in any of the three formats (EUI, Microsoft, Cisco) and it will spit out the mac address in all three... simplifying copy/paste if you have given MACs in one format and need another :)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/env python

# My Post: https://www.linickx.com/3970/python-and-mac-addresses

# REF: http://craigbalfour.blogspot.co.uk/2008/10/normalizing-mac-address-string.html
# REF http://www.cyberciti.biz/faq/python-command-line-arguments-argv-example/

# Lib
import sys, getopt

# Function

def printhelp():
  print("Usage: %s -m MAC " % sys.argv[0])
  print("\n MAC can be in any of the following formats: ")
  print(" - 00:00:00:00:00:00")
  print(" - 00-00-00-00-00-00")
  print(" - 0000.0000.0000")
  print("\n Version %s" % version)

# Defaults

addr=""
version="1.01"

# CLI input
try:
    myopts, args = getopt.getopt(sys.argv[1:],"m:h")
except getopt.GetoptError as e:
    print (str(e))
    printhelp()
    sys.exit(2)

# o == option
# a == argument passed to the o

for o, a in myopts:
    if o == '-m':
        addr=a
    if o == '-h':
        printhelp()
        sys.exit()

if addr == "":
  addr = raw_input("Enter the MAC address: ")

# Determine which delimiter style out input is using
if "." in addr:
  delimiter = "."
elif ":" in addr:
  delimiter = ":"
elif "-" in addr:
  delimiter = "-"

# Eliminate the delimiter
m = addr.replace(delimiter, "")

m = m.lower()
u = m.upper()

# convert!
cisco= ".".join(["%s%s%s%s" % (m[i], m[i+1], m[i+2], m[i+3]) for i in range(0,12,4)])
eui= ":".join(["%s%s" % (m[i], m[i+1]) for i in range(0,12,2)])
ms= "-".join(["%s%s" % (u[i], u[i+1]) for i in range(0,12,2)])

print "Cisco: " + cisco
print "EUI: " + eui
print "Microsoft: " + ms

... I'm sure there will be more of these to come!

UPDATE: I've created a gist for convertmac.py. The code above has been modified to support a command line argument... like so...

linickx:~ nick$ ./convertmac.py -m 11:22:33:11:33:11
Cisco: 1122.3311.3311
EUI: 11:22:33:11:33:11
Microsoft: 11-22-33-11-33-11
linickx:~ nick$

rgds,

Nick

 
Nick Bettison ©