No Account Yet?

You are not logged in.

Add to: JBookmarks Add to: Facebook Add to: Windows Live Add to: Digg Add to: Del.icoi.us Add to: Reddit Add to: StumbleUpon Add to: Slashdot Add to: Netscape Add to: Furl Add to: Yahoo Add to: Blogmarks Add to: Technorati Add to: Newsvine Add to: Google Information
How to generate MAC Addresses for XEN E-mail
Programming HowTo's - Python HowTo's
Written by Allen Sanabria   
Tuesday, 22 April 2008 20:18
#!/usr/bin/env python
# Quick Python script to generate random valid MAC address for XEN Domains
#Copyright (C) 2008 Allen Sanabria

#This program is free software; you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation; either version 2 of the License, or
#(at your option) any later version.

#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#GNU General Public License for more details.

#You should have received a copy of the GNU General Public License along
#with this program; if not, write to the Free Software Foundation, Inc.,
#51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

# This address range is reserved for use by Xen 00:16:3E
# I'm importing the function choice of the random module
# The reason for this is so that it will pick a random character from the string I gave it to generate a valid MAC for XEN
from random import choice
from sys import stdin
def x():
X = choice("0123456789ABCDEF")
return str(X)
print "Enter how many MAC Addresses do you want me to generate: "
mac = stdin.readline()
mac_list = []
for i in range(int(mac)):
mac_list.append("00:16:3E"+":"+x()+x()+":"+x()+x()+":"+x()+x())
for con in range(len(mac_list)):
while mac_list.count(mac_list[con]) > 1:
print "OH NOOO DUPPPE "+mac_list[con]
mac_list.pop(con)
mac_list.insert(con, "00:16:3E"+":"+x()+x()+":"+x()+x()+":"+x()+x())
print mac_list[con]

OUTPUT

python random1.py
Enter how many MAC Addresses do you want me to generate:
5
00:16:3E:09:EB:6F
00:16:3E:5E:4B:AE
00:16:3E:E0:EC:F9
00:16:3E:CF:2A:AE
00:16:3E:A9:D8:DB
Comments
Search RSS
dynasty  - My First Python Post     |SAdministrator |2008-04-23 18:45:47
This is my first post on one of my python scripts... So please let me know if I
should explain in more details on what each part of the script does.
ikenticus   |SAdministrator |2008-05-05 17:13:33
I think the beauty of python is it ability to munge even more into a single
line than perl, such that a randomizer run once will not
generate dupes:

Code:

import os, sys
from random import randint

if len(sys.argv)<2:
print
'Usage: %s [0-9]+' % os.path.basename(sys.argv[0])

sys.exit(1)

for mac in [ ':'.join(['%02x' % randint(0,255) for p in
range(3)]) for r in range(int(sys.argv[1])) ]:
  print '00:16:3e:' + mac


Boy, this "write comment" feature really sucks about preserving
preformatted code. There's nothing wrong with Allen's code, I was
just fooling around with seeing how much more compact I could make it
and this was the best I could come up with so far. I'll probably use this
snippet in another code basics tutotial later on.
Only registered users can write comments!

3.22 Copyright (C) 2007 Alain Georgette / Copyright (C) 2006 Frantisek Hliva. All rights reserved."

Last Updated ( Saturday, 19 July 2008 14:03 )
 

 

Python News