易网时代-编程资源站
Welcome
微信登录
首页
/
操作系统
/
Linux
/
Linux批量 添加/删除 用户脚本
说明:蓝色=命令名称 浅绿=命令参数 浅蓝=选项 紫色=目录 系统环境:CentOS 6.2 i686 添加脚本:
#!/bin/bash
PWD=users.txt
PASSWD=123456
while getopts "d:p:h" opt
do
case $opt in
h)
cat << EOF
Useing Option:
-d a txt for users,default ./users.txt
-p users password, default 123456
-h help infomation
EOF
exit;
;;
d)
PWD=$OPTARG
;;
p)
PASSWD=$OPTARG
;;
*)
echo "Unkonw argument! Please use -h for help."
exit;
esac
done
if test -f "$PWD"
then
for i in `cat $PWD`
do
USER=`awk -F ":" "{if($1~/peixun_""$i""/) print $1}" /etc/passwd`
#if ["$USER" != "peixun_$1"];
#echo "`awk -F ":" "{if($1~/peixun_""$i""/) print $1}" /etc/passwd`"
if test -z "`awk -F ":" "{if($1~/peixun_""$i""/) print $1}" /etc/passwd`"
then
useradd -m peixun_$i
echo "$PASSWD"|passwd
--stdin peixun_$i &> /dev/null
echo "USER:peixun_$i PASSWD:$PASSWD"
echo "`date +"%Y-%m-%D %H%M%S"` USER:peixun_$i PASSWD:$PASSWD" >> /var/log/adduser.log
else
echo "User peixun_$i is exists"
fi
done
else
echo "not found $PWD"
exit;
fi
删除脚本:
#!/bin/bash
del_users_only(){
for i in $USERS
do
userdel $i
echo "USER:$i"
echo "`date +"%Y-%m-%d %H:%M:%S"` USER:$i" >> /var/log/deluser.log
done
}
del_users_home(){
for i in $USERS
do
HOME=`awk -F ":" "{ if($1~/test/) print $6}" /etc/passwd`
userdel -r $i
echo "USER:$i del HOME:$HOME"
echo "`date +"%Y-%m-%d %H:%M:%S"` USER:$i del HOME:$HOME" >> /var/log/deluser.log
done
}
select_y_n_2(){
echo "Do you want to del HOME?(y/n)"
read yn2
case $yn2 in
n|N)
del_users_only;
;;
y|Y)
del_users_home;
;;
*)
echo "Please enter y or n"
select_y_n_2;
esac
}
select_y_n_1(){
echo "del USERS:$USERS"
echo "Do you want to del USERS?(y/n)"
read yn1
case $yn1 in
n|N)
exit;
;;
""|y|Y)
select_y_n_2;
;;
*)
echo "Please enter y or n"
select_y_n_1;
esac
}
if test -z "$1" ||[ "$1" = "-" ]||[ "${1:0:1}" != "-" ]
then
echo "Unkonw argument! Please use -h for help."
exit;
else
while getopts "u:h" opt
do
case $opt in
""|h)
cat << EOF
Useing Option:
-u keywork for users
-h help infomation
EOF
exit;
;;
u)
WORD=$OPTARG
;;
?)
echo "Unkonw argument! Please use -h for help."
exit;
esac
done
USERS=`awk -F ":" "$3>=500&&$1~/""$WORD""/ {print $1}" /etc/passwd`
if test -z "$USERS"
then
echo "Users are not found!"
exit;
else
select_y_n_1
fi
fi
Linux中的super pi(bc 命令总结)Linux去除重复列(awk之数组妙用)相关资讯 Linux脚本
获取Linux服务器基本信息脚本 (02月17日)
Linux 批量拷贝数据脚本 (03/05/2015 10:47:12)
简单的Linux脚本程序 (11/10/2013 08:45:07)
Linux定时任务中用脚本代替可执行 (06/06/2015 06:37:39)
Linux下的flashback脚本 (05/21/2014 08:25:05)
Linux下记录所有用户操作的脚本 (07/29/2013 08:33:39)
本文评论 查看全部评论 (0)
表情: 姓名:
匿名
字数
版权所有©石家庄振强科技有限公司2024
冀ICP备08103738号-5
网站地图