看我如何利用Shell编写vsftpd管理软件
今天弄服务器的时候无意间用到此脚本,觉得当时写的虽然不咋样,但还是有点借鉴意义,就拿出来给大家看看。希望大家喜欢
先看看演示视频
高清版下载:
OK然后我们来看看脚本的实现过程,时间有点久了当时的想法可能现在有点想不起来了,解释的不对的地方望见谅
脚本分为7个部分,这里我只做简单的解释
第一部分
脚本开始就利用awk提取日志中的用户名字段保存到tmp目录下的ftplog-userlist.txt文件中,方便后面查询
然后开始信息的展示,提供选择功能通过read函数读取用户的选择
第二部分 Show ftp Database
>通过 sed读取用户名
>通过sed读取用户总数
第三部分Ftp-log-Manager
>通过 cat读取ftplog-userlist.txt列表展示日志列表
>通过 cat读取ftplog-userlist.txt日志总数并展示,如果为0则打印Today, no one has logged an FTP server
>如果按3则通过read读取用户输入的用户名然后通过awk切割日志并展示
第四部分Add ftp Users
>通过 read读取用户输入,然后写文件,最后重启服务
第五部分Del ftp Users
>通过 read读取用户输入,然后写文件,最后重启服务
> rm 删除用户目录和配置信息
第六部分Chanage FTP user password
>通过 read读取用户输入,然后stty $(cat /root/.stty)隐藏用户输入,然后通过sed 替换配置文件
>最后dbload重载虚拟用户数据库,重启服务
第七部分EXIT
> 删除/tmp/ftplog-userlist.txt并退出脚本
脚本代码:
#!/bin/bash # Ftp-Manager #Vsftpd-Manager Can be quickly vsftpd virtual user configuration management. # #---------------------------------------------------------------------------- # Current Version = 1.2.2 # Dated: 2013/11/12 # #including: 1. Add / remove virtual users. # 2. Quickly update virtual user password # 3. See what users who have logged FTP-Server. # 4. View log information for the specified user #---------------------------------------------------------------------------- #Ftp-Server address:-----> ftp://ftp.cdivtc.com/ #---------------------------------------------------------------------------- clear cat -n /var/log/xferlog | awk ‘{print $15}‘ | cut -d "@" -f1 | sort -u > /tmp/ftplog-userlist.txt if [ $UID -ne 0 ] then echo -e "\033[0;46Please run this script to root users\033[0m" else echo -e "\033[0;38m FTP-Manager\033[0m" echo -e "\033[0;31m=====================Menu========================\033[0m" echo -e "\033[0;33mCurrent Version 1.2.2\033[0m" echo -e "\033[0;33mDated 2013/11/12\033[0m" echo -e "\033[0;31m-------------------------------------------------\033[0m" echo -e "\033[0;36m1)View ftp database\033[0m" echo -e "\033[0;36m2)View log user list\033[0m" echo -e "\033[0;36m3)View log information for specified user\033[0m" echo -e "\033[0;36m4)Add user\033[0m" echo -e "\033[0;36m5)Del user\033[0m" echo -e "\033[0;36m6)Change password\033[0m" echo -e "\033[0;36m0)Exit\033[0m" echo -e "\033[0;31m=================================================\033[0m" echo -ne "\033[0;35mEnter you chose[0-6]: \033[0m" read Num if [ "${Num}" -lt 0 -o "${Num}" -gt 6 ] then echo "Input error, no such option,This is not between 0-6." #========================================Show Ftp Database========================================================== else if [ "${Num}" == "1" ] then echo -e "\033[0;31m====Users Information====\033[0m" sed ‘2~2d‘ /etc/vsftpd/Virtualuser.txt echo -e "\033[0;31m======Users Number=======\033[0m" UserNumber=`sed ‘2~2d‘ /etc/vsftpd/Virtualuser.txt | wc -l` echo -e "\033[0;35mUsers Number: \033[0m\033[0;33m$UserNumber\033[0m" read -p "Press any key to return to main menu" $0 #========================================FTP-Log-Manager================================================== elif [ "${Num}" == "2" ] then echo -e "\033[0;31m====Users Information====\033[0m" cat /tmp/ftplog-userlist.txt echo -e "\033[0;31m====Users Number=========\033[0m" userlognumber=`cat /tmp/ftplog-userlist.txt | wc -l` if [ -s "${userlognumber}" ] then echo -e "\033[0;35mToday, no one has logged an FTP server\033[0m" else echo -e "\033[0;35mUsers Number: \033[0m\033[0;33m$userlognumber\033[0m" read -p "Press any key to return to main menu" $0 fi elif [ "${Num}" == "3" ] then echo -ne "\033[0;33mPlease enter the user name you want to view: \033[0m" read user grep "${user}" /tmp/ftplog-userlist.txt if [ $? -ne 0 ] then echo -ne "\033[0;31mWithout this user,The user is not logged in the FTP server. Verify that the user input the correct.\033[0m" echo "" read -p "Press any key to return to main menu." $0 else cat /var/log/xferlog | awk ‘{print $1 " | " $2 " | " $4 " |" $7 "\t|" $12 "\t|" $14 "\t|"$9}‘ | grep $user | more read -p "Press any key to return to main menu." $0 fi #========================================Add Ftp Users========================================================== elif [ "${Num}" == "4" ] then echo -ne "\033[0;33mPlease enter your username: \033[0m" read username echo -ne "\033[0;33mPlease enter your password: \033[0m`stty -echo`" read password echo "" echo -ne "\033[0;33mPlease input again: \033[0m`stty -echo`" read password stty $(cat /root/.stty) echo $username >>/etc/vsftpd/Virtualuser.txt echo $password >>/etc/vsftpd/Virtualuser.txt db_load -T -t hash -f /etc/vsftpd/Virtualuser.txt /etc/vsftpd/Virtualuser.db echo "write_enable=yes" > /etc/vsftpd/Virtualuserdir/$username echo "anon_mkdir_write_enable=yes" >> /etc/vsftpd/Virtualuserdir/$username echo "anon_other_write_enable=yes" >> /etc/vsftpd/Virtualuserdir/$username echo "anon_upload_enable=yes" >> /etc/vsftpd/Virtualuserdir/$username echo "local_root=/var/ftp/Users/$username" >> /etc/vsftpd/Virtualuserdir/$username mkdir /var/ftp/Users/$username -pv | 2> /dev/null chown root.ftp /var/ftp/Users/$username chmod g+s /var/ftp/Users/$username chmod 774 /var/ftp/Users/$username echo "" service vsftpd restart echo -e "\033[0;32mCreate user success.\033[0m" read -p "Press any key to return to main menu" $0 #========================================Del FTP users========================================================== elif [ "${Num}" == "5" ] then echo -ne "\033[0;33mPlease input you want to delete user: \033[0m" read delusername usernumber=`grep -n $delusername /etc/vsftpd/Virtualuser.txt | cut -d ":" -f1` sed -i ‘‘$usernumber‘d‘ /etc/vsftpd/Virtualuser.txt sed -i ‘‘$usernumber‘d‘ /etc/vsftpd/Virtualuser.txt rm -rf /etc/vsftpd/Virtualuserdir/$delusername rm -rf /var/ftp/Users/$delusername rm -rf /etc/vsftpd/Virtualuser.db service vsftpd restart db_load -T -t hash -f /etc/vsftpd/Virtualuser.txt /etc/vsftpd/Virtualuser.db service vsftpd restart echo -e "\033[0;32mDelete user success.\033[0m" read -p "Press any key to return to main menu" $0 #========================================Chanage FTP user password========================================================== elif [ "${Num}" == "6" ] then echo -ne "\033[0;33mPlease input you want to modify the user password: \033[0m" read updateuser passwdrows=$(( `grep -n $updateuser /etc/vsftpd/Virtualuser.txt | cut -d ":" -f1` + 1 )) echo -ne "\033[0;33mPlease enter a new password:\033[0m `stty -echo`" read updatepasswd echo "" echo -ne "\033[0;33mPlease input again:\033[0m `stty -echo`" read updatepasswd stty $(cat /root/.stty) sed -i ‘‘$passwdrows‘ ‘s/.*./‘‘$updatepasswd‘‘/‘‘ /etc/vsftpd/Virtualuser.txt rm -rf /etc/vsftpd/Virtualuser.db db_load -T -t hash -f /etc/vsftpd/Virtualuser.txt /etc/vsftpd/Virtualuser.db echo "" service vsftpd restart echo -e "\033[0;32mPassword updated successfully.\033[0m" read -p "Press any key to return to main menu" $0 #========================================option 0========================================================== # Exit shell script elif [ "${Num}" == "0" ] then rm -rf /tmp/ftplog-userlist.txt exit else $0 fi fi fi
大家喜欢就点个赞嘛,谢谢
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。