您的位置:首页 > 运维架构 > Linux

500、Linux获取随机密码

2016-05-04 10:40 483 查看

1、通过md5sum

[oldboy@oldboy ~]$ date | md5sum
0c0224bab3b2253d03680df246f2244c  -


2、通过urandom

[root@oldboy ~]# head -1 /dev/urandom | cksum
3464856893 212


3、通过环境变量获取

[oldboy@oldboy ~]$ echo ${RANDOM}
21524
[oldboy@oldboy ~]$ echo ${RANDOM}
5722
[oldboy@oldboy ~]$ echo ${RANDOM}
4910


4、获取定长随机密码

[stu09@oldboy ~]$ date | md5sum
f125f769f6a793c383c5ed343e75bb35  -
[stu09@oldboy ~]$ date | md5sum | cut -c 1-8		# 方法1
6d4b2274
[stu09@oldboy ~]$ head -1 /dev/urandom | cksum
4195806788 8
[stu09@oldboy ~]$ head -1 /dev/urandom | cksum | cut -c 1-8		# 方法2
17477213


5、示例:批量创建10个用户stu01-stu10,并且设置随机8位密码,要求不能用shell的循环(例如:for,while等),只能用linux命令及管道实现。

[root@oldboy ~]# head -1 /dev/urandom | cksum | cut -c 1-8	##获取8位随机数
31837928
[root@oldboy ~]# head -1 /dev/urandom | cksum | cut -c 1-8	##获取8位随机数
18928057
[root@oldboy ~]# head -1 /dev/urandom | cksum | cut -c 1-8	##获取8位随机数
10311251
[root@oldboy ~]# echo stu{01..10} | xargs -n 1 | awk '{print "useradd "$0"; echo `head -200 /dev/urandom | cksum | cut -c 1-8` | passwd --stdin "$0'}
useradd stu01; echo `head -200 /dev/urandom | cksum | cut -c 1-8` | passwd --stdin stu01
useradd stu02; echo `head -200 /dev/urandom | cksum | cut -c 1-8` | passwd --stdin stu02
useradd stu03; echo `head -200 /dev/urandom | cksum | cut -c 1-8` | passwd --stdin stu03
useradd stu04; echo `head -200 /dev/urandom | cksum | cut -c 1-8` | passwd --stdin stu04
useradd stu05; echo `head -200 /dev/urandom | cksum | cut -c 1-8` | passwd --stdin stu05
useradd stu06; echo `head -200 /dev/urandom | cksum | cut -c 1-8` | passwd --stdin stu06
useradd stu07; echo `head -200 /dev/urandom | cksum | cut -c 1-8` | passwd --stdin stu07
useradd stu08; echo `head -200 /dev/urandom | cksum | cut -c 1-8` | passwd --stdin stu08
useradd stu09; echo `head -200 /dev/urandom | cksum | cut -c 1-8` | passwd --stdin stu09
useradd stu10; echo `head -200 /dev/urandom | cksum | cut -c 1-8` | passwd --stdin stu10
[root@oldboy ~]# echo stu{01..10} | xargs -n 1 | awk '{print "useradd "$0"; echo `head -200 /dev/urandom | cksum | cut -c 1-8` | passwd --stdin "$0'} | bash	### 但是不知道每个用户的登录密码
Changing password for user stu01.
passwd: all authentication tokens updated successfully.
Changing password for user stu02.
passwd: all authentication tokens updated successfully.
Changing password for user stu03.
passwd: all authentication tokens updated successfully.
Changing password for user stu04.
passwd: all authentication tokens updated successfully.
Changing password for user stu05.
passwd: all authentication tokens updated successfully.
Changing password for user stu06.
passwd: all authentication tokens updated successfully.
Changing password for user stu07.
passwd: all authentication tokens updated successfully.
Changing password for user stu08.
passwd: all authentication tokens updated successfully.
Changing password for user stu09.
passwd: all authentication tokens updated successfully.
Changing password for user stu10.
passwd: all authentication tokens updated successfully.
[root@oldboy ~]# echo stu{01..10} | xargs -n 1 | awk '{print "useradd "$0"; userpasswd=`head -1 /dev/urandom | cksum | cut -c 1-8`; echo ${userpasswd} >> passwd.log; echo ${userpasswd} | passwd --stdin "$0 '}
useradd stu01; userpasswd=`head -1 /dev/urandom | cksum | cut -c 1-8`; echo ${userpasswd} >> passwd.log; echo ${userpasswd} | passwd --stdin stu01
useradd stu02; userpasswd=`head -1 /dev/urandom | cksum | cut -c 1-8`; echo ${userpasswd} >> passwd.log; echo ${userpasswd} | passwd --stdin stu02
useradd stu03; userpasswd=`head -1 /dev/urandom | cksum | cut -c 1-8`; echo ${userpasswd} >> passwd.log; echo ${userpasswd} | passwd --stdin stu03
useradd stu04; userpasswd=`head -1 /dev/urandom | cksum | cut -c 1-8`; echo ${userpasswd} >> passwd.log; echo ${userpasswd} | passwd --stdin stu04
useradd stu05; userpasswd=`head -1 /dev/urandom | cksum | cut -c 1-8`; echo ${userpasswd} >> passwd.log; echo ${userpasswd} | passwd --stdin stu05
useradd stu06; userpasswd=`head -1 /dev/urandom | cksum | cut -c 1-8`; echo ${userpasswd} >> passwd.log; echo ${userpasswd} | passwd --stdin stu06
useradd stu07; userpasswd=`head -1 /dev/urandom | cksum | cut -c 1-8`; echo ${userpasswd} >> passwd.log; echo ${userpasswd} | passwd --stdin stu07
useradd stu08; userpasswd=`head -1 /dev/urandom | cksum | cut -c 1-8`; echo ${userpasswd} >> passwd.log; echo ${userpasswd} | passwd --stdin stu08
useradd stu09; userpasswd=`head -1 /dev/urandom | cksum | cut -c 1-8`; echo ${userpasswd} >> passwd.log; echo ${userpasswd} | passwd --stdin stu09
useradd stu10; userpasswd=`head -1 /dev/urandom | cksum | cut -c 1-8`; echo ${userpasswd} >> passwd.log; echo ${userpasswd} | passwd --stdin stu10
[root@oldboy ~]# echo stu{01..10} | xargs -n 1 | awk '{print "useradd "$0"; userpasswd=`head -1 /dev/urandom | cksum | cut -c 1-8`; echo ${userpasswd} >> passwd.log; echo ${userpasswd} | passwd --stdin "$0 '} | bash	###将密码保存在当前目录的passwd.log中,可根据里面的密码进行登录
Changing password for user stu01.
passwd: all authentication tokens updated successfully.
Changing password for user stu02.
passwd: all authentication tokens updated successfully.
Changing password for user stu03.
passwd: all authentication tokens updated successfully.
Changing password for user stu04.
passwd: all authentication tokens updated successfully.
Changing password for user stu05.
passwd: all authentication tokens updated successfully.
Changing password for user stu06.
passwd: all authentication tokens updated successfully.
Changing password for user stu07.
passwd: all authentication tokens updated successfully.
Changing password for user stu08.
passwd: all authentication tokens updated successfully.
Changing password for user stu09.
passwd: all authentication tokens updated successfully.
Changing password for user stu10.
passwd: all authentication tokens updated successfully.
[root@oldboy ~]# cat passwd.log		# 密码信息
40814279
18955776
52266806
13477959
32334090
22175853
58152844
21678445
13336223
25331082


另请参考:linux实战考试题:批量创建用户和密码-看看你会么?
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: