Python 创建随机mac地址单播、组播

创建随机mac地址

import random 


def randomMAC(): 
    mac = [ 
        random.randint(0x00,0x7f),random.randint(0x00,0xff),0xff)
    ]
    return ':'.join(map(lambda x: "%02x" % x,mac))


print(randomMAC())

创建部分值固定的随机MAC地址

import random 


def randomMAC(): 
    mac = [ 
        0x52,0x54,0x00,mac))


print(randomMAC())

创建单播、组播随机mac地址

import random 
import subprocess
from subprocess import Popen,PIPE,STDOUT


def randomMAC():
    res = "1"

    while True:
        mac = [ 
            random.randint(0x00,0xff)
        ]

        target_mac = ':'.join(map(lambda x: "%02x" % x,mac))
        target_mac_two = target_mac.split(":",1)[0]
        target_mac_two = "0x{}".format(target_mac_two)
        cmd = "echo $(({}&1))".format(target_mac_two)
        proc = subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
        buff = proc.communicate()[0]
        res = buff.split()[0]
        if res == "0":
            break

    return res,target_mac


res,target_mac = randomMAC()
print(res,target_mac)

相关文章

功能概要:(目前已实现功能)公共展示部分:1.网站首页展示...
大体上把Python中的数据类型分为如下几类: Number(数字) ...
开发之前第一步,就是构造整个的项目结构。这就好比作一幅画...
源码编译方式安装Apache首先下载Apache源码压缩包,地址为ht...
前面说完了此项目的创建及数据模型设计的过程。如果未看过,...
python中常用的写爬虫的库有urllib2、requests,对于大多数比...