bash 如何通过命令行获取 32 个十六进制数字的随机字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34328759/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get a random string of 32 hexadecimal digits through command line?
提问by Ana
I'd like to put together a command that will print out a string of 32 hexadecimal digits. I've got a Python script that works:
我想组合一个命令来打印出一串 32 位十六进制数字。我有一个有效的 Python 脚本:
python -c 'import random ; print "".join(map(lambda t: format(t, "02X"), [random.randrange(256) for x in range(16)]))'
This generates output like:
这会生成如下输出:
6EF6B30F9E557F948C402C89002C7C8A
Which is what I need.
这就是我需要的。
On a Mac, I can even do this:
在 Mac 上,我什至可以这样做:
uuidgen | tr -d '-'
However, I don't have access to the more sophisticated scripting languages ruby and python, and I won't be on a Mac (so no uuidgen). I need to stick with more bash'ish tools like sed, awk, /dev/random because I'm on a limited platform. Is there a way to do this?
但是,我无法使用更复杂的脚本语言 ruby 和 python,而且我不会使用 Mac(因此没有 uuidgen)。我需要坚持使用更多 bash'ish 工具,如 sed、awk、/dev/random,因为我在一个有限的平台上。有没有办法做到这一点?
回答by Renaud Pacalet
If you have hexdump
then:
如果你有hexdump
那么:
hexdump -n 16 -e '4/4 "%08X" 1 "\n"' /dev/random
should do the job.
应该做的工作。
Explanation:
解释:
-n 16
to consume 16 bytes of input (32 hex digits = 16 bytes).4/4 "%08X"
to iterate four times, consume 4 bytes per iteration and print the corresponding 32 bits value as 8 hex digits, with leading zeros, if needed.1 "\n"
to end with a single newline.
-n 16
消耗 16 个字节的输入(32 个十六进制数字 = 16 个字节)。4/4 "%08X"
要迭代四次,每次迭代消耗 4 个字节,并将相应的 32 位值打印为 8 个十六进制数字,如果需要,带有前导零。1 "\n"
以一个换行符结束。
Note: this solution uses /dev/random
but it could as well use /dev/urandom
. The choice between the two is a complex question and out of the scope of this answer. If you are not sure, have a look maybe at this other question.
注意:此解决方案使用/dev/random
但它也可以使用/dev/urandom
. 两者之间的选择是一个复杂的问题,超出了本答案的范围。如果您不确定,请查看其他问题。
回答by touzoku
If you are looking for a single command and have openssl installed, see below. Generate random 16 bytes (32 hex symbols) and encode in hex (also -base64 is supported).
如果您正在寻找单个命令并安装了 openssl,请参见下文。生成随机 16 字节(32 个十六进制符号)并以十六进制编码(也支持 -base64)。
openssl rand -hex 16
回答by touzoku
There three ways that I know of:
我知道的三种方式:
#!/bin/bash
n=16
# Read n bytes from urandom (in hex):
xxd -l "$n" -p /dev/urandom | tr -d " \n" ; echo
od -vN "$n" -An -tx1 /dev/urandom | tr -d " \n" ; echo
hexdump -vn "$n" -e ' /1 "%02x"' /dev/urandom ; echo
Use one, comment out the other two.
使用一个,注释掉另外两个。
回答by caveman
Here are a few more options, all of which have the nice property of providing an obvious and easy way to directly select the length of the output string. In all the cases below, changing the '32' to your desired string length is all you need to do.
这里还有一些选项,所有这些选项都提供了一种直接选择输出字符串长度的明显且简单的方法。在以下所有情况下,您只需将“32”更改为所需的字符串长度即可。
#works in bash and busybox, but not in ksh
tr -dc 'A-F0-9' < /dev/urandom | head -c32
#works in bash and ksh, but not in busybox
tr -dc 'A-F0-9' < /dev/urandom | dd status=none bs=1 count=32
#works in bash, ksh, AND busybox! w00t!
tr -dc 'A-F0-9' < /dev/urandom | dd bs=1 count=32 2>/dev/null
EDIT: Tested in different shells.
编辑:在不同的外壳中测试。
回答by Jay Sullivan
Try:
尝试:
xxd -u -l 16 -p /dev/urandom
Example output:
示例输出:
C298212CD8B55F2E193FFA16165E95E3