从文本文件创建批量帐户

时间:2011-11-18 01:25:47

标签: bash account creation

我有以下脚本,它从users.txt读取第一个和第二个字段,并使用它们生成用户名和密码,并为每一行创建帐户。问题是该脚本仅为前两行创建帐户而不为其余行

创建帐户

enter image description here

#!/bin/bash
FILE=/home/knoppix/users.txt
USERSH=/bin/bash


while IFS=":" read GECOS USRGRP ; do

groupadd -f $USRGRP



USERNM="u$(cat /dev/urandom| tr -dc '0-9' | fold -w 6| head -n 1)"
USERPW=$(cat /dev/urandom| tr -dc 'a-zA-Z0-9' | fold -w 6| head -n 1)


useradd $USERNM -p $USERPW -g $USRGRP -c "$GECOS,$USRGRP" -d $HOME/$USERNM -s $USERSH -m

ACCNT=$(grep $USRNM /etc/passwd)
echo "${tgrn}Account creation successful!${tr}"
echo "$ACCNT"
echo "Credentials"
echo "${tred}Username:${tr} $USERNM ${tred}Password:${tr} $USERPW"
echo
done < $FILE

2 个答案:

答案 0 :(得分:3)

#!/bin/bash

while IFS=: read GECOS USRGRP; do

    # your groupadd and useradd commands here

done < /home/knoppix/users.txt

答案 1 :(得分:0)

#!/bin/bash
for line in $file; do
# make the account

done
rm $file
touch $file