使用 openssl 批量生成随机 MAC

使用 openssl 批量生成随机 MAC

网络管理中有时用来占用 DHCP 池,占用 ARP 记录……等用途。所以前面一段设成固定的,方便识别。


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# 格式1:AABB-CCDD-EEFF
for i in {1..256}
  do
    openssl rand -hex 4 | sed 's/^/abcd/; s/\(....\)/\1-/g; s/.$//'   >>mac.txt
  done
  
#注释:
#	openssl rand -hex 4 | sed 's/^/abcd/; s/\(....\)/\1-/g; s/.$//'
#	生成4个字节的16进制字符
#								在行首插入“abcd”
#											每隔4个字符插入一个“-”
#															删除行末最后一个字符


# 格式2:AA:BB:CC:DD:EE:FF
for i in {1..256}
  do
    openssl rand -hex 4 | sed 's/^/abcd/; s/\(..\)/\1:/g; s/.$//'   >>mac.txt
  done

# 格式3:AABBCCDDEEFF
for i in {1..256}
  do
    openssl rand -hex 4 | sed 's/^/abcd/'   >>mac.txt
  done

注意:前面插 “abcd” ,在有的网络设备,及 windows 上,会被识别位非法MAC

可以把第二位改为 “2 6 A E” 中的任意一个

参考:两种生成12位16进制字符串的方式:

1
2
3
openssl rand 6 | xxd -p

openssl rand -hex 6


比如:“405582” 开头是 “NOKIA” 的设备

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
# 格式1:AABB-CCDD-EEFF
for i in {1..256}
  do
    openssl rand -hex 3 | sed 's/^/405582/; s/\(....\)/\1-/g; s/.$//'   >>mac.txt
  done

# 格式2:AA:BB:CC:DD:EE:FF
for i in {1..256}
  do
    openssl rand -hex 3 | sed 's/^/405582/; s/\(..\)/\1:/g; s/.$//'   >>mac.txt
  done

# 格式3:AABBCCDDEEFF
for i in {1..256}
  do
    openssl rand -hex 3 | sed 's/^/405582/'   >>mac.txt
  done

相关内容