Java教程

利用bash_shell批量添加删除用户

本文主要是介绍利用bash_shell批量添加删除用户,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

程序使用bash脚本编写的,内部内置了一些日志,全部采用echo命令实现。

使用方法

  1. 将用户切换为root.
  2. 在脚本所在目录下创建usernames.txt文件并将要创建的用户名存放在该文件里,每个用户占用一行,例如要创建test1test2这两个账户,那么就在文件中存入:
test1
test2

此外usernames.txt这个文件名可以在脚本最上方的config中进行修改.

  1. 在命令行中输入bash scriptName.sh -create就可以创建在usernames.txt中存储的账号。
  2. 在命令行中输入bash scriptName.sh -delete就可以删除在usernames.txt中存储的账号.

其中-create-delete可以在脚本最上方的config中进行修改.

代码

centOS版本

# config
userNameFile="usernames.txt"
logStorageFile="log.txt"
create="-create"
delete="-delete"

# function code
addUser() {
    userName=$1
        if id -u $userName >/dev/null 2>&1; then
                data="Attempted to add an existing user: $userName"
                echo $data
                echo $data >> $logStorageFile
        else
                password=$(openssl rand -base64 6)
                useradd -m $userName
                echo "$userName:$password" | chpasswd
                echo "added a new user: $userName"
                data="userName: $userName password: $password"
                echo $data >> $logStorageFile
        fi
}

deleteUser() {
    userName=$1
        if id -u $userName >/dev/null 2>&1; then
                echo "deleting $userName"
                userdel -r $userName
                data="deleted a user: $userName"
                echo $data
                echo $data >> $logStorageFile
        else
                echo "user $userName does not exist!"
                data="Attempted to delete a user that did not exist: $userName"
                echo $data >> $logStorageFile
        fi
}

help() {
        echo "
        1. Check whether file $userNameFile is correctly configured.
                Example:
                testUser1
                testUser2

        2. Check that the options are correctly selected.
                Append '-create' behind the script name to add the user stored in file $userNameFile;
                Append '-delete' behind the script name to delete the user stored in file $userNameFile.
        "
}

# main code
if [ ! -f $userNameFile ]; then
        help
        exit 1
fi

userNames=$(cat $userNameFile)
op="${1}"

echo $(date) >> $logStorageFile
if [[ $op == $create || $op == $delete ]]
then
        echo "operate: $op" >> $logStorageFile
        for userName in $userNames
        do
                if [[ $op == $create ]]
                then
                        addUser $userName
                else
                        deleteUser $userName
                fi
        done
else
        echo $op
        help
fi

ubuntu版本

# config
userNameFile="usernames.txt"
logStorageFile="log.txt"
create="-create"
delete="-delete"

# function code
addUser() {
    	userName=$1
        if id -u $userName >/dev/null 2>&1; then
                data="Attempted to add an existing user: $userName"
                echo $data
                echo $data >> $logStorageFile
        else
                password=$(openssl rand -base64 6)
                useradd -m $userName
                echo "$userName:$password" | chpasswd
                echo "added a new user: $userName"
                data="userName: $userName password: $password"
                echo $data >> $logStorageFile
        fi
}

deleteUser() {
    	userName=$1
        if id -u $userName >/dev/null 2>&1; then
                echo "deleting $userName"
                userdel -r $userName
                data="deleted a user: $userName"
                echo $data
                echo $data >> $logStorageFile
        else
                echo "user $userName does not exist!"
                data="Attempted to delete a user that did not exist: $userName"
                echo $data >> $logStorageFile
        fi
}

help() {
        echo "
        1. Check whether file $userNameFile is correctly configured.
                Example:
                testUser1
                testUser2

        2. Check that the options are correctly selected.
                Append '-create' behind the script name to add the user stored in file $userNameFile;
                Append '-delete' behind the script name to delete the user stored in file $userNameFile.
        "
}

# main code
if [ ! -f $userNameFile ]; then
    	help
    	exit 1
fi

userNames=$(cat $userNameFile)
op="${1}"

echo $(date) >> $logStorageFile
if [[ $op == $create || $op == $delete ]]
then
        echo "operate: $op" >> $logStorageFile
        for userName in $userNames
        do
                if [[ $op == $create ]]
                then
                        addUser $userName
                else
                        deleteUser $userName
                fi
        done
else
        echo $op
        help
fi
这篇关于利用bash_shell批量添加删除用户的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!