1 #!/bin/bash23 vsftp_conf="/etc/vsftpd/vsftpd.conf"45 if [ -f $vsftp_conf ]; then6 if [ $# -ne 1 ]; then7 echo "Parameters is not allow"8 echo "usage: $0 on or $0 off"9 exit 110 fi1112 case "$1" in13 on) CMD='YES';;14 off) CMD='NO';;15 *)16 echo "format error!"17 exit 118 ;;19 esac2021 if [ $CMD = 'YES' ]; then22 sed 's/^.*anonymous_enable=.*/anonymous_enable=YES/' $vsftp_conf > /tmp/$$.vsftpd.conf23 mv -f /tmp/$$.vsftpd.conf $vsftp_conf24 /etc/init.d/vsftpd restart25 else26 sed 's/^.*anonymous_enable=.*/anonymous_enable=NO/' $vsftp_conf > /tmp/$$.vsftpd.conf27 mv -f /tmp/$$.vsftpd.conf $vsftp_conf28 /etc/init.d/vsftpd restart29 fi30 else echo "$vsftp_conf is not exist"31 fi
說明:
列5 先判斷/etc/vsftpd/vsftpd.conf是否存在,若不存在則回報檔案不存在。(列30)列6 判斷所傳入的引數是否為1個,若不是則回報錯誤並結束。 (列7~10)列12-19 使用case,判斷是on, off或是格式有誤。列21-24 若為on則設定anonymous_enable=YES,並重新啟動vsftpd。列25-28 若為off則設定anonymous_enable=NO,並重新啟動vsftpd。
測試:
1. 允許匿名登入:
[root@centos bin]# ./vsftpd.sh onShutting down vsftpd: [ OK ]Starting vsftpd for vsftpd: [ OK ][root@centos bin]# ftp localhostConnected to localhost.localdomain.220 (vsFTPd 2.0.5)530 Please login with USER and PASS.530 Please login with USER and PASS.KERBEROS_V4 rejected as an authentication typeName (localhost:root): ftp331 Please specify the password.Password:230 Login successful.Remote system type is UNIX.Using binary mode to transfer files.ftp>
2. 不允許匿名登入:
[root@centos bin]# ./vsftpd.sh offShutting down vsftpd: [ OK ]Starting vsftpd for vsftpd: [ OK ][root@centos bin]# ftp localhostConnected to localhost.localdomain.220 (vsFTPd 2.0.5)530 Please login with USER and PASS.530 Please login with USER and PASS.KERBEROS_V4 rejected as an authentication typeName (localhost:root): ftp331 Please specify the password.Password:530 Login incorrect.Login failed.ftp>
References:
Linux Shell 程式設計實務 P.305
No comments:
Post a Comment