Get the App
SLTechnology News&Howtos  ›  Servers  › 

What are the methods of automatically generating MAC addresses by Linux

Shulou Source: shulou.com Published: 2022-06-01 15:50:12 09月16日 Update

This article introduces the relevant knowledge of "what are the methods of automatically generating MAC addresses by Linux". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

There are many ways to generate MAC addresses under Linux. In addition to the common shell generation methods, you can also generate MAC addresses through Perl, ruby and other methods. The following editor makes a simple summary of the automatic generation methods of MAC addresses.

1. Shell generation method

Shell generation methods are the most numerous and feel to be the most simple and efficient. Here are several methods for random generation of commonly used tools:

Openssl tool generation

The code is as follows

Yang@crunchbang:~$ openssl rand-hex 6 | sed's / (. ) / 1GRAPHERA g; sUnix. UniUnix

A0:77:d4:ef:08:7d

Yang@crunchbang:~$ openssl rand 6 | xxd-p | sed's / (. ) / 1PUBG / 1GUBG; sPUBGUBG

3b:7f:95:c8:39:6d

Od generation

The code is as follows

Yang@crunchbang:~$ od-An-N10-x / dev/random | md5sum | sed-r's / ^ (. {10}). * $/ 1max; s / ([0-9a-f] {2}) / 1rig; sbank; $/ 1max; s / (0-max] {2}) / 1g; g; g, g

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 cycle generation

The code is as follows

Yang@crunchbang:~$ for i in {1... 6}; do printf "% 0.2X:" $[$RANDOM% 0x100]; done | sed's Gunther

8E:9E:FB:AE:FF:D2

Yang@crunchbang:~$ hog0123456789ABCDEF for c in {1.. 12}; do echo-n ${hgroup $(($RANDOM)): 1}; if [[$((c% 2) = 0 & & $c! = 12]]; then echo-n:; fi;done;echo

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

Here again sigh, the language itself is not high-level, do not underestimate why shell,shell can achieve why we have to use perl, python, php and so on to achieve.

2. Perl generation method

The 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 strength and simplicity of perl is indisputable.

Third, ruby generation method

The code is as follows

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.

4. Python generation method

The code is as follows

Yang@crunchbang:~$ python-c "from itertools import imap; from random import randint; print': '.join ([' x% x for x in imap (lambda x:randint (0255), range (6)]])"

52:75:80:68:3a:cc

A python script is also available on the official centos and redhat sites:

The code is as follows

#! / 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: "x"% x, mac))

#

Print randomMAC ()

When you have a virtinst.util module, you can also generate new mac and uuid using the following simple statements:

The code is as follows

#! / 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 ""

This is the end of the content of "what are the methods for Linux to automatically generate MAC addresses". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Generation method code address automatic generation content tools more knowledge practical powerful concise advanced learned and then indisputable again at the same time dilemma official Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia MySQL Shulou Technology Shulou Tech Info Docker