ftp上传下载shell脚本

Table of Contents

工作中,通过ftp反复上传下载大文件来测试路由器的稳定性。

一共有两个脚本:

1  ftp.sh,主要调用这个脚本,内容如下,

----------------------------------------------------------------------------------------------------------

#!/bin/bash
# script function:
# 1 run ftp to upload and download file every fixed minutes;
# 2 check host online or not before every ftp loop;

# 2015/04/19 Attaboy first release
# 2015/05/08 Attaboy modified.
# 2015/05/15 Attaboy modified.
# 2015/05/19 Zhu Li modified.
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:/~bin
export PATH

# kill all ftp processes .
for j in `ps -ef | grep -i ftp | grep -v grep | grep -v vsftpd | awk '{print $2}' | grep -v $$`
do
kill -9 $j
done

while [ 2 == 2 ]
do

# create 3 files in fixed size.
dir /tmp/test/ftptest && rm -rf /tmp/test/ftptest/file /tmp/test/ftptest/get
mkdir -p /tmp/test/ftptest/get /tmp/test/ftptest/file
dd if=/dev/zero of=/tmp/test/ftptest/file/100m.file bs=1M count=0 seek=100
dd if=/dev/zero of=/tmp/test/ftptest/file/10m.file bs=1M count=0 seek=10
dd if=/dev/zero of=/tmp/test/ftptest/file/1m.file bs=1M count=0 seek=1

# check host online or not.
declare -i pingerr_1=`ping -c 3 10.0.0.1 | grep -i Unreachable`
declare -i pingerr_2=`ping -c 3 10.0.0.1 | grep -i out`
declare -i pingerr_3=`ping -c 3 10.0.0.1 | grep -i quench`
declare -i pingerr_4=`ping -c 3 10.0.0.1 | grep -i bad`
declare -i pingerr_5=`ping -c 3 10.0.0.1 | grep -i host`
if [ $pingerr_1 == 3 -or $pingerr_2 == 3 -or $pingerr_3 == 3 -or $pingerr_4 == 3 $pingerr_5 == 3 ] ; then
# if [ $pingerr_3 == 50 -or $pingerr_4 == 50 ] ; then
echo "Oops,ftp server ping time out!"
echo "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@" >> /tmp/test/ftptest/ftp.log
echo "`date` ----- Oops, dut ping time out!" >> /tmp/test/ftptest/ftp.log
echo "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@" >> /tmp/test/ftptest/ftp.log
# exit 9
# fi
fi

# get DUT config .
echo '>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>' >> /tmp/test/ftptest/dut_info.txt
(sleep 2 ; echo root ;
sleep 2 ; echo ps@20140618 ;
sleep 1 ; echo cat /etc/firmwareInfo ;
sleep 1 ; echo time ;
sleep 1 ; echo uptime ;
sleep 1 ; echo date ;
sleep 1 ; echo free ;
sleep 1 ; echo df -h ;
sleep 1 ; echo fdisk -l ;
sleep 1 ; echo dmesg ;
sleep 1 ; echo ps ;
sleep 1 ; echo iwconfig ;
sleep 1 ; echo ifconfig ;
sleep 1 ; echo route -n ;
sleep 1 ; echo arp ;
sleep 1 ; echo ping -c 4 qq.com ;
sleep 4 ; echo wlanconfig ath0 list ;
sleep 1 ; echo netstat -antp ;
sleep 1 ; echo iptables -vnL ;
sleep 1 ; echo iptables -t nat -vnL ;
sleep 1 ; echo iptables -t mangle -vnL ;
sleep 1 ; echo exit) | telnet 10.0.0.1 &> /tmp/test/ftptest/dut_info.txt
echo '<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<' >> /tmp/test/ftptest/dut_info.txt
echo "|||||||||||||||||||||`date`|||||||||||||||||||||||" >> /tmp/test/ftptest/dut_info.txt

# get the first ftp root folder of the ftp server embeded in DUT.
(sleep 2;echo root;
sleep 2;echo ps@20140618;
sleep 1;echo df -h;
sleep 1;echo exit) | telnet 10.0.0.1 &> /tmp/tmpfile
ftpdir=`grep "tmp" /tmp/tmpfile | awk 'NR==1 {print $6}'`
if [ "${ftpdir}" == "" ] ; then
echo -e "your disk hasn't been correctly mounted,please check again."
echo "##############################################################" >> /tmp/test/ftptest/ftp.log
echo "`date` -----> Oops, disk umounted!" >> /tmp/test/ftptest/ftp.log
echo "##############################################################" >> /tmp/test/ftptest/ftp.log
#exit 7
fi

rm -f /tmp/tmpfile

# start ftp up and down.
ftp -n <<end
open 10.0.0.110
user snake snake
hash
binary
prompt
lcd /tmp/test/ftptest/file
mkdir $HOSTNAME$$
cd $HOSTNAME$$
!echo -e "$(date)">>/tmp/test/ftptest/ftp.log
!echo -e "-----------------------------------------------------------">>/tmp/test/ftptest/ftp.log
mput *
lcd /tmp/test/ftptest/get
mget *
!rm -rf *
mdelete *
cd ..
rmdir $HOSTNAME$$
quit
end

# kill all ftp processes .
for i in `ps -ef | grep -i ftp | grep -v grep | grep -v lftp | grep -v vsftpd | awk '{print $2}' | grep -v $$`
do
kill -9 $i
done

ftp -n <<end
open 10.0.0.1
user snake snake
hash
binary
prompt
lcd /tmp/test/ftptest/file
cd $ftpdir
mkdir $HOSTNAME$$
cd $HOSTNAME$$
!echo -e "$(date)">>/tmp/test/ftptest/ftp.log
!echo -e "-----------------------------------------------------------">>/tmp/test/ftptest/ftp.log
mput *
lcd /tmp/test/ftptest/get
!rm -rf *
mget *
mdelete *
cd ..
rmdir $HOSTNAME$$
quit
end

# kill all ftp processes .
for i in `ps -ef | grep -i ftp | grep -v grep | grep -v lftp | grep -v vsftpd | awk '{print $2}' | grep -v $$`
do
kill -9 $i
done

done

----------------------------------------------------------------------------------------------------------------------------

2  ftp_reset.sh,顾名思义,就是重置ftp进程的脚本,因为ftp在实际运行中经常出现挂死现象,解决办法是在centos的crontab下添加定时任务,每隔3分钟运行一次ftp_reset.sh脚本,可以在很大程度上避免ftp程序挂死的问题。

#!/bin/bash
# 2015/05/08    Attaboy    first release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:/~bin
export PATH

for i in `ps -ef | grep -i ftp | grep -v ftp_reset | grep -v grep | grep -v vsftpd | grep -v defunct | awk '{print $2}' | grep -v $$`
do    
kill -9 $i
done

exec /tmp/ftp/public/ftp.sh &

exit 0