2014年12月2日 星期二

一次建立20個使用者帳號

利用 Bash / Shell Script 來一次完成建置 20 個使用者帳號,
且要給每個使用者加入不同的登入密碼,
並且強制要求第一次登入後,要修改使用者密碼。

設定群組:s100
建立帳號:s100a000 ~ s100a020
設置密碼:`date +%N` 來產生亂數

vi buildAccount.sh
# !/bin/bash
# 2014/12/02    Author:Kai-Lin Liao
# Create 20 user
# account:   s100a000 ~ s100a020,
# usergroup: s100

# Setting Parameter
GROUP=s100
USER_HEAD=s100a0

# Check root
if [ `whoami` != "root" ]
then
        echo "===== Permision Deny ====="
        echo "You need login root."
        echo
        exit 0
fi

# Create group
groupadd s100

# Create account 0~9
for i in `seq 0 9`
do
        USER_NAME=${USER_HEAD}0$i
        # Setting account password is ramdon
        PASSWD=`date +%N`
        echo "Creating User 0$i: $USER_NAME"
        echo "Set Password: $PASSWD"
        useradd -m -s /bin/bash -g $GROUP $USER_NAME
        echo $USER_NAME:$PASSWD | chpasswd
        chage -d 0 $USER_NAME
done

# Create account 10~20
for i in `seq 10 20`
do
        USER_NAME=${USER_HEAD}$i
        # Setting account password is ramdon
        PASSWD=`date +%N`
        echo "Creating User $i: $USER_NAME"
        echo "Set Password: $PASSWD"
        useradd -m -s /bin/bash -g $GROUP $USER_NAME
        echo $USER_NAME:$PASSWD | chpasswd
        chage -d 0 $USER_NAME
done



刪除剛剛建的群組:s100,及使用者s100a000 ~ s100a020

vi deleteAccount.sh
# !/bin/bash
# 2014/12/02    Author:Kai-Lin Liao
# Delete 20 user 
# account:   s100a000 ~ s100a020,

# Setting Parameter
GROUP=s100
USER_HEAD=s100a0

# Check root
if [ `whoami` != "root" ]
then
        echo "===== Permision Deny ====="
        echo "You need login root."
        echo
        exit 0
fi


# Create account 0~9
for i in `seq 0 9`
do
        USER_NAME=${USER_HEAD}0$i
        echo "Delete User 0$i: $USER_NAME"
        userdel -r $USER_NAME
done

# Create account 10~20
for i in `seq 10 20`
do
        USER_NAME=${USER_HEAD}$i
        echo "Delete User $i: $USER_NAME"
        userdel -r $USER_NAME
done

# Delete group
groupdel $GROUP

沒有留言:

張貼留言