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

2015-03-09 21:55:06 +08:00
 sweetomy
网上是这样生成的 openssl rand -hex 6 | sed 's/\(..\)/\1:/g; s/.$//' 但有时第二位不是偶数 郁闷
2146 次点击
所在节点    问与答
5 条回复
ryd994
2015-03-09 23:11:08 +08:00
生成一个,验证如果不是就重新生成
检查第二位不难吧
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
2015-03-10 09:00:45 +08:00
@Septembers 第二位不是偶数
invite
2015-03-10 10:20:49 +08:00
你需要用哪类代码生成。
sweetomy
2015-03-10 11:01:26 +08:00
@invite 随便呀

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/175657

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX