Several methods of randomly generating MAC address

Source: Internet
Author: User
Tags imap join openssl rand uuid python script

First, Shell generation method

Shell-generated methods are the most, but also feel the most simple and efficient, here are several common tools to generate random methods:

OpenSSL tool generation

The code is as follows Copy Code

yang@crunchbang:~$ OpenSSL Rand-hex 6 | Sed ' s/(..) /1:/g; s/.$//'
a0:77:d4:ef:08:7d
yang@crunchbang:~$ OpenSSL Rand 6 | Xxd-p | Sed ' s/(..) /1:/g; s/:$//'
3b:7f:95:c8:39:6d

OD generation

The code is as follows Copy Code

yang@crunchbang:~$ Od-an-n10-x/dev/random | md5sum | Sed-r ' s/^ (. { 10}). *$/1/; s/([0-9a-f]{2})/1:/g; s/:$//; '
B0:85:1a:41:b1
yang@crunchbang:~$
yang@crunchbang:~$ od/dev/urandom-w6-tx1-an|sed-e ' s///'-E ' s//:/g ' |head-n 1
D8:d3:67:20:c5:f2

For Loop generation

The code is as follows Copy Code

yang@crunchbang:~$ for I in {1..6}; Do printf "%0.2x:" $[$RANDOM% 0x100]; Done | Sed ' s/:$/n/'
8e:9e:fb:ae:ff:d2
yang@crunchbang:~$ h=0123456789abcdef;for C in {1..12};d o echo-n ${h:$ (($RANDOM%16)): 1};if [[$ ((c%2)) = 0 && $c !=]];then echo-n:; fi;done;echo
19:7f:a9:41:e2:20

Here again, the language itself is not high and low, do not despise the Shell,shell can realize why must use Perl, Python, PHP and so on to achieve.

Second, Perl generation method

The code is as follows Copy Code

yang@crunchbang:~$ perl-e ' printf ("%.2X:", rand (255)) for (1..5);p rintf ("%.2xn", rand (255)) '
F8:42:c1:d4:a8:28
yang@crunchbang:~$ perl-e ' Print join (":", Map {sprintf "%0.2x", Rand (256)} (1..6)). " N "'
A7:02:bd:bc:59:e2

The power and simplicity of Perl are indisputable.

Three, Ruby generation method

The code is as follows Copy Code

yang@crunchbang:~$ ruby-e ' puts (1..6) map{"%0.2x"%rand (256)}.join (":")
Cd:97:ed:52:b7:f4

The method used here is almost the same as in Perl.

Four, Python generation method

The code is as follows Copy Code

yang@crunchbang:~$ python-c "from itertools import IMAP; from random import randint; print ': '. Join (['%02x '%x for x in IMAP (Lambda X:randint (0,255), Range (6))]) "
52:75:80:68:3a:cc

A Python script is also given at the official site of CentOS and Redhat:

The code is as follows Copy Code

#!/usr/bin/python
# macgen.py script to generate a MAC address for Red Hat virtualization guests
#
Import Random
#
Def randommac ():
Mac = [0x00, 0x16, 0x3e,
Random.randint (0x00, 0x7f),
Random.randint (0x00, 0xFF),
Random.randint (0x00, 0xFF)]
Return ': '. Join (Map (lambda x: "%02x"% x, Mac))
#
Print Randommac ()

When you have a virtinst.util module, you can also use the following simple statement to generate a new Mac and UUID:

  code is as follows copy code

#!/usr/bin /env python
# -*-mode:python;-*-
print "
print" New UUID: "
Import virtinst.util; Print virtinst.util.uuidToString (Virtinst.util.randomUUID ())
print "New MAC:"
Import virtinst.util; Print Virtinst.util.randomMAC ()
print ""

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.