微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐