基本需求:实现ftp账号到期自动关闭、延期、限额、限速操作,
系统搭建
系统:centos6.4
软件:pureftpd+mysql
软件版本:pure-ftpd-1.0.36
安装:
#yum -y install httpd mysql-devel mysql-server gcc
#tar xvf pure-ftpd-1.0.36.tar.gz
#cd pure-ftpd-1.0.36
#./configure--prefix=/usr/local/pure-ftpd/ --with-everything --with-altlog --with-puredb--with-ftpwho --with-mysql --with-throttling --with-ratios --with-quotas--with-virtualhosts --with-virtualchroot --with-sysquotas --with-cookie--with-rfc2640 --with-bonjour --with-welcomemsg --with-language=simplified-chinese--with-uploadscript
#make&make install
#mkdir /usr/local/pure-ftpd/etc/
#cpconfiguration-file/pure-ftpd.conf /usr/local/pure-ftpd/etc/
#cpconfiguration-file/pure-config.pl /usr/local/pure-ftpd/sbin/
#cppureftpd-mysql.conf /usr/local/pure-ftpd/etc/
#chmod755 /usr/local/pure-ftpd/sbin/pure-config.pl
部分截图
#ln-s /usr/local/pure-ftpd/bin/* /usr/local/bin/
#ln-s /usr/local/pure-ftpd/sbin/* /usr/local/sbin/
#ln-s /usr/local/pure-ftpd/share/man/man8/* /usr/local/share/man/man8/
#groupadd-g 10000 hzftp
#useradd-u 10000 -g 10000 -d /var/ftp -s /sbin/nologin hzftp
#passwd hzftp (123456)
#cp/etc/skel/.b* /mnt/hzftpdata/ 注:/mnt/hzftpdata 为ftp账号的存储目录
#vi/usr/local/pure-ftpd/etc/pure-ftpd.conf
VerboseLog yes
NoAnonymous yes
MySQLConfigFile /usr/local/pure-ftpd/etc/pureftpd-mysql.conf
PassivePortRange 30000 50000
MinUID 10000
AltLog clf:/var/log/pureftpd.log
AltLog stats:/var/log/pureftpd.log
AltLog w3c:/var/log/pureftpd.log
reateHomeDir yes
Quota 1000:10
创建数据库
#mysql
createdatabase pureftpd;
grantselect on pureftpd.* to [email protected] identified by ‘456789‘;
flushprivileges;
createtable if not exists `users`( `user` varchar(16) not null default ‘‘, `password`varchar(32) not null default ‘‘,`startdate` date not null, `enddate` date notnull, `uid` int(11) not null, `gid` int (11) not null, `dir` varchar(128) notnull default ‘‘, `quotafiles` int(10) not null default ‘10000‘, `quotasize`int(10) not null default ‘1000‘, `ulbandwidth` int(10) not null, `dlbandwidth`int(10) not null, `ipaddress` varchar(15) not null default ‘*‘, `comment`tinytext, `status` enum(‘0‘,‘1‘) not null default ‘1‘, `ulratio` smallint(5)not null default ‘1‘, `dlratio` smallint(5) not null default ‘1‘,`email`varchar(50) not null default ‘‘, primary key (`user`), unique key `user`(`user`) )engine=innodb default charset=utf8;
实际使用过程中发现自带的账号关闭功能不是很理想,所以考虑使用两张表users和closedftp.
分别存储活动账号和已关闭账号信息
create table closedftp select * from users;
修改数据库配置
vi pureftpd-mysql.conf
MYSQLSocket /var/lib/mysql/mysql.sock
MYSQLUser hzftp
MYSQLPassword 456789
MYSQLCrypt md5
启动 mysql及ftp服务
#service mysqld start
#/usr/local/pure-ftpd/sbin/pure-config.pl /usr/local/pure-ftpd/etc/pure-ftpd.conf
至此ftp搭建完毕,通过操作mysql实现用户管理比较麻烦
在这里本人通过脚本实现
内容如下:
more ftp-manage.sh
#/bin/bash
HOSTNME="127.0.0.1"
PORT="3306"
USERNAME="hzftp"
PASSWORD="456789"
DBNAME="pureftpd"
TABLENAME="users"
TABLENAME1="closedftp"
#closeday=(date +%Y-%m-%d)
#shutday=(date +%Y-%m-%D)
echo "select operation;"
echo "1):添加账号"
echo "2):修改密码"
echo "3):禁用账号"
echo "4):自动禁用"
echo "5):已关闭账号延期"
echo "6):使用中账号延期"
echo "7):查看账号信息"
echo "please select operation"
read operation
case "$operation" in
1)
read -p "input ftp name:" name
read -p "input ftp password:" pass
read -p "input closeday:(格式为0000-00-00)" closeday
sql="select max(uid) from users limit 1"
echo "用户ID一般为依次递增,目前ID已使用至"
sql_max="select max(uid) from users limit 1"
echo &(mysql -h ${HOSTNAME} -P${PORT} -u${USERNAME} -p${PASSWORD} ${DBNAME} -e "${sql_max}")
read -p "input user ID:" ud
read -p "input quota size:" qsize
userhome=/mnt/hzftpdata/$name
echo 信息确认
echo 用户名:"$name",密码:"$pass",关闭日期:"$closeday",用户ID:"$ud",限额:"$qsize",路径:"$userhome"
echo "ctrl+ C" 取消操作
sleep 10
sql="insert into users values (‘$name‘,‘$pass‘,‘$(date +%Y-%m-%d)‘,‘$closeday‘,‘$ud‘,‘10000‘,‘$userhome‘,‘10000‘,‘$q
size‘,‘300‘,‘300‘,‘‘,‘‘,‘1‘,‘1‘,‘1‘,‘‘)"
sql1="select user,password,startdate,enddate,uid,gid,dir from users where user=‘$name‘"
mysql -h ${HOSTNAME} -P${PORT} -u${USERNAME} -p${PASSWORD} ${DBNAME} -e "${sql}"
echo "信息已确认:"
mysql -h ${HOSTNAME} -P${PORT} -u${USERNAME} -p${PASSWORD} ${DBNAME} -e "${sql1}"
;;
2)
read -p "input name:" name
read -p "input pass:" pass
sql="update users set password=‘$pass‘ where user=‘$name‘"
sql2="select user,password,dir,enddate from users where user=‘$name‘"
mysql -h ${HOSTNAME} -P${PORT} -u${USERNAME} -p${PASSWORD} ${DBNAME} -e "${sql}"
mysql -h ${HOSTNAME} -P${PORT} -u${USERNAME} -p${PASSWORD} ${DBNAME} -e "${sql2}"
;;
3)
read -p "input name:" name
sql="update users set status=‘0‘ where user=‘$name‘"
sql2="insert into closedftp select * from users where status=‘0‘ and user=‘$name‘"
sql3="delete from users where user=‘$name‘"
mysql -h ${HOSTNAME} -P${PORT} -u${USERNAME} -p${PASSWORD} ${DBNAME} -e "${sql}"
mysql -h ${HOSTNAME} -P${PORT} -u${USERNAME} -p${PASSWORD} ${DBNAME} -e "${sql2}"
mysql -h ${HOSTNAME} -P${PORT} -u${USERNAME} -p${PASSWORD} ${DBNAME} -e "${sql3}"
;;
4)
sql="update users set status=‘0‘ where enddate<=‘$(date +%Y-%m-%d)‘"
sql2="insert into closedftp select * from users where enddate<=‘$(date +%Y-%m-%d)‘"
sql3="delete from users where enddate<=‘$(date +%Y-%m-%d)‘"
mysql -h ${HOSTNAME} -P${PORT} -u${USERNAME} -p${PASSWORD} ${DBNAME} -e "${sql}"
mysql -h ${HOSTNAME} -P${PORT} -u${USERNAME} -p${PASSWORD} ${DBNAME} -e "${sql2}"
mysql -h ${HOSTNAME} -P${PORT} -u${USERNAME} -p${PASSWORD} ${DBNAME} -e "${sql3}"
;;
5)
read -p "请输入要延期的账号:" name
read -p "延期时间(格式0000-00-00):" closeday
sql="update closedftp set enddate=‘$closeday‘,status=‘1‘ where user=‘$name‘"
sql2="insert into users select * from closedftp where user=‘$name‘"
sql3="delete from closedftp where user=‘$name‘"
sql4="select user,password,dir,enddate from users where user=‘$name‘"
mysql -h ${HOSTNAME} -P${PORT} -u${USERNAME} -p${PASSWORD} ${DBNAME} -e "${sql}"
mysql -h ${HOSTNAME} -P${PORT} -u${USERNAME} -p${PASSWORD} ${DBNAME} -e "${sql2}"
mysql -h ${HOSTNAME} -P${PORT} -u${USERNAME} -p${PASSWORD} ${DBNAME} -e "${sql3}"
mysql -h ${HOSTNAME} -P${PORT} -u${USERNAME} -p${PASSWORD} ${DBNAME} -e "${sql4}"
;;
6)
read -p "请输入要延期的账号:" name
read -p "延期时间(格式0000-00-00):" closeday
sql="update users set enddate=‘$closeday‘ where user=‘$name‘"
sql2="select user,dir,enddate from users where user=‘$name‘"
mysql -h ${HOSTNAME} -P${PORT} -u${USERNAME} -p${PASSWORD} ${DBNAME} -e "${sql}"
echo "信息已更改"
mysql -h ${HOSTNAME} -P${PORT} -u${USERNAME} -p${PASSWORD} ${DBNAME} -e "${sql2}"
;;
7)
read -p "input name:" name
sql="select user,password,dir,startdate,enddate from users where user=‘$name‘"
mysql -h ${HOSTNAME} -P${PORT} -u${USERNAME} -p${PASSWORD} ${DBNAME} -e "${sql}"
;;
*) echo "Error,please select 1,2,3,or 4:"
;;
esac