批量创建10个用户stu01-stu10

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


##方法1:

[[email protected] ~]# 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

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.

上述命令实际就是再拼N条下面的命令的组合,举一条命令stu01用户的过程拆解如下:

useradd stu01 ;

pass=$((RANDOM+10000000));

echo "$pass"|passwd --stdin stu01;

echo -e "stu01        `echo "$pass"`">>/tmp/oldboy.log

特别说明:如果用shell循环结构会更简单,之所以限制使用循环的目的是锻炼学生的基础命令运用

能力,学到现在还没学到SHELL循环课程呢


##方法2:

[[email protected] ~]# 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|chpasswd


##方法3:

[[email protected] ~]# 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/‘ | bash

Changing password for user stu21.

passwd: all authentication tokens updated successfully.

Changing password for user stu22.

passwd: all authentication tokens updated successfully.

Changing password for user stu23.

passwd: all authentication tokens updated successfully.

Changing password for user stu24.

passwd: all authentication tokens updated successfully.

Changing password for user stu25.

passwd: all authentication tokens updated successfully.

Changing password for user stu26.

passwd: all authentication tokens updated successfully.

Changing password for user stu27.

passwd: all authentication tokens updated successfully.

Changing password for user stu28.

passwd: all authentication tokens updated successfully.

Changing password for user stu29.

passwd: all authentication tokens updated successfully.

Changing password for user stu30.

passwd: all authentication tokens updated successfully.

功能: 创建10个用户 分别是 stu21-stu30 其密码是用随机数变量RANDOM生成,均保存至 userInfo.txt中,格式: username:passwd   这个写的不算好  如果有更好的一定要分享哦!  上面的随机数 我之前是用日期生成的,是不对的,因为有可能会有重复现象,所以我后来干脆用RANDOM**3取其前8位,可确保唯一性


##方法4:

[[email protected] ~]# mkdir /data

[[email protected] ~]# echo stu{01..10} |tr ‘ ‘ ‘\n‘|sed -rn ‘[email protected]^(.*)[email protected] \1 ; echo $RANDOM|md5sum|cut -c 1-8 >/data/\1;cat /data/\1|passwd --stdin \[email protected]‘|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.

时间: 2024-11-14 10:58:55

批量创建10个用户stu01-stu10的相关文章

批量创建10个系统用户,并且设置随机数密码

linux生成随机密码有很多方法,这里介绍两种,一种是内部系统变量($RANDOM),另一种是使用openssl. 内部系统变量($RANDOM)直接echo就可以获取到一组随机数: # echo $RANDOM 30468 获取8位随机数: #echo $RANDOM|md5sum |cut -c 1-8 393c839b 使用openssl命令获取随机数: # openssl rand -base64 8 qpIpWLYS6Yk= 获取8位随机数: # openssl rand -base6

批处理批量创建90个用户

rem create90.bat rem 用net user命令创建90用户 for /L %%i in (1,1,9) do net user k0%%i     /add /active:yes /expires:never /passwordchg:no /passwordreq:no /times:all /usercomment:"管理员" /workstations:* for /L %%i in (1,1,9) do net localgroup administrato

shell脚本,批量创建10个系统帐号并设置密码为随机8位字符串。

[[email protected] wyb]# cat user10.sh #!/bin/bash #批量创建10个系统帐号wangyb01-wangyb10并设置密码(密码为随机8位字符串). >user.list for user in `seq -w 10` do useradd wangyb$user password=`echo $RANDOM|md5sum|cut -c 1-8` echo $password|passwd wangyb$user --stdin echo wang

shell脚本,在指定目录下通过随机小写10个字母加固定字符串oldboy批量创建10个html文件。

[[email protected] wyb]# cat test10.sh #!/bin/bash #使用for循环在/test10目录下通过随机小写10个字母加固定字符串oldboy批量创建10个html文件 dir=/root/wyb/test10/ [ ! -d $dir ] && mkdir -p $dir for i in `seq 10` do touch $dir`echo $RANDOM|md5sum|cut -c 1-10`_oldboy.html done [[ema

批量创建100个用户

#!/bin/bash #描述:批量创建100个用户 #随机6位密码: `date | md5sum | cut -b 1-6` #随机6位密码: `cat /dev/urandom |head -1|md5sum | cut -b 1-6` i=1 pass=888888 while [ $i -le 100 ] do #判断所需添加用户是否存在 id $user 1>/dev/null 2&1 if [ $? -eq 1 ] then echo "用户不存在,允许添加"

批量创建10个系统帐号o01-zkg10并设置密码

批量创建10个系统帐号o01-zkg10并设置密码(密码为随机8位字符串)脚本如下: #!/bin/sh#批量创建用户名和密码#by zkg 2019-08-22. /etc/init.d/functions if [ $UID -ne 0 ];thenecho "create user need root privage"exitfi for user in echo zkg{01..10}docheck_user=grep "\b$user\b" /etc/pa

linux批量添加10个用户并将其随机密码和用户名写入文件

需求: 批量新建10个测试用户,并且让其密码随机,把用户名和密码写入一个文件,并有创建时间和创建者 #!/usr/bin/python # -*- coding: utf-8 -*- import os import random import time import io def deluser(nums): '''删除用户测试用户''' for i in range(nums): username = 'stu' + str(i) linux_cmd = 'userdel -r {usern

Linux系统编写shell脚本批量创建和删除用户

一.编写shell脚本批量添加用户 实现方法:判断用户是否存在,存在则返回错误提示,同时判断用户文件是否存在,不存在则退出 1.创建添加用户脚本 [[email protected] ~]# vim useradd.sh #!/bin/bashif [ $# -eq 0 ];then        echo "你没有输入任何文件!"        exit 1fi if [ ! -f $1 ];then        echo "输入有误!"        exit

批量创建10个系统帐号,并设置密码(密码为随机数,要求字符和数字等混合)

#!/bin/bashfor i in `seq 1 10`do if id user-$i > /dev/null;then read -ep "user-$i用户已存在是否删除(y/n)" aa if [ $aa = y ];then userdel -rf user-$i elif [ $aa = n ];then continue else "输入有误" fi else useradd user-$i if [ $? -eq 0 ];then echo