星空网 > 软件开发 > 操作系统

批量创建10个用户stu01

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

 

 1 ##方法1: 2 [root@server tmp]# echo stu{01..10}|tr " " "\n"|sed -r 's#(.*)#useradd \1 ; pass=$((RANDOM+10000000)); echo "$pass"|passwd --stdin \1; echo -e "\1 \t `echo "$pass"`">>/tmp/oldboy.log#g'|bash 3 useradd: user 'stu01' already exists 4 Changing password for user stu01. 5 passwd: all authentication tokens updated successfully. 6 useradd: user 'stu02' already exists 7 Changing password for user stu02. 8 passwd: all authentication tokens updated successfully. 9 useradd: user 'stu03' already exists10 Changing password for user stu03.11 passwd: all authentication tokens updated successfully.12 useradd: user 'stu04' already exists13 Changing password for user stu04.14 passwd: all authentication tokens updated successfully.15 useradd: user 'stu05' already exists16 Changing password for user stu05.17 passwd: all authentication tokens updated successfully.18 useradd: user 'stu06' already exists19 Changing password for user stu06.20 passwd: all authentication tokens updated successfully.21 useradd: user 'stu07' already exists22 Changing password for user stu07.23 passwd: all authentication tokens updated successfully.24 useradd: user 'stu08' already exists25 Changing password for user stu08.26 passwd: all authentication tokens updated successfully.27 useradd: user 'stu09' already exists28 Changing password for user stu09.29 passwd: all authentication tokens updated successfully.30 useradd: user 'stu10' already exists31 Changing password for user stu10.32 passwd: all authentication tokens updated successfully.33 上述命令实际就是再拼N条下面的命令的组合,举一条命令stu01用户的过程拆解如下:34 useradd stu01 ; 35 pass=$((RANDOM+10000000)); 36 echo "$pass"|passwd --stdin stu01; 37 echo -e "stu01    `echo "$pass"`">>/tmp/oldboy.log38 特别说明:如果用shell循环结构会更简单,之所以限制使用循环的目的是锻炼学生的基础命令运用39 能力,学到现在还没学到SHELL循环课程呢40 ##方法2:41 [root@server tmp]# echo stu{11..12}|xargs -n1 useradd ;echo stu{11..12}:`cat /dev/urandom|tr -dc 0-9|fold -w8|head -1`|xargs -n1|tee -a pass.txt|chpasswd42 ##方法3:43 [root@server tmp]# echo stu{21..30} | tr ' ' '\n' | sed -e 's/^/useradd /' -e 's/\(stu[0-9]\{2\}\)$/\1 \&\& echo "\1:`echo $[$RANDOM**3] | cut -c1-8`" | tee -a userInfo.txt | cut -d: -f2 | passwd --stdin \1/' | bash44 Changing password for user stu21.45 passwd: all authentication tokens updated successfully.46 Changing password for user stu22.47 passwd: all authentication tokens updated successfully.48 Changing password for user stu23.49 passwd: all authentication tokens updated successfully.50 Changing password for user stu24.51 passwd: all authentication tokens updated successfully.52 Changing password for user stu25.53 passwd: all authentication tokens updated successfully.54 Changing password for user stu26.55 passwd: all authentication tokens updated successfully.56 Changing password for user stu27.57 passwd: all authentication tokens updated successfully.58 Changing password for user stu28.59 passwd: all authentication tokens updated successfully.60 Changing password for user stu29.61 passwd: all authentication tokens updated successfully.62 Changing password for user stu30.63 passwd: all authentication tokens updated successfully.64 功能: 创建10个用户 分别是 stu21-stu30 其密码是用随机数变量RANDOM生成,均保存至 userInfo.txt中,格式: username:passwd  这个写的不算好 如果有更好的一定要分享哦! 上面的随机数 我之前是用日期生成的,是不对的,因为有可能会有重复现象,所以我后来干脆用RANDOM**3取其前8位,可确保唯一性65 ##方法4:66 [root@server tmp]# echo stu{01..10} |tr ' ' '\n'|sed -rn 's@^(.*)$@useradd \1 ; echo $RANDOM|md5sum|cut -c 1-8 >/data/\1;cat /data/\1|passwd --stdin \1@gp'|bash67 useradd: user 'stu01' already exists68 Changing password for user stu01.69 passwd: all authentication tokens updated successfully.70 useradd: user 'stu02' already exists71 Changing password for user stu02.72 passwd: all authentication tokens updated successfully.73 useradd: user 'stu03' already exists74 Changing password for user stu03.75 passwd: all authentication tokens updated successfully.76 useradd: user 'stu04' already exists77 Changing password for user stu04.78 passwd: all authentication tokens updated successfully.79 useradd: user 'stu05' already exists80 Changing password for user stu05.81 passwd: all authentication tokens updated successfully.82 useradd: user 'stu06' already exists83 Changing password for user stu06.84 passwd: all authentication tokens updated successfully.85 useradd: user 'stu07' already exists86 Changing password for user stu07.87 passwd: all authentication tokens updated successfully.88 useradd: user 'stu08' already exists89 Changing password for user stu08.90 passwd: all authentication tokens updated successfully.91 useradd: user 'stu09' already exists92 Changing password for user stu09.93 passwd: all authentication tokens updated successfully.94 useradd: user 'stu10' already exists95 Changing password for user stu10.96 passwd: all authentication tokens updated successfully.

 




原标题:批量创建10个用户stu01

关键词:

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

去越南物流:https://www.goluckyvip.com/tag/105547.html
至越南物流:https://www.goluckyvip.com/tag/105548.html
物流至越南:https://www.goluckyvip.com/tag/105549.html
美元汇率:https://www.goluckyvip.com/tag/10555.html
临沂到越南物流:https://www.goluckyvip.com/tag/105551.html
广东越南快递:https://www.goluckyvip.com/tag/105552.html
长治婚庆女司仪和主持人:https://www.vstour.cn/a/366176.html
北京丰台区水上乐园哪家好玩?:https://www.vstour.cn/a/366177.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流