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


    介绍

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

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

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

    openssl rand

    openssl rand的用法

    # 查看openssl rand的手册
    man openssl rand
    
    • 1
    • 2
    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 .
    
    3.0.2                                                              2023-02-06                                                OPENSSL-RAND(1SSL)
    
    • 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
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53

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

    $ openssl rand -hex 6
    14480616a8f2
    
    • 1
    • 2

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

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

    这样,就生成了一个随机的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/.$//指的是:去掉最后一个字符

  • 相关阅读:
    Java接口和抽象类的区别
    每日一题 2596. 检查骑士巡视方案
    前端品优购项目准备工作
    C++——二叉搜索树
    【Java基础】23.接口
    Linux磁盘扩容(超详细)
    深入讲解Netty那些事儿之从内核角度看IO模型(下)
    合并果子(C++)[堆]
    VMWare Workstation 16 安装 Ubuntu 22.04 LTS
    C语言PTA练习题(期末考试成绩排名,新生舞会,约瑟夫游戏(序号+姓名+密码),排队点名)
  • 原文地址:https://blog.csdn.net/bitcsljl/article/details/132612846