-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrandmac
executable file
·85 lines (62 loc) · 1.82 KB
/
randmac
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#! /usr/bin/env python
#
# Generate a random, valid MAC address
#
import random, binascii
import os, sys
import string, re
from optparse import Option, OptionParser, OptionValueError
from binascii import hexlify
Mfgprefix = [
('000569', 'VMWare'),
('000C29', 'VMWare'),
('001c14', 'VMWare'),
('005056', 'VMWare'),
('001c42', 'Parallels'),
('00163e', 'Xensource'),
]
def randhex(n=6):
z = os.urandom(3)
return "%02x:%02x:%02x" % (ord(z[0]), ord(z[1]), ord(z[2]))
def read_oui(fn):
fd = open(fn, 'rb')
pat = re.compile(r'^\s*([0-9A-F]{2}-[0-9A-F]{2}-[0-9A-F]{2})\s+\(hex\)\s+(.*)$', re.I)
db = []
for l in fd:
if len(l) == 0:
continue
m = pat.match(l)
if m is not None:
pref = m.group(1)
comp = m.group(2).strip()
p = pref.replace('-','')
t = (p, comp)
db.append(t)
#print "%s -- %s" % (p, comp)
fd.close()
return db
usage = """%s: Generate valid, Random Ethernet MAC address
Usage: %s [options]""" % (sys.argv[0], sys.argv[0])
parser = OptionParser(usage)
parser.add_option("", "--oui", dest="oui", type="string",
default=None, metavar='F',
help="Read the IEEE OUI database from file 'F' [%default]")
parser.add_option("-v", "--verbose", dest="verbose",
action="store_true", default=False,
help="Show verbose output [%default]")
opt, args = parser.parse_args()
if opt.oui is not None:
db = read_oui(opt.oui)
else:
db = Mfgprefix
random.shuffle(db)
random.shuffle(db)
t = random.sample(db, 1)[0]
p = t[0]
pref = "%s:%s:%s" % (p[0:2], p[2:4], p[4:])
mac = "%s:%s" % (pref.lower(), randhex())
if opt.verbose:
print("%s - %s" % (mac, t[1]))
else:
print(mac)
# EOF