利用strings生成随机字符串

利用strings生成随机字符串

Linux 可以很方便产生随机字符串,用来设置初始密码或者其它一次性密码。省去了自己写随机字符串或者使用其它工具。

strings - print the strings of printable characters in files.
-n min-len
grep
-o, --only-matching
-E, --extended-regexp
Repetition
A regular expression may be followed by one of several repetition operators:
? The preceding item is optional and matched at most once.
* The preceding item will be matched zero or more times.
+ The preceding item will be matched one or more times.
{n} The preceding item is matched exactly n times.
{n,} The preceding item is matched n or more times.
{,m} The preceding item is matched at most m times. This is a GNU extension.
{n,m} The preceding item is matched at least n times, but not more than m times.
# strings -n 8 /dev/urandom | head -99 |grep -oE "\w{8}"
R0aEHAet
h9IsWpMd
otcEN02C
Bgn6OZUP
PseBIRxU
elXpXZap
8aBIsIFH
jrvmZul0
VQNwYY6B
hdAJaJ_h
UoiaShvb

8核16G计算型主机测试结果:)
# 100位的一晚上一个都没出现
strings -n 100 /dev/urandom | head |grep -oE "\w{100}"
# 20位的一晚上才出2个
strings -n 20 /dev/urandom | grep -oE "\w{20}"
# 大概30秒才出现
strings -n 12 /dev/urandom | grep -oE "\w{12}"
# 10位的比较靠谱,2秒内
strings -n 10 /dev/urandom  |grep -oE "\w{10}"
# 8位最佳满足复杂性和随机性要求,计算也快速
strings -n 8 /dev/urandom  |grep -oE "\w{8}" | head

还有个更棒的方法: 这个方法更好,不但快而且简洁。

head -1 /dev/urandom | base64 | cut -c1-10

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注