使用openssl rand随机生成MAC地址的方法

介绍

当我们使用虚拟网卡的时候,有时候需要为虚拟网卡配置随机的MAC地址。我们知道,网卡的MAC地址实际上是一个6字节的整型数,通常表现为用英文冒号(:)隔开的十六进制字符串(全部大写或者全部小写),如下面所示(全部小写):

8c:ec:75:ab:b7:dc

openssl rand命令可以生成一个n字节的数,我们可以使用该命令生成MAC地址。

openssl rand

openssl rand的用法

# 查看openssl rand的手册
man openssl rand
OPENSSL-RAND(1SSL)                                                  OpenSSL                                                  > OPENSSL-RAND(1SSL)

NAME
       openssl-rand - generate pseudo-random bytes

SYNOPSIS
       openssl rand [-help] [-out file] [-base64] [-hex] [-engine id] [-rand files] [-writerand file] [-provider name] [-provider-path path]
       [-propquery propq] num

DESCRIPTION
       This command generates num random bytes using a cryptographically secure pseudo random number generator (CSPRNG).

       The random bytes are generated using the RAND_bytes(3) function, which provides a security level of 256 bits, provided it managed to
       seed itself successfully from a trusted operating system entropy source.  Otherwise, the command will fail with a nonzero error code.
       For more details, see RAND_bytes(3), RAND(7), and EVP_RAND(7).

OPTIONS
       -help
           Print out a usage message.

       -out file
           Write to file instead of standard output.

       -base64
           Perform base64 encoding on the output.

       -hex
           Show the output as a hex string.

       -engine id
           See "Engine Options" in openssl(1).  This option is deprecated.

       -rand files, -writerand file
           See "Random State Options" in openssl(1) for details.

       -provider name
       -provider-path path
       -propquery propq
           See "Provider Options" in openssl(1), provider(7), and property(7).

SEE ALSO
       openssl(1), RAND_bytes(3), RAND(7), EVP_RAND(7)

HISTORY
       The -engine option was deprecated in OpenSSL 3.0.

COPYRIGHT
       Copyright 2000-2021 The OpenSSL Project Authors. All Rights Reserved.

       Licensed under the Apache License 2.0 (the "License").  You may not use this file except in compliance with the License.  You can obtain
       a copy in the file LICENSE in the source distribution or at <https://www.openssl.org/source/license.html>.

3.0.2                                                              2023-02-06                                                OPENSSL-RAND(1SSL)

阅读完openssl rand的手册,我们知道,openssl rand能生成n字节的伪随机数,n可以指定,-hex选项用于以十六进制输出这个伪随机数,所以,首先生成一个6字节数的十六进制字符串:

$ openssl rand -hex 6
14480616a8f2

下面只需要每2个字符串之间加一个英文冒号(:)就可以了,我们选择sed命令来处理:

$ openssl rand -hex 6 | sed 's/\(..\)/\1:/g; s/.$//'
b1:4b:f0:6f:89:4b

这样,就生成了一个随机的MAC地址。
简单解释一下,
openssl rand -hex 6 生成一个6字节数的十六进制字符串,
中间的 | 是管道符,将生成的字符串传递给sed命令,
sed 's/\(..\)/\1:/g; s/.$//'先在每2个字符串后面加一个英文冒号(:),然后去掉末尾的英文冒号,这样就得到了一个MAC地址字符串。

下面,详细解释一下sed 's/\(..\)/\1:/g; s/.$//'的用法:
sed后面是一个单引号包裹的字符串,字符串里有2部分,分号(;)前面的s/\(..\)/\1:/g的作用是在每2个字符串后面加一个英文冒号(:),分号(;)后面的s/.$//去掉末尾的英文冒号。

  1. s/\(..\)/\1:/g

这是一个全局替换表达式,格式为: s/要替换的字符串模式/替换成的字符串/g
s substitute,替代、替换的意思
g global,全局的意思,表示符合条件的要全部替换
\(..\) 表示要匹配的字符串,\用于转义左右括号,其实就是(..),,其中,.代表非换行符的任意字符,(..)代表2个非换行字符组成的任意字符串
\1 代表符合(..)格式的第一个子字符串,\1:就是在符合条件的子字符串加上一个英文冒号(:)
所以, s/\(..\)/\1:/g指的是:在每2个字符串后面加一个英文冒号

2. s/.$//
这是一个替换表达式,格式为: s/要替换的字符串模式/替换成的字符串/,只替换第一个
. 代表非换行符的任意一个字符
$代表末尾
所以, s/.$//指的是:去掉最后一个字符

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值