Linux automatically generates a MAC address method summary

  

There are many ways to produce a MAC address under Linux. In addition to the common shell generation method, you can also generate a MAC address by Perl, ruby, etc. The automatic generation method makes a simple summary.

a, the shell generation method

shell generation method is the largest, but also the feeling is the most simple and efficient, there are several methods commonly used tools column randomly generated: < Br>

openssl tool generation

Code is as follows

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
>

Code is as follows

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///& rsquo; -e ‘s//:/g’| Head -n 1

d8:d3:67:20:c5:f2

for loop generation

The code is as follows

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};do echo -n ${h:$(($RANDOM%16)):1};if [[ $((c%2)) = 0 && $c ! = 12 ]];then echo -n :;fi;done;echo

19:7F:A9:41:E2:20

Here again, the language itself has no high-level royalties. Don't underestimate the shell, what the shell can do is to use perl, python, php, etc.

Second, perl generation method

Code is as follows

yang@crunchbang:~$ perl -e ‘printf(“%.2x:”,rand(255 ))for(1..5);printf(“%.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 is indisputable.

Third, ruby ​​generation method

Code is as follows

yang@crunchbang:~$ ruby ​​-e ‘puts (1..6).map{“%0.2 X”%rand(256)}.join(“:”)’

CD:97:ED:52:B7:F4

The method used here is almost in perl The same way.

Four, python generation method

Code is as follows

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

Centos and redhat official sites also give a python script:

The code is as follows

#! /usr/bin/python

# macgen.py script to generate a MAC address for Red Hat Virtualization guests

#

import random

#< Br>

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 the virtinst.util module, you can also use the following simple statement to generate new mac and uuid:

Code is as follows

#! /usr/bin/env python

# -*- mode: python; -*-

print “”

print “New UUID:”< Br>

import virtinst.util ; print virtinst.util.uuidToString(virtinst.util.randomUUID())

print “New MAC:”

import virtinst.util ; Print virtinst.util.randomMAC()

print “”

The above is the method of generating MAC address for Linux. This article introduces four methods to generate MAC address, you can choose The method you like to automatically generate a MAC address.

Copyright © Windows knowledge All Rights Reserved