500、Linux获取随机密码

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实战考试题:批量创建用户和密码-看看你会么?



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值