介紹
當我們使用虛擬網(wǎng)卡的時候,有時候需要為虛擬網(wǎng)卡配置隨機的MAC地址。我們知道,網(wǎng)卡的MAC地址實際上是一個6字節(jié)的整型數(shù),通常表現(xiàn)為用英文冒號(:)隔開的十六進制字符串(全部大寫或者全部小寫),如下面所示(全部小寫):
8c:ec:75:ab:b7:dc
openssl rand
命令可以生成一個n字節(jié)的數(shù),我們可以使用該命令生成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字節(jié)的偽隨機數(shù),n可以指定,-hex選項用于以十六進制輸出這個偽隨機數(shù),所以,首先生成一個6字節(jié)數(shù)的十六進制字符串:
$ 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字節(jié)數(shù)的十六進制字符串,
中間的 |
是管道符,將生成的字符串傳遞給sed
命令,sed 's/\(..\)/\1:/g; s/.$//'
先在每2個字符串后面加一個英文冒號(:),然后去掉末尾的英文冒號,這樣就得到了一個MAC地址字符串。
下面,詳細解釋一下sed 's/\(..\)/\1:/g; s/.$//'
的用法:
sed后面是一個單引號包裹的字符串,字符串里有2部分,分號(;)前面的s/\(..\)/\1:/g
的作用是在每2個字符串后面加一個英文冒號(:),分號(;)后面的s/.$//
去掉末尾的英文冒號。
s/\(..\)/\1:/g
這是一個全局替換表達式,格式為: s/要替換的字符串模式/替換成的字符串/gs
substitute,替代、替換的意思g
global,全局的意思,表示符合條件的要全部替換\(..\)
表示要匹配的字符串,\
用于轉(zhuǎn)義左右括號,其實就是(..)
,,其中,.
代表非換行符的任意字符,(..)
代表2個非換行字符組成的任意字符串\1
代表符合(..)
格式的第一個子字符串,\1:
就是在符合條件的子字符串加上一個英文冒號(:)
所以, s/\(..\)/\1:/g
指的是:在每2個字符串后面加一個英文冒號文章來源:http://www.zghlxwxcb.cn/news/detail-700736.html
2. s/.$//
這是一個替換表達式,格式為: s/要替換的字符串模式/替換成的字符串/,只替換第一個.
代表非換行符的任意一個字符$
代表末尾
所以, s/.$//
指的是:去掉最后一個字符文章來源地址http://www.zghlxwxcb.cn/news/detail-700736.html
到了這里,關(guān)于使用openssl rand隨機生成MAC地址的方法的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!