V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
sweetomy
V2EX  ›  问与答

如何生成随机的 mac 地址 且第二位必须是偶数

  •  
  •   sweetomy · 2015-03-09 21:55:06 +08:00 · 2142 次点击
    这是一个创建于 3354 天前的主题,其中的信息可能已经有所发展或是发生改变。
    网上是这样生成的 openssl rand -hex 6 | sed 's/\(..\)/\1:/g; s/.$//' 但有时第二位不是偶数 郁闷
    5 条回复    2015-03-10 11:01:26 +08:00
    ryd994
        1
    ryd994  
       2015-03-09 23:11:08 +08:00 via Android
    生成一个,验证如果不是就重新生成
    检查第二位不难吧
    Septembers
        2
    Septembers  
       2015-03-09 23:38:32 +08:00
    echo "from random import randint, choice; mac = map(lambda _: randint(0, 0xFF), xrange(6)); mac[2] = choice(range(0, 255, 2)); print ':'.join(map(lambda x: '%02x' % x, mac))" | python

    see https://www.centos.org/docs/5/html/5.2/Virtualization/sect-Virtualization-Tips_and_tricks-Generating_a_new_unique_MAC_address.html
    sweetomy
        3
    sweetomy  
    OP
       2015-03-10 09:00:45 +08:00
    @Septembers 第二位不是偶数
    invite
        4
    invite  
       2015-03-10 10:20:49 +08:00
    你需要用哪类代码生成。
    sweetomy
        5
    sweetomy  
    OP
       2015-03-10 11:01:26 +08:00
    @invite 随便呀
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   5482 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 41ms · UTC 09:26 · PVG 17:26 · LAX 02:26 · JFK 05:26
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.