Python生成随机MAC地址

利用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

以上是 Python生成随机MAC地址 的全部内容, 来源链接: utcz.com/z/348662.html

回到顶部