批量创建特殊要求用户

111 阅读1分钟

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

一、代码

[ -d /oldboy ] || mkdir /oldboy
cd /oldboy
echo $(pwd) && \
source /etc/init.d/functions

for i in oldboy{01..10}
do
useradd $i &>/dev/null
if [ $? -eq 0 ]
then
action "$i" /bin/true
ps=$(uuidgen|tr '0-9' 'a-z'|cut -c 1-10)
echo $ps | passwd --stdin $i &>/dev/null
echo $i $ps >>/tmp/passwd.txt
#echo $(uuidgen|tr '0-9' 'a-z'|cut -c 1-10 )|passwd --stdin $i &>/dev/null
#echo $i $(uuidgen|tr '0-9' 'a-z'|cut -c 1-10) >>/tmp/passwd.txt
else
action "$i" /bin/false
fi
done

二、代码解析

1、functions

source /etc/init.d/functions #使用内核自带函数

配合action 使用有如下效果

source /etc/init.d/functions

action test1 /bin/true
action test2 /bin/false

2、/dev/null作用 我们一般会把/dev/null当成一个垃圾站,不要的东西丢进去。比如来清除文件中的内容。

3、配置密码

echo "password" |passwd --stdin user