1.字符串截取
key="qwertyuiopasdfghjklzxcvbnm1234567890"
read -p "--------------num" num
randpass(){
rand=""
for i in `seq $num`
do
num=$[RANDOM%${#key}]
local tmp=${key:num:1}
rand=${rand}${tmp}
done
echo $rand
}
2.使用命令生成
2.1生成16进制随机字符串
[root@leo lianxi]# uuidgen
8f377946-c2c6-494f-829f-06b484d8c113
[root@leo lianxi]# openssl rand -hex 1
7d
[root@leo lianxi]# openssl rand -hex 2
7596
[root@leo lianxi]# openssl rand -hex 3
ee2c0d
[root@leo lianxi]# openssl rand -hex 4
c66fad11
2.2生成含有特殊字符的
[root@leo lianxi]# openssl rand -base64 1
Ng==
[root@leo lianxi]# openssl rand -base64 2
YTs=
[root@leo lianxi]# openssl rand -base64 3
Jw9n
[root@leo lianxi]# openssl rand -base64 4
rgXoBQ==
[root@leo lianxi]# openssl rand -base64 5
9WNRQ6U=
[root@leo lianxi]# openssl rand -base64 6
dgu0GW5W
[root@leo lianxi]# openssl rand -base64 10
6tui3IErbDhwuA==
[root@leo lianxi]# date +%s
1590421845
[root@leo lianxi]# date +%s%n
1590421860
3.设备文件生成随机数
[root@leo lianxi]# tr -cd '_a-zA-Z0-9' < /dev/urandom | head -c 10
oxKgM29Vll
[root@leo lianxi]# tr -cd '0-9' < /dev/urandom | head -c 10
4706212081
4.hash值生成
[root@leo lianxi]# echo a | md5sum | cut -d ' ' -f1
60b725f10c9c85c70d97880dfe8191b3
[root@leo lianxi]# sha256sum /etc/passwd | cut -d ' ' -f1
651b40b91d69b0a08884923ad53d205fe1c14eafe0a5ee19deb4824540e88a59