README
shell环境下运行脚本,根据需求选择相应的功能。
List \t\t create the userlist 这一步是必须执行的,脚本会识别本地当前目录下的文件
Useradd \t\t useradd the user
Passwd \t\t set the password to userlist
Del \t\t deleate the user 删除用户之前必须要有需要删除用户的列表文件
Sudoers \t\t set the Sudoers
Exit \t\t quit the shell
#!/bin/bash
########################################
#Author:qingbo.song #
#Date:2015.11. #
#E-mail:qingbo.song@apicloud.com #
#Comment: the system useradd #
#Path: /opt/shell/Linux_user_set.sh #
#Version:V 2.0 #
######################################## userfile="./userlist.txt"
Option_Echo()
{
echo -e "
######################################################
List \t\t create the userlist
Useradd \t\t useradd the user
Passwd \t\t set the password to userlist
Del \t\t deleate the user
Sudoers \t\t set the Sudoers
Exit \t\t quit the shell
######################################################"
} Welcome()
{
while [[ == ]]; do
echo "===============Welcome to use Linux_user_set.sh!==============="
echo "The shell to add the system user,and set the password to it! Please chose the option to run the program!"
#显示命令提示信息,调用Option_Echo函数
Option_Echo
#获取用户输入的信息
echo -n ":"
read option
if [[ -n "${option}" ]]; then #判断用户输入的信息不为空
if [[ "${option}" = "List" || "${option}" = "Useradd" || "${option}" = "Passwd" || "${option}" = "Del" || "${option}" = "Sudoers" || "${option}" = "Exit" ]]; then
if [[ "${option}" = "List" ]]; then
#创建用户列表函数调用
UserList
break
elif [[ "${option}" = "Useradd" ]]; then
#创建系统用户函数调用
UserAdd
break
elif [[ "${option}" = "Passwd" ]]; then
#设置用户密码函数调用
Passwd
break
elif [[ "${option}" = "Del" ]]; then
#sudoers函数调用
DelUser
break
elif [[ "${option}" = "Sudoers" ]]; then
Sudoers
break
elif [[ "${option}" = "Exit" ]]; then
#rm -fr ${userfile} #删除userlist.txt文件
echo "The shell is shutdown!"
rm -fr ${userfile}
exit
else
echo "Please input the ture option,Thank you!"
fi
else
echo "Please input the ture option,Thank you!"
fi
else
echo "The option is not NULL!Please input the ture option,Thank you!"
fi
done
} #创建用户列表
UserList()
{
echo "You will create a file of the user list,it's the ${userfile}!The format:"
echo -e "user01\nuser02"
echo "Please input the \"exit\" to quit"
#初始化userlist.txt
cat /dev/null > ${userfile}
while [[ $? == ]]; do
echo -n "Please input the user name:"
read name
if [[ -n "${name}" ]]; then #判断用户输入的信息是否非空
if [[ "${name}" = "exit" || "${name}" = "EXIT" || "${name}" = "Exit" ]]; then
Welcome
else
#将需要创建的用户添加到userlist.txt文件中
echo ${name} >> ${userfile}
echo "The user name \"${name}\" to save the file of ${userfile}!"
fi
else
echo "The name is not NULL!Please input the Ture user name,Thank you!!!"
fi
done
} #创建系统用户
UserAdd()
{
cat ${userfile} |grep -v "#"|while read LINE
do
echo "Create the user of ${LINE}!"
#-d 创建用户登录开始目录;-m 家目录不存在自动创建;-n 不创建相同名字的组;
#mkdir /users
useradd -d /home/${LINE} -m -n ${LINE} >> /dev/null >&
done Welcome
} #设置用户密码
Passwd()
{
ip=`ip addr|grep eth0|tail -|awk '{print $2}'|awk -F "/" '{print $1}'` #centos6.5获取
name=`hostname`
wdfile="./userpwdfile_${ip}_${name}.txt"
#重置user_password_list.txt
cat /dev/null > ${wdfile}
cat ${userfile} |grep -v "#"|while read LINE
do
echo "Produce the user of ${LINE} password:"
password=`</dev/urandom tr -dc '0123456789!@#$%{}<>=*&abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' | head -c24; echo ""`
echo "${LINE}:${password}" >> ${wdfile}
echo "${LINE}&&password save to the file ${wdfile}"
done
chpasswd < ${wdfile}
if [[ $? -eq ]]; then
echo "The user password to set success!"
else
echo "The user password to set ERROR!Please try again for yourself!"
fi
#返回Welcome界面
Welcome
} #批量删除user
DelUser()
{
echo "Now deleate the ${userfile} list of users:"
cat ${userfile} |grep -v "#"|while read LINE
do
echo "Deleate the user of ${LINE}!"
#-d 创建用户登录开始目录;-m 家目录不存在自动创建;-n 不创建相同名字的组;
userdel -r ${LINE} >> /dev/null >&
done Welcome
} #批量添加sudoers,请谨慎使用该功能
Sudoers()
{
echo "Now add the sudoers to /etc/sudoers:"
if [[ -f ${userfile} ]]; then
cat ${userfile}|grep -v "#"|while read LINE
do
echo "${LINE} ALL=(ALL) ALL" >> /etc/sudoers
echo "${LINE} 用户成功添加到sudoers中"
done
else
echo "${userfile}文件不存在!"
exit
fi Welcome
} #main 函数
Welcome
批量管理Linux系统用户