编写脚本批量添加、删除用户
2013-06-27 00:24
561 查看
编写脚本批量添加、删除用户环境要求:
编写相应脚本用来批量添加用户;
要求提供交互功能,当管理员执行该脚本时,可以根据提示指定需添加的用户数量(少于100)、用户名前缀、并能够设置这些用户账户的失效时间,初始密码;
用户名编号统一使用两位数,如使用”01”、”02”、”03”的形式,而不是”1”、”2”、”3”的形式。
并编写对应的批量删除用户脚本,要能够通过命令行参数指定用户名前缀,执行脚本后删除所有使用了该前缀的用户账户,但要防止删除root用户。
批量添加用户脚本:
1、新建批量添加用户脚本useradd.sh截图如下:
![](http://img1.51cto.com/attachment/201306/001844272.png)
内容如下:#!/bin/bash
read-p"input nu <1-99>:"nu
read-p"input name:"name
read-p"input date <YYYY-MM-DD>:" date
read-p"input password:"password
a=1
if[ $nu -lt 100 ]
then
while[ $a -le $nu ]
do
if[ $a -lt 10 ]
then
useradd-e $date "$name"0"$a"
echo "$passwd" | --stdin "$name"0"$a"&> /dev/null
else
useradd-e $date "$name""$a"
echo "password" | passwd --stdin "$name""$a"&> /dev/null
fi
a=`expr $a + 1`
done
fi
2、给脚本useradd.sh增加x权限
![](http://img1.51cto.com/attachment/201306/001857713.png)
3、执行脚本useradd.sh,添加以hello和www为前缀的用户:4、验证结果:查看/etc/passwd文件是否添了新用户:
![](http://img1.51cto.com/attachment/201306/001914509.png)
tail -16/etc/passwd
![](http://img1.51cto.com/attachment/201306/001927166.png)
批量删除用户:
1、批量删除用户脚本userdel.sh截图如下:
![](http://img1.51cto.com/attachment/201306/001958733.png)
内容如下:
#!/bin/bash
if [ $# -le 0 ]
then
echo"Error:The prefix of users has not be specified."
echo"usage:$0nameprefix"
exit1
fi
tar jcvf /root/usrs.cnf.tar.gz /etc/passwd /etc/shadow /etc/group &> /dev/null
usersdel=`grep "$1" /etc/passwd | cut -d ":" -f 1 | grep -v "root"`
for u in $usersdel
do
userdel -r $u &> /dev/null
done
2、给脚本userdel.sh增加x权限:
![](http://img1.51cto.com/attachment/201306/002008700.png)
3、执行脚本userdel.sh,删除上边新建用户。如userdel.sh hello 或userdel.sh www
![](http://img1.51cto.com/attachment/201306/002020862.png)
4、验证结果:查看/etc/passwd文件中已删除的用户是否还存在。发现只剩系统服务用户。
![](http://img1.51cto.com/attachment/201306/002035517.png)
总结:批量新建用户,在实际生产环境中有很多种方法,不只for语句一种添加方式。
本文出自 “听闻” 博客,请务必保留此出处http://wenzhongxiang.blog.51cto.com/6370734/1230407
编写相应脚本用来批量添加用户;
要求提供交互功能,当管理员执行该脚本时,可以根据提示指定需添加的用户数量(少于100)、用户名前缀、并能够设置这些用户账户的失效时间,初始密码;
用户名编号统一使用两位数,如使用”01”、”02”、”03”的形式,而不是”1”、”2”、”3”的形式。
并编写对应的批量删除用户脚本,要能够通过命令行参数指定用户名前缀,执行脚本后删除所有使用了该前缀的用户账户,但要防止删除root用户。
批量添加用户脚本:
1、新建批量添加用户脚本useradd.sh截图如下:
![](http://img1.51cto.com/attachment/201306/001844272.png)
内容如下:#!/bin/bash
read-p"input nu <1-99>:"nu
read-p"input name:"name
read-p"input date <YYYY-MM-DD>:" date
read-p"input password:"password
a=1
if[ $nu -lt 100 ]
then
while[ $a -le $nu ]
do
if[ $a -lt 10 ]
then
useradd-e $date "$name"0"$a"
echo "$passwd" | --stdin "$name"0"$a"&> /dev/null
else
useradd-e $date "$name""$a"
echo "password" | passwd --stdin "$name""$a"&> /dev/null
fi
a=`expr $a + 1`
done
fi
2、给脚本useradd.sh增加x权限
![](http://img1.51cto.com/attachment/201306/001857713.png)
3、执行脚本useradd.sh,添加以hello和www为前缀的用户:4、验证结果:查看/etc/passwd文件是否添了新用户:
![](http://img1.51cto.com/attachment/201306/001914509.png)
tail -16/etc/passwd
![](http://img1.51cto.com/attachment/201306/001927166.png)
批量删除用户:
1、批量删除用户脚本userdel.sh截图如下:
![](http://img1.51cto.com/attachment/201306/001958733.png)
内容如下:
#!/bin/bash
if [ $# -le 0 ]
then
echo"Error:The prefix of users has not be specified."
echo"usage:$0nameprefix"
exit1
fi
tar jcvf /root/usrs.cnf.tar.gz /etc/passwd /etc/shadow /etc/group &> /dev/null
usersdel=`grep "$1" /etc/passwd | cut -d ":" -f 1 | grep -v "root"`
for u in $usersdel
do
userdel -r $u &> /dev/null
done
2、给脚本userdel.sh增加x权限:
![](http://img1.51cto.com/attachment/201306/002008700.png)
3、执行脚本userdel.sh,删除上边新建用户。如userdel.sh hello 或userdel.sh www
![](http://img1.51cto.com/attachment/201306/002020862.png)
4、验证结果:查看/etc/passwd文件中已删除的用户是否还存在。发现只剩系统服务用户。
![](http://img1.51cto.com/attachment/201306/002035517.png)
总结:批量新建用户,在实际生产环境中有很多种方法,不只for语句一种添加方式。
本文出自 “听闻” 博客,请务必保留此出处http://wenzhongxiang.blog.51cto.com/6370734/1230407
相关文章推荐
- 批量添加删除用户脚本
- <linux小脚本>case实现批量添加/删除用户
- shell编程(十)---- 批量添加和删除用户的脚本
- shell脚本-批量添加或删除用户
- 批量添加删除用户脚本
- <linux小脚本>批量添加/删除用户
- SHELL 脚本批量添加删除用户
- 批量 添加/删除 用户脚本
- 脚本批量添加和批量删除用户
- Linux中用脚本实现批量添加和删除普通用户
- sell脚本应用:批量添加(删除)用户
- 批量添加用户和批量删除用户脚本
- linux 批量添加,删除用户脚本范例
- shell 脚本实战笔记(9)--linux自动批量添加用户
- 【shell脚本练习】批量添加用户和设置密码
- perl脚本之------向/etc/aliases邮件组添加删除用户
- 实习笔记----脚本: SVN批量添加与删除
- 脚本实现批量添加用户
- 批量添加删除用户(SHELL)
- Linux下批量添加/删除用户