vsftp自动安装管理脚本

作者: admin 分类: Shell脚本 发布时间: 2020-04-03 19:13

环境:Centos7.6  vsftp

#!/bin/bash
###################################################################
# File Name: vsftp.sh
# Author: xunyin
# E-mail: lnhxzwb@126.com
# Created Time: 2020年 04月 03日
#==================================================================

ftp_conf()
{
cp /etc/vsftpd/vsftpd.conf{,.bak}
cp /etc/pam.d/vsftpd{,.bak}
cat>/etc/vsftpd/vsftpd.conf<<EOF
#禁止匿名用户登录
anonymous_enable=NO
#允许本地用户登录
local_enable=YES
#启用虚拟账户 
guest_enable=YES
#把虚拟账户映射到系统账户virftp               
guest_username=virftp
#使用虚拟用户验证(PAM验证)
pam_service_name=vsftpd
#设置存放各虚拟用户配置文件的目录(此目录下与虚拟用户名相同的文件为他的配置文件)
user_config_dir=/etc/vsftpd/vsftpd_viruser
#启用chroot时,虚拟用户根目录允许写入
allow_writeable_chroot=YES
#开启主动模式
port_enable=YES
#主动模式进行数据传输时使用20端口
connect_from_port_20=YES
#开启被动模式
pasv_enable=YES
pasv_min_port=3000
pasv_max_port=3100
EOF

useradd -d /ftproot -s /sbin/nologin virftp  &>/dev/null
chown -R virftp:virftp /ftproot
mkdir -p /etc/vsftpd/vsftpd_viruser

cat>/etc/pam.d/vsftpd<<EOF
auth       required     pam_listfile.so item=user sense=deny file=/etc/vsftpd/ftpusers onerr=succeed
auth       required     pam_userdb.so   db=/etc/vsftpd/vir_user
account    required     pam_userdb.so   db=/etc/vsftpd/vir_user
EOF
systemctl restart vsftpd
}

db_conf()
{
db_load -T -t hash -f /etc/vsftpd/vir_user  /etc/vsftpd/vir_user.db
chmod 600 /etc/vsftpd/vir_user.db
systemctl restart vsftpd
}

pass_add()
{
read -s -p "请输入密码: " passwd
echo -e " "
read -s -p "请再次输入密码: " pass
echo -e " "
}

check_user()
{
grep "^$name$" /etc/vsftpd/vir_user &>/dev/null
}

pass_true()
{
while true
do
pass_add
if [ "$passwd" == "$pass" ];then
break
else
echo -e "\033[31m============================================\033[0m" 
echo -e "\033[31m 两次输入密码不相同,请重新输入 \033[0m" 
echo -e "\033[31m============================================\033[0m" 
fi
done
}


user_add()
{
read -p "请输入用户名: " name
check_user
if [ $? -eq 0 ];then
echo -e "\033[31m============================================\033[0m" 
echo -e "\033[31m 用户$name已存在 \033[0m"
echo -e "\033[31m============================================\033[0m" 
else
pass_true
echo "$name" >>/etc/vsftpd/vir_user
echo "$pass" >>/etc/vsftpd/vir_user
mkdir -p /ftproot/$name
chown -R virftp.virftp /ftproot/$name
cat >/etc/vsftpd/vsftpd_viruser/${name}.conf <<EOF
# 允许写入
write_enable=YES
#允许浏览FTP目录和下载
anon_world_readable_only=NO
# 允许虚拟用户上传文件
anon_upload_enable=YES
# 允许虚拟用户创建目录
anon_mkdir_write_enable=YES
# 允许虚拟用户执行其他操作(如改名、删除)
anon_other_write_enable=YES
# 上传文件的掩码,如022时,上传目录权限为755,文件权限为644
anon_umask=022
# 指定虚拟用户的虚拟目录(虚拟用户登录后的主目录)
local_root=/ftproot/$name
EOF
db_conf
echo -e "\033[32m============================================\033[0m" 
echo -e "\033[32m 用户$name已创建成功 \033[0m"
echo -e "\033[32m============================================\033[0m" 
fi

}

unlock_name()
{
read -p "请输入解禁的用户名: " name
grep "^$name$" /etc/vsftpd/ftpusers &>/dev/null
if [ $? -eq 0 ];then
sed -i "/^${name}$/d" /etc/vsftpd/ftpusers
echo -e "\033[32m============================================\033[0m" 
echo -e "\033[32m 用户$name已解禁成功 \033[0m"
echo -e "\033[32m============================================\033[0m" 
systemctl restart vsftpd
else
echo -e "\033[31m============================================\033[0m" 
echo -e "\033[31m 您所修改的用户$name不存在 \033[0m"
echo -e "\033[31m============================================\033[0m" 
fi
}

lock_name()
{
read -p "请输入禁用的用户名: " name
check_user
if [ $? -eq 0 ];then
grep "^$name$" /etc/vsftpd/ftpusers &>/dev/null
if [ $? -eq 0 ];then
echo -e "\033[31m============================================\033[0m" 
echo -e "\033[31m 用户$name已被禁用,请不要重复操作 \033[0m" 
echo -e "\033[31m============================================\033[0m" 
else
echo "$name" >>/etc/vsftpd/ftpusers
systemctl restart vsftpd
echo -e "\033[32m============================================\033[0m" 
echo -e "\033[32m 用户$name已禁用成功 \033[0m"
echo -e "\033[32m============================================\033[0m" 
fi
else
echo -e "\033[31m============================================\033[0m" 
echo -e "\033[31m 您所修改的用户$name不存在 \033[0m" 
echo -e "\033[31m============================================\033[0m" 
fi
}

user_del()
{
read -p "请输入删除的用户名: " name
check_user
if [ $? -eq 0 ];then
echo -e "\033[33m 是否删除所选用户及相关数据,请确认后执行 \033[0m"
read -n1 -p "是否删除相关目录数据[Y|N]:" data
echo " "
if [ "$data" == "y" ] || [ "$data" == "Y" ];then
sed -i "/^$name$/,+1d" /etc/vsftpd/vir_user
rm -rf /etc/vsftpd/vsftpd_viruser/$name
rm -rf /ftproot/$name
db_conf
echo -e "\033[32m=====================================================\033[0m"
echo -e "\033[32m 用户$name及相关目录数据已删除 \033[0m"
echo -e "\033[32m=====================================================\033[0m"
elif [ "$data" == "n" ] || [ "$data" == "N" ];then
sed -i "/$name/,+1d" /etc/vsftpd/vir_user
rm -rf /etc/vsftpd/vsftpd_viruser/$name
db_conf
echo -e "\033[32m=====================================================\033[0m"
echo "用户$name已删除 \033[0m"
echo -e "\033[32m=====================================================\033[0m"
else
echo -e "\033[31m============================================\033[0m" 
echo -e "\033[31m 删除用户失败,请检查后执行 \033[0m"
echo -e "\033[31m============================================\033[0m" 
fi
else
echo -e "\033[31m============================================\033[0m" 
echo -e "\033[31m 您所删除的用户$name不存在 \033[0m"
echo -e "\033[31m============================================\033[0m" 
fi

}


change_pass()
{
read -p "请输入修改密码的用户名: " name
check_user
if [ $? -eq 0 ];then
pass_true
sed -i "/^${name}$/{n;s/.*/$pass/}" /etc/vsftpd/vir_user
if [ $? -eq 0 ];then
echo -e "\033[32m=====================================================\033[0m"
echo -e "\033[32m 密码修改成功,请使用新的密码登录\033[0m"
echo -e "\033[32m=====================================================\033[0m"
else
echo -e "\033[31m============================================\033[0m" 
echo -e "\033[31m 密码修改失败,请重新修改 \033[0m"
echo -e "\033[31m============================================\033[0m" 
exit 1
fi
else
echo -e "\033[31m============================================\033[0m" 
echo -e "\033[31m 您所修改的用户$name不存在 \033[0m"
echo -e "\033[31m============================================\033[0m" 
fi
}

rm_ftp()
{
systemctl stop vsftpd
yum remove vsftpd -y
rm -rf /etc/vsftpd
rm -rf /etc/pam.d/vsftpd*
userdel -r virftp
rpm -qa |grep vsftpd 
if [ "$?" != "0" ];then
echo -e "\033[32m=====================================================\033[0m"
echo -e "\033[32m FTP卸载成功! \033[0m"
echo -e "\033[32m程序将退出,感谢使用!\033[0m"
sleep 3
exit 1
else
echo -e "\033[31m============================================\033[0m" 
echo -e "\033[31m FTP卸载失败!\033[0m"
echo -e "\033[31m============================================\033[0m" 
exit 1
fi
}

help()
{
cat <<EOF
+++++++++++++++++++++++++++++++++++++++++++++++++
+   安装FTP输入: install          
+   添加FTP用户: add 
+   修改FTP密码: passwd
+   解禁FTP用户: unlock    
+   禁用FTP用户: lock
+   删除FTP用户: del
+   卸载FTP输入: remove
+   退出FTP程序: exit
+++++++++++++++++++++++++++++++++++++++++++++++++
EOF
}{"type":"block","srcIndex":1,"srcClientId":"892ecf40-c475-4d1f-913c-84dc4e503a51","srcRootClientId":""}

main()
{
while true
do
help
read -p "请选择使用方式: " mod
if [ "$mod" == "install" ];then
pack_ftp=$(rpm -qa |grep vsftpd |wc -l)
if [ "$pack_ftp" -eq "1" ];then
echo -e "\033[32m FTP已被安装,正在配置,请稍等... \033[0m"
sleep 3
ftp_conf
echo -e "\033[32m=====================================================\033[0m"
echo -e "\033[32m FTP配置成功,请创建用户后使用! \033[0m"
echo -e "\033[32m=====================================================\033[0m"
else
echo -e "\033[32m FTP程序正在安装,请稍等...\033[0m"
sleep 3
yum -y install vsftpd libdb-utils
ftp_conf
echo -e "\033[32m=====================================================\033[0m"
echo -e "\033[32m FTP已安装成功,请创建用户后使用! \033[0m"
echo -e "\033[32m=====================================================\033[0m"
fi
elif [ "$mod" == "add" ];then
user_add
elif [ "$mod" == "passwd" ];then
change_pass
elif [ "$mod" == "unlock" ];then
unlock_name
elif [ "$mod" == "lock" ];then
lock_name
elif [ "$mod" == "del" ];then
user_del
elif [ "$mod" == "remove" ];then
rm_ftp
elif [ "$mod" == "exit" ];then
echo -e "\033[32m程序将退出,感谢使用\033[0m"
sleep 5
exit 1
else
echo -e "\033[33m============================================\033[0m" 
echo -e "\033[33m 您所输入的参数不正确,请按帮助信息选择 \033[0m"
echo -e "\033[33m============================================\033[0m" 
fi
done
}

main


温馨提示:如无特殊说明,本站文章均为作者原创,转载请注明出处!

发表评论