今天看到51CTO正在搞Linux Shell 脚本大赛,特地将自己的×××作发出来,希望能够得到分享。
这里说一下这个脚本的功能,这个脚本主要用来实现VSFTPD服务器的基础管理,主要用于方便运维管理。代码不是很美,还请各位多多指教。
这个代码主要基于本地用户管理、有需求基于LDAP认证管理的朋友,可以联系我,LDAP认证的功能上将更强大了些。
管理界面:
代码部份:
read -p "请选择您要做的操作:" caozuo
case $caozuo in
0) read -p "请输入您要修改密码的用户名: " selectuser
while [ ! "$selectuser" ] || [ ! `more /etc/vsftpd/chroot_list | grep -w $selectuser` ]
do
read -p "对不起,您输入的用户名$selectuser不存在,请重新输入要查询的用户名,退出请按q键: " selectuser
if [ "$selectuser" ] && [ "$selectuser" == "q" ];then
exit
fi
done
passwd $selectuser
echo "记录: 管理员$adminroot于$timedate1修改用户$selectuser密码" >> /opt/vsftp/adminlog/"$adminroot""$timedate2"
read -p "您已经成功修改用户$selectuser的密码,按回车键继续操作" var
;;
1) read -p "请输入您要查询的用户名: " selectuser
while [ ! "$selectuser" ] || [ ! `more /etc/vsftpd/chroot_list | grep -w $selectuser` ]
do
echo "记录: 管理员$adminroot于$timedate1查询用户$selectuser" >> /opt/vsftp/adminlog/"$adminroot""$timedate2"
read -p "对不起,查询的用户名$selectuser不存在,请重新输入要查询的用户名,退出请按q键: " selectuser
if [ "$selectuser" ] && [ "$selectuser" == "q" ];then
exit
fi
done
echo "记录: 管理员$adminroot于$timedate1查询用户$selectuser" >> /opt/vsftp/adminlog/"$adminroot""$timedate2"
read -p "您查询的用户$selectuser为FTP用户,按回车键继续" var
;;
2) read -p "请输入您需要添加的用户名: " adduser
while [ ! "$adduser" ] || ( [ "$adduser" ] && [ `more /etc/vsftpd/chroot_list | grep -w $adduser` ] )
do
read -p "对不起,您的操作有误,用户名不能为空,或您添加的用户已经存在,请重新输入要查询的用户名,退出请按q键: " adduser
if [ "$adduser" ] && [ "$adduser" == "q" ];then
exit
fi
done
useradd $adduser -d /opt/vsftp/$adduser -s /sbin/nologin
chmod -R 777 /opt/vsftp/$adduser
echo $adduser@123 | passwd --stdin $adduser
echo "$adduser" >> /etc/vsftpd/chroot_list
setquota -u $adduser 512000 614400 0 0 /opt
cp /etc/vsftpd/vconf/moban /etc/vsftpd/vconf/$adduser
echo "记录: 管理员$adminroot于$timedate1添加用户$adduser" >> /opt/vsftp/adminlog/"$adminroot""$timedate2"
read -p "您添加用户$adduser成功,按回车键继续" var
;;
3) read -p "请输入您需要删除的用户名: " deluser
while [ ! "$deluser" ] || [ ! `more /etc/vsftpd/chroot_list | grep -w $deluser` ]
do
read -p "对不起,您输入的用户名$deluser不存在,请重新输入要删除的用户名,退出请按q键: " deluser
if [ "$deluser" ] && [ "$deluser" == "q" ];then
exit
fi
done
userdel $deluser
rm -rf /opt/vsftp/$deluser/.gnome2
rm -rf /opt/vsftp/$deluser/.bashrc
rm -rf /opt/vsftp/$deluser/.bash_profile
rm -rf /opt/vsftp/$deluser/.bash_logout
rm -rf /etc/vsftpd/vconf/$deluser
umount /opt/vsftp/$deluser
mv /opt/vsftp/$deluser /opt/vsftp/bak$deluser
rm -rf /opt/vsftp/$deluser
sed -i "/$deluser/d" /etc/vsftpd/chroot_list
sed -i "/$deluser/d" /etc/vsftpd/mountuser.sh
echo "记录: 管理员$adminroot于$timedate1删除用户$deluser" >> /opt/vsftp/adminlog/"$adminroot""$timedate2"
read -p "您已经成功删除用户$deluser,按回车键继续" var
;;
4) more /etc/vsftpd/chroot_list
read -p "以上为所有FTP用户,按回车键继续" var
;;
5) read -p "请输入您要查询限额的用户名: " selectquota
while [ ! "$selectquota" ] || [ ! `more /etc/vsftpd/chroot_list | grep -w $selectquota` ]
do
read -p "对不起,您输入的用户名$selectquota不存在,请重新输入要查询限额的用户名,退出请按q键: " selectquota
if [ "$selectquota" ] && [ $selectquota == q ];then
exit
fi
done
quota $selectquota | sed -n '3p' | echo "当前用户的软限制为: `awk '{print $3}'`KB"
quota $selectquota | sed -n '3p' | echo "当前用户的硬限制为: `awk '{print $4}'`KB"
echo "记录: 管理员$adminroot于$timedate1查询用户$selectquota磁盘限额信息" >> /opt/vsftp/adminlog/"$adminroot""$timedate2"
read -p "当前用户的磁盘限额如上,按回车键继续" var
;;
6) read -p "请输入您要修改限额的用户名: " revisequota
while [ ! "$revisequota" ] || [ ! `more /etc/vsftpd/chroot_list | grep -w $revisequota` ]
do
read -p "对不起,您输入的用户名$revisequota不存在,请重新输入要查询限额的用户名,退出请按q键: " revisequota
if [ "$revisequota" ] && [ $revisequota == q ];then
exit
fi
done
quota $revisequota | sed -n '3p' | echo "修改前用户的软限制为: `awk '{print $3}'`KB"
quota $revisequota | sed -n '3p' | echo "修改前用户的硬限制为: `awk '{print $4}'`KB"
read -p "请修改您要修改的用户软限制值,默认单位为KB: " quota1
while [ ! $quota1 ];
do
read -p "对不起,修改用户的软限制值不能为空,请重新输入软限制值,退出请按q键: " quota1
if [ $quota1 ];then
if [ $quota1 == q ];then
exit
fi
fi
done
read -p "请修改您要修改的用户硬限制值,默认单位为KB: " quota2
while [ ! $quota2 ] || [ $quota2 -lt $quota1 ];
do
read -p "对不起,修改用户的硬限制值不能为空,且硬限制的值不能小于软限制的值,请重新输入硬限制值,退出请按q键: " quota2
if [ "$quota2" ] && [ "$quota2" == "q" ];then
exit
fi
done
setquota -u $revisequota $quota1 $quota2 0 0 /opt
echo "记录: 管理员$adminroot于$timedate1修改用户$revisequota软限制为: $quota1 硬限制为: $quota2" >> /opt/vsftp/adminlog/"$adminroot""$timedate2"
quota $revisequota | sed -n '3p' | echo "修改后用户的软限制为: `awk '{print $3}'`KB"
quota $revisequota | sed -n '3p' | echo "修改后用户的硬限制为: `awk '{print $4}'`KB"
read -p "当前用户修改后磁盘限额如上,按回车键继续" var
;;
7) read -p "请输入您要查询的用户名: " selectuser
while [ ! $selectuser ] || [ ! `more /etc/vsftpd/chroot_list | grep -w $selectuser` ]
do
read -p "对不起,您查询的用户名$selectuser不存在,请重新输入,退出请按q键: " selectuser
if [ "$selectuser" ] && [ "$selectuser" == "q" ];then
exit
fi
done
read -p "请输入您要查询的日期(格式:2013-07-11 08:32:21): " datetime3
while [ ! $datetime3 ]
do
read -p "对不起您的输入有误,日期时间不能空,请重新输入: " datetime3
done
read -p "请输入您要查询的操作动作(DELETE、MKDIR、UPLOAD、RMDIR、DOWNLOAD、LOGIN): " opreate
while [ ! $opreate ]
do
read -p "对不起,操作动作不能为空,请重新输入: " opreate
done
while ( [ $opreate != 'MKDIR' ] && [ $opreate != 'UPLOAD' ] && [ $opreate != 'DELETE' ] && [ $opreate != 'LOGIN' ] && [ $opreate != 'RMDIR' ] && [ $o
preate != 'DOWNLOAD' ] )
do
read -p "对不起您输入的操作动作有误,请重新输入: " opreate
done
echo " 操作日期|操作时间|操作人|是否成功|操作动作|IP地址|操作内容" > /opt/vsftp/ftplog/"$timedate1""$selectuser""$opreate".xls
more /var/log/messages | grep $selectuser] | grep $opreate | grep $datetime3 | awk '{print $1"|"$2"|"$5"|"$6"|"$7"|"$9"|"$10 }' >> /opt/vsftp/ftplog/
"$timedate1""$selectuser""$opreate".xls
sed -i 's/://3' /opt/vsftp/ftplog/"$timedate1""$selectuser""$opreate".xls
sed -i 's/"//g' /opt/vsftp/ftplog/"$timedate1""$selectuser""$opreate".xls
sed -i 's/\[//' /opt/vsftp/ftplog/"$timedate1""$selectuser""$opreate".xls
sed -i 's/]//' /opt/vsftp/ftplog/"$timedate1""$selectuser""$opreate".xls
sed -i 's/,//g' /opt/vsftp/ftplog/"$timedate1""$selectuser""$opreate".xls
sed -i 's/UPLOAD/上传资料/g' /opt/vsftp/ftplog/"$timedate1""$selectuser""$opreate".xls
sed -i 's/DELETE/删除文件/g' /opt/vsftp/ftplog/"$timedate1""$selectuser""$opreate".xls
sed -i 's/LOGIN/用户登录/g' /opt/vsftp/ftplog/"$timedate1""$selectuser""$opreate".xls
sed -i 's/RMDIR/删除目录/g' /opt/vsftp/ftplog/"$timedate1""$selectuser""$opreate".xls
sed -i 's/MKDIR/新建目录/g' /opt/vsftp/ftplog/"$timedate1""$selectuser""$opreate".xls
sed -i 's/DOWNLOAD/下载资料/g' /opt/vsftp/ftplog/"$timedate1""$selectuser""$opreate".xls
sed -i 's/OK/成功/g' /opt/vsftp/ftplog/"$timedate1""$selectuser""$opreate".xls
sed -i 's/FAIL/失败/g' /opt/vsftp/ftplog/"$timedate1""$selectuser""$opreate".xls
echo "记录: 管理员$adminroot于$timedate1查询用户$selectuser的操作动作$opreate日志" >> /opt/vsftp/adminlog/"$adminroot""$timedate2"
read -p "直接查看请按a键,下载到Win查看请按b键: " abjian
if [ "$abjian" == "a" ];then
more /opt/vsftp/ftplog/"$timedate1""$selectuser""$opreate".xls
read -p "以上为指定用户的操作日志,按回车键继续操作" var
elif [ "$abjian" == "b" ];then
cd /opt/vsftp/ftplog
sz "$timedate1""$selectuser""$opreate".xls
read -p "指定用户的操作日志已经成功导出到Win,按回车键继续操作" var
fi
;;
8) read -p "请输入您要查询的日期(格式:2012-09-12): " riqi
while [ ! $riqi ]
do
read -p "对不起日期不能为空,请重新输入(格式:2012-09-12): " riqi
done
while [ `echo $riqi | grep -q '^[1-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]$' && echo ! aa || echo aa` ]
do
read -p "您输入的日期格式错误,请重新输入(格式:2012-09-12): " riqi
done
read -p "请输入您要查询的时间,(格式:23:12): " shijian
if [ ! $shijian ];then
echo " 操作日期|操作时间|操作人|是否成功|操作动作|IP地址|操作内容" > /opt/vsftp/ftplog/"$timedate1"查询"$riqi".xls
more /var/log/messages | grep $riqi | grep vsftpd | awk '{print $1"|"$2"|"$5"|"$6"|"$7"|"$9"|"$10 }' >> /opt/vsftp/ftplog/"$timedate1"查询"$riqi".xls
sed -i 's/://3' /opt/vsftp/ftplog/"$timedate1"查询"$riqi".xls
sed -i 's/"//g' /opt/vsftp/ftplog/"$timedate1"查询"$riqi".xls
sed -i 's/\[//' /opt/vsftp/ftplog/"$timedate1"查询"$riqi".xls
sed -i 's/]//' /opt/vsftp/ftplog/"$timedate1"查询"$riqi".xls
sed -i 's/,//g' /opt/vsftp/ftplog/"$timedate1"查询"$riqi".xls
sed -i 's/UPLOAD/上传资料/g' /opt/vsftp/ftplog/"$timedate1"查询"$riqi".xls
sed -i 's/DELETE/删除文件/g' /opt/vsftp/ftplog/"$timedate1"查询"$riqi".xls
sed -i 's/LOGIN/用户登录/g' /opt/vsftp/ftplog/"$timedate1"查询"$riqi".xls
sed -i 's/RMDIR/删除目录/g' /opt/vsftp/ftplog/"$timedate1"查询"$riqi".xls
sed -i 's/MKDIR/新建目录/g' /opt/vsftp/ftplog/"$timedate1"查询"$riqi".xls
sed -i 's/DOWNLOAD/下载资料/g' /opt/vsftp/ftplog/"$timedate1"查询"$riqi".xls
sed -i 's/OK/成功/g' /opt/vsftp/ftplog/"$timedate1"查询"$riqi".xls
sed -i 's/FAIL/失败/g' /opt/vsftp/ftplog/"$timedate1"查询"$riqi".xls
echo "记录: 管理员$adminroot于$timedate1查询日期为:$riqi的所有用户操作记录" >> /opt/vsftp/adminlog/"$adminroot""$timedate2"
read -p "直接查看请按a键,下载到Win查看请按b键: " abjian
if [ "$abjian" == "a" ];then
more /opt/vsftp/ftplog/"$timedate1"查询"$riqi".xls
read -p "以上为指定用户的操作日志,按回车键继续操作" var
elif [ "$abjian" == "b" ];then
cd /opt/vsftp/ftplog
sz "$timedate1"查询"$riqi".xls
read -p "指定用户的操作日志已经成功导出到Win,按回车键继续操作" var
fi
else
while [ `echo $shijian | grep -q '^[0-9][0-9]:[0-9][0-9]$' && echo ! aa || echo aa` ]
do
read -p "您输入的时间格式不正确,请重新输入(格式:23:12): " shijian
done
shi=`echo ${shijian:0:2}`
jian=`echo ${shijian:${#shijian}-2:${#shijian}}`
echo "操作日期|操作时间|操作人|是否成功|操作动作|IP地址|操作内容" > /opt/vsftp/ftplog/"$timedate1"查询"$riqi"-"$shi"."$jian"
more /var/log/messages | grep $riqi | grep $shijian | grep vsftpd | awk '{print $1"|"$2"|"$5"|"$6"|"$7"|"$9"|" $10 }' >> /opt/vsftp/ftplog/"$timedate
1"查询"$riqi"-"$shi"."$jian"
sed -i 's/://3' /opt/vsftp/ftplog/"$timedate1"查询"$riqi"-"$shi"."$jian"
sed -i 's/"//g' /opt/vsftp/ftplog/"$timedate1"查询"$riqi"-"$shi"."$jian"
sed -i 's/\[//' /opt/vsftp/ftplog/"$timedate1"查询"$riqi"-"$shi"."$jian"
sed -i 's/]//' /opt/vsftp/ftplog/"$timedate1"查询"$riqi"-"$shi"."$jian"
sed -i 's/,//g' /opt/vsftp/ftplog/"$timedate1"查询"$riqi"-"$shi"."$jian"
sed -i 's/UPLOAD/上传资料/g' /opt/vsftp/ftplog/"$timedate1"查询"$riqi"-"$shi"."$jian"
sed -i 's/DELETE/删除文件/g' /opt/vsftp/ftplog/"$timedate1"查询"$riqi"-"$shi"."$jian"
sed -i 's/LOGIN/用户登录/g' /opt/vsftp/ftplog/"$timedate1"查询"$riqi"-"$shi"."$jian"
sed -i 's/RMDIR/删除目录/g' /opt/vsftp/ftplog/"$timedate1"查询"$riqi"-"$shi"."$jian"
sed -i 's/MKDIR/新建目录/g' /opt/vsftp/ftplog/"$timedate1"查询"$riqi"-"$shi"."$jian"
sed -i 's/DOWNLOAD/下载资料/g' /opt/vsftp/ftplog/"$timedate1"查询"$riqi"-"$shi"."$jian"
sed -i 's/OK/成功/g' /opt/vsftp/ftplog/"$timedate1"查询"$riqi"-"$shi"."$jian"
sed -i 's/FAIL/失败/g' /opt/vsftp/ftplog/"$timedate1"查询"$riqi"-"$shi"."$jian"
echo "记录: 管理员$adminroot于$timedate1查询日期为:$riqi时间为:$shijian的所有用户操作记录" >> /opt/vsftp/adminlog/"$adminroot""$timedate2"
read -p "直接查看请按a键,下载到Win查看请按b键: " abjian
if [ "$abjian" == "a" ];then
more /opt/vsftp/ftplog/"$timedate1"查询"$riqi"-"$shi"."$jian".xls
read -p "以上为指定用户的操作日志,按回车键继续操作" var
elif [ "$abjian" == "b" ];then
cd /opt/vsftp/ftplog
sz "$timedate1"查询"$riqi"-"$shi"."$jian".xls
read -p "指定用户的操作日志已经成功导出到Win,按回车键继续操作" var
fi
fi
;;
9) read -p "请输入您要查询的用户名: " selectuser
while [ ! $selectuser ]
do
read -p "对不起,查询的用户名不能为空,请重新输入要查询的用户名: " selectuser
done
while [ ! `more /etc/vsftpd/chroot_list | grep -o $selectuser` ]
do
read -p "对不起,您查询的用户名不存在,请重新输入要查询的用户名: " selectuser
done
echo "操作日期|操作时间|操作人|是否成功|操作动作|IP地址|操作内容" > /opt/vsftp/ftplog/"$timedate1"查询"$selectuser".xls
more /var/log/messages | grep $selectuser | grep vsftpd | awk '{print $1 "|" $2 "|" $5 "|" $6 "|"$7 "|" $9 "|" $10 }' >> /opt/vsftp/ftplog/"$timedate
1"查询"$selectuser".xls
sed -i 's/://3' /opt/vsftp/ftplog/"$timedate1"查询"$selectuser".xls
sed -i 's/"//g' /opt/vsftp/ftplog/"$timedate1"查询"$selectuser".xls
sed -i 's/\[//' /opt/vsftp/ftplog/"$timedate1"查询"$selectuser".xls
sed -i 's/]//' /opt/vsftp/ftplog/"$timedate1"查询"$selectuser".xls
sed -i 's/,//g' /opt/vsftp/ftplog/"$timedate1"查询"$selectuser".xls
sed -i 's/UPLOAD/上传资料/g' /opt/vsftp/ftplog/"$timedate1"查询"$selectuser".xls
sed -i 's/DELETE/删除文件/g' /opt/vsftp/ftplog/"$timedate1"查询"$selectuser".xls
sed -i 's/LOGIN/用户登录/g' /opt/vsftp/ftplog/"$timedate1"查询"$selectuser".xls
sed -i 's/RMDIR/删除目录/g' /opt/vsftp/ftplog/"$timedate1"查询"$selectuser".xls
sed -i 's/MKDIR/新建目录/g' /opt/vsftp/ftplog/"$timedate1"查询"$selectuser".xls
sed -i 's/DOWNLOAD/下载资料/g' /opt/vsftp/ftplog/"$timedate1"查询"$selectuser".xls
sed -i 's/OK/成功/g' /opt/vsftp/ftplog/"$timedate1"查询"$selectuser".xls
sed -i 's/FAIL/失败/g' /opt/vsftp/ftplog/"$timedate1"查询"$selectuser".xls
echo "记录: 管理员$adminroot于$timedate1查询日期为:$selectuser的所有用户操作记录" >> /opt/vsftp/adminlog/"$adminroot""$timedate2"
read -p "直接查看请按a键,下载到Win查看请按b键: " abjian
if [ "$abjian" == "a" ];then
more /opt/vsftp/ftplog/"$timedate1"查询"$selectuser".xls
read -p "以上为指定用户的操作日志,按回车键继续操作" var
elif [ "$abjian" == "b" ];then
cd /opt/vsftp/ftplog
sz "$timedate1"查询"$selectuser".xls
read -p "指定用户的操作日志已经成功导出到Win,按回车键继续操作" var
fi
;;
10) ls /opt/vsftp/ftplog/ | more
echo "记录: 管理员$adminroot于$timedate1查看了所有导出的用户操作日志文件名" >> /opt/vsftp/adminlog/"$adminroot""$timedate2"
read -p "以上为所有已导出用户操作日志文件名,按回车键继续" var
;;
说明:
由于代码篇幅太大,这里贴出前10项功能代码,希望能够给有需求的朋友能起到一个抛砖引玉的作用。