test-definitions/blob/master/auto-test/ftp/ftp.sh

#! /bin/bash

vsftpd_op()
{
    local cmd=""
    local operation=$1
    local log_file="vsftpd.log"
#根据不同发行版起不同的service。以ubuntu为例,这里可能是service vsftpd start
    case $distro in
        "ubuntu" | "debian" )
            cmd="service vsftpd $operation"
            echo "$cmd" | tee ${log_file}
            $cmd | tee ${log_file}
            ;;
        * )
		#其他发行版用systemctl 来启动service
            #cmd="${operation}_service vsftpd"
            cmd="systemctl ${operation} vsftpd.service"
            echo "$cmd" | tee ${log_file}
           #eval \$$cmd | tee ${log_file}
            ;;
    esac
}

vsftpd_execute()
{
    local operation=$1
	#调用函数vsftpd_op 控制ftp的service
    vsftpd_op $operation
	#等于零的话,测试失败?
    if [ 0 -ne $? ]; then
        echo "vsftpd $operation failed"
        lava-test-case vsftpd-$operation --result fail
    else
        echo "vsftpd $operation pass"
        lava-test-case vsftpd-$operation --result pass
    fi
}

set -x

cd ../../utils
    . ./sys_info.sh
cd -
#distro=`cat /etc/redhat-release | cut -b 1-6`
#根据不同的发行版安装不同的包
case $distro in
    "ubuntu")
        apt-get install vsftpd -y
        apt-get install expect -y
        print_info $? install-package
        ;;
    "centos")
        yum install vsftpd -y
        yum install vsftpd.aarch64 -y
        yum install expect -y
        yum install ftp -y
        print_info $? install-package
        ;;
    "opensuse")
        zypper install -y ftp
        zypper install -y expect
        ;;
esac

# test case -- start, stop, restart
#分别测试start/restart/stop 这三个操作的ftp
vsftpd_execute start
vsftpd_execute restart
vsftpd_execute stop

#process=$(vsftpd_op status | grep "running")
#if [ "$process"x != ""x  ]; then
 #   vsftpd_op stop
#fi

FTP_PUT_LOG=ftp_put_test.log
FTP_GET_LOG=ftp_get_test.log
if [ "$distro"x = "centos"x ] ;
then
	FTP_USERS=/etc/vsftpd/ftpusers
	VSFTPD_CONF=/etc/vsftpd/vsftpd.conf
else
	FTP_USERS=/etc/ftpusers
	VSFTPD_CONF=/etc/vsftpd.conf
fi

if [ ! -e ${FTP_USERS}.origin ];
then
    cp ${FTP_USERS}{,.origin}
else
    cp ${FTP_USERS}{.origin,}
fi

if [ ! -e ${VSFTPD_CONF}.origin ];
then
    cp ${VSFTPD_CONF}{,.origin}
else
    cp ${VSFTPD_CONF}{.origin,}
fi

# prepare for the put and get test and the ftp home is ~/
mkdir tmp && cd tmp
echo 'For ftp put testing' > $FTP_PUT_LOG
echo 'For ftp get testing' > ~/$FTP_GET_LOG
#替换配置文件中的变量
sed -i 's/root/#root/g' $FTP_USERS
sed -i 's/listen=NO/listen=YES/g' $VSFTPD_CONF
sed -i 's/listen_ipv6=YES/#listen_ipv6=YES/g' $VSFTPD_CONF
sed -i 's/#write_enable=YES/write_enable=YES/g' $VSFTPD_CONF
sed -i 's/write_enable=NO/write_enable=YES/g' $VSFTPD_CONF
sed -i 's/userlist_enable=YES/userlist_enable=NO/g' $VSFTPD_CONF
if [ "$distro" == "ubuntu" ] ; then
    sed -i 's/pam_service_name=vsftpd/pam_service_name=ftp/g' $VSFTPD_CONF
fi
#配置完参数后重启tftp的服务
vsftpd_op restart
#睡眠5s
sleep 5
#测试ftp service的状态.
vsftpd_op status
systemctl restart vsftpd.service
service restart vsftpd.service
# for get and put test
cd /root
#SELinux安全访问策略限制会导致550 Failed to open file的错误所以这里打开
setsebool -P allow_ftpd_full_access 1
cd -
EXPECT=$(which expect)
#使用绝对路径执行expect
$EXPECT << EOF
set timeout 100
spawn ftp localhost
expect "Name"
send "\r"
expect "Password"
send "root\r"
expect "ftp>"
#passive表示被动,ftp的工作模式有主动和被动解决"227 Entering Passive MOde"
send "passive\r"
expect "ftp>"
send "get ftp_get_test.log\r"
expect {
   "Transfer complete"
   {
       send "put ftp_put_test.log\r"
       expect "Transfer complete"
   }
   "Failed to open file"
   {
       send "put ftp_put_test.log\r"
       expect "Transfer complete"
   }
   "Connection refused"
   {
       send "put ftp_put_test.log\r"
       expect "Transfer complete"
   }
}
send "quit\r"
expect eof
EOF
#看是否可以查找到FTP_GET_LOG 文件
if [ $(find . -maxdepth 1 -name "$FTP_GET_LOG")x != ""x ]; then
    lava-test-case vsftpd-download --result pass
else
    lava-test-case vsftpd-download --result fail
fi

cd -
#cd到用户的根目录
cd ~

if [ $(find . -maxdepth 1 -name "ftp_put_test.log")x != ""x ]; then
    lava-test-case vsftpd-upload --result pass
else
    lava-test-case vsftpd-upload --result fail
fi
#删除tmp 目录
#删除安装包
rm -rf tmp
case $distro in
    "ubuntu")
        apt-get remove vsftpd expect -y
        print_info $? remove-package
        ;;
    "centos")
        yum remove vsftpd expect -y
        print_info $? remove-package
        ;;
esac

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值