Home  >  Article  >  Backend Development  >  Python生成随机MAC地址

Python生成随机MAC地址

WBOY
WBOYOriginal
2016-06-10 15:17:302204browse

利用python代码生成一个随机的MAC地址,使用python网络编程时或可用上,如果使用scapy模块则可直接利用RandMAC()函数来生成MAC。

python

复制代码 代码如下:

import random
Maclist = []
for i in range(1,7):
    RANDSTR = "".join(random.sample("0123456789abcdef",2))
    Maclist.append(RANDSTR)
RANDMAC = ":".join(Maclist)
print RANDMAC
--------------------------------运行结果-----------------------------------
e4:13:0e:1a:73:f5

下列的Fake_HW是用struct打包成二进制格式的mac地址

复制代码 代码如下:

import random
import struct
mac_bin_list = []
mac_hex_list = []
for i in range(1,7):
    i = random.randint(0x00,0xff)
    mac_bin_list.append(i)
Fake_HW = struct.pack("BBBBBB",mac_bin_list[0], mac_bin_list[1], mac_bin_list[2], mac_bin_list[3], mac_bin_list[4], mac_bin_list[5])
for j in mac_bin_list:
    mac_hex_list.append(hex(j))
Hardware = ":".join(mac_hex_list).replace("0x","")
print Hardware
--------------------结果-----------------------------
24:c7:6f:92:2c:42

以上就是本文的全部内容了,希望大家能够喜欢。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn