首页 > 技术文章 > shell实现增加删除Linux系统用户脚本(密码为随机)

songqingbo 2016-01-04 13:16 原文

README
  shell环境下运行脚本,根据需求选择相应的功能。
1 List    \t\t  create the userlist 这一步是必须执行的,脚本会识别本地当前目录下的文件
2 Useradd \t\t  useradd the user 
3 Passwd  \t\t  set the password to userlist
4 Del        \t\t  deleate the user 删除用户之前必须要有需要删除用户的列表文件
5 Sudoers \t\t  set the Sudoers
6 Exit    \t\t  quit the shell

 

  1 #!/bin/bash
  2 ########################################
  3 #Author:qingbo.song                    #
  4 #Date:2015.11.18                       #
  5 #E-mail:qingbo.song@apicloud.com       #
  6 #Comment: the system useradd           #
  7 #Path: /opt/shell/Linux_user_set.sh    #
  8 #Version:V 2.0                         #
  9 ########################################
 10 
 11 userfile="./userlist.txt"
 12 Option_Echo()
 13 {
 14 echo -e "
 15 ######################################################
 16 List    \t\t  create the userlist 
 17 Useradd \t\t  useradd the user 
 18 Passwd  \t\t  set the password to userlist
 19 Del        \t\t  deleate the user
 20 Sudoers \t\t  set the Sudoers
 21 Exit    \t\t  quit the shell
 22 ######################################################"
 23 }
 24 
 25 
 26 Welcome()
 27 {
 28     while [[ 1 == 1 ]]; do
 29         echo "===============Welcome to use Linux_user_set.sh!==============="
 30         echo "The shell to add the system user,and set the password to it! Please chose the option to run the program!"
 31         #显示命令提示信息,调用Option_Echo函数
 32         Option_Echo
 33         #获取用户输入的信息
 34         echo -n ":"
 35         read option
 36         if [[ -n "${option}" ]]; then    #判断用户输入的信息不为空
 37             if [[ "${option}" = "List" || "${option}" = "Useradd" || "${option}" = "Passwd" || "${option}" = "Del" || "${option}" = "Sudoers" || "${option}" = "Exit" ]]; then
 38                 if [[ "${option}" = "List" ]]; then
 39                     #创建用户列表函数调用
 40                     UserList
 41                     break
 42                 elif [[ "${option}" = "Useradd" ]]; then
 43                     #创建系统用户函数调用
 44                     UserAdd
 45                     break
 46                 elif [[ "${option}" = "Passwd" ]]; then
 47                     #设置用户密码函数调用
 48                     Passwd
 49                     break
 50                 elif [[ "${option}" = "Del" ]]; then
 51                     #sudoers函数调用
 52                     DelUser
 53                     break
 54                 elif [[ "${option}" = "Sudoers" ]]; then
 55                     Sudoers
 56                     break
 57                 elif [[ "${option}" = "Exit" ]]; then
 58                     #rm -fr ${userfile}    #删除userlist.txt文件
 59                     echo "The shell is shutdown!"
 60                     rm -fr ${userfile}
 61                     exit 0
 62                 else
 63                     echo "Please input the ture option,Thank you!"
 64                 fi
 65             else
 66                 echo "Please input the ture option,Thank you!"            
 67             fi
 68         else
 69             echo "The option is not NULL!Please input the ture option,Thank you!"
 70         fi
 71     done  
 72 }
 73 
 74 #创建用户列表
 75 UserList()
 76 {
 77     echo "You will create a file of the user list,it's the ${userfile}!The format:"
 78     echo -e "user01\nuser02"
 79     echo "Please input the \"exit\" to quit"
 80     #初始化userlist.txt
 81     cat /dev/null > ${userfile}
 82     while [[ $? == 0 ]]; do
 83         echo -n "Please input the user name:"
 84         read name
 85         if [[ -n "${name}" ]]; then        #判断用户输入的信息是否非空
 86             if [[ "${name}" = "exit" || "${name}" = "EXIT" || "${name}" = "Exit" ]]; then
 87                 Welcome
 88             else
 89                 #将需要创建的用户添加到userlist.txt文件中
 90                 echo ${name} >> ${userfile}
 91                 echo "The user name \"${name}\" to save the file of ${userfile}!"
 92             fi
 93         else
 94             echo "The name is not NULL!Please input the Ture user name,Thank you!!!"
 95         fi
 96     done    
 97 }
 98 
 99 #创建系统用户
100 UserAdd()
101 {
102     cat ${userfile} |grep -v "#"|while read LINE
103     do
104         echo "Create the user of ${LINE}!"
105         #-d 创建用户登录开始目录;-m 家目录不存在自动创建;-n 不创建相同名字的组;
106         #mkdir /users
107         useradd -d /home/${LINE} -m -n ${LINE} >> /dev/null 2>&1
108     done
109 
110     Welcome
111 }
112 
113 #设置用户密码
114 Passwd()
115 {
116     ip=`ip addr|grep eth0|tail -1|awk '{print $2}'|awk -F "/" '{print $1}'`        #centos6.5获取
117     name=`hostname`
118     wdfile="./userpwdfile_${ip}_${name}.txt"
119     #重置user_password_list.txt
120     cat /dev/null > ${wdfile}
121     cat ${userfile} |grep -v "#"|while read LINE
122     do
123         echo "Produce the user of ${LINE} password:"
124         password=`</dev/urandom tr -dc '0123456789!@#$%{}<>=*&abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' | head -c24; echo ""`
125         echo "${LINE}:${password}" >> ${wdfile}
126         echo "${LINE}&&password save to the file ${wdfile}"
127     done
128     chpasswd < ${wdfile}
129     if [[ $? -eq 0 ]]; then
130         echo "The user password to set success!"
131     else
132         echo "The user password to set ERROR!Please try again for yourself!"
133     fi
134     #返回Welcome界面
135     Welcome
136 }
137 
138 #批量删除user
139 DelUser()
140 {
141     echo "Now deleate the ${userfile} list of users:"
142     cat ${userfile} |grep -v "#"|while read LINE
143     do
144         echo "Deleate the user of ${LINE}!"
145         #-d 创建用户登录开始目录;-m 家目录不存在自动创建;-n 不创建相同名字的组;
146         userdel -r ${LINE} >> /dev/null 2>&1
147     done
148 
149     Welcome
150 }
151 
152 #批量添加sudoers,请谨慎使用该功能
153 Sudoers()
154 {
155     echo "Now add the sudoers to /etc/sudoers:"
156     if [[ -f ${userfile} ]]; then
157         cat ${userfile}|grep -v "#"|while read LINE
158         do
159             echo "${LINE}    ALL=(ALL)     ALL" >> /etc/sudoers
160             echo "${LINE} 用户成功添加到sudoers中"
161         done
162     else
163         echo "${userfile}文件不存在!"
164         exit 1
165     fi    
166 
167     Welcome
168 }
169 
170 #main 函数
171 Welcome
批量管理Linux系统用户

 

推荐阅读