测试远端FTP是否正常的SHELL
时间:2007-11-05 来源:yangiori007
通过CURL+SHELL测试远程FTP主机工作状态
---自动切换上传FTP主机
由于公司的设备很多,而且每台SQUID的日志都要定时上传到本网段内部的FTP服务器.而有些时候会因为UPLOAD上传配置文件错误或远程FTP主机服务DOWN,而导致SQUID日志大量堆积.现在通过CURL进行FTP状态的测试,SHELL进行IP地址的替换.解决问题.与大家分享如下代码.请多提宝贵意见与建议。本人还是比较菜,代码部分过于烦琐.请见谅
#!/bin/bash
UPLOAD_CONF=/usr/local/squid/etc/upload.conf ##########AUTO_create FTP host IP##################################
OPT=0
rm -f ./ftp.host #####根据客户机的IP地址来创建本网段可用的FTP主机地址############################# if [ -f /etc/sysconfig/network-scripts/ifcfg-eth0 ];then
IP=`cat /etc/sysconfig/network-scripts/ifcfg-eth0|awk -F = '/IPADDR/{print $2}'`
echo $IP|grep -i -E "^192" > /dev/null
if [ $? == 0 ];then
for ip in 111 112 113 114
do
echo "${IP%.*}.$ip" >> ./ftp.host
done
else
OPT=1
fi
fi if [ $OPT == 1 ];then IP=`cat /etc/sysconfig/network-scripts/ifcfg-eth1|awk -F = '/IPADDR/{print $2}'`
echo $IP|grep -i -E "^172" > /dev/null
if [ $? == 0 ];then
for ip in 111 112 113 114
do
echo "${IP%.*}.$ip" >> ./ftp.host
done
else
exit 1
fi
fi
###################################################################################
##############取FTP上传信息######################################################### IP=`cat $UPLOAD_CONF|awk -F = '/LDCIP/ {print $2}'`
USER=`cat $UPLOAD_CONF|awk -F = '/USER/ {print $2}'`
PASSWD=`cat $UPLOAD_CONF|awk -F = '/PASSWD/ {print $2}'`
#######冗余判断##################################################################### IP=${IP:-"`head -n1 ./ftp.host`"}
USER=${USER:-"fromcf"}
PASSWD=${PASSWD:-"fromcf102090"} ##########################测试主程序############################################### /usr/bin/curl -u $USER:$PASSWD ftp://$IP -o /dev/null -s if test $? != 0;then while read IP2
do
/usr/bin/curl -u $USER:$PASSWD ftp://$IP2 -o /dev/null -s if [ $? == 0 ];then
sed "s/$IP/$IP2/g" ./upload.conf > ./upload.conf.tmp&&cp -f ./upload.conf.tmp ./upload.conf&&rm -f ./upload.conf.tmp
ERR=0
break
else
ERR=1
fi done < ./ftp.host if [ $ERR != 0 ];then
echo "Please check you HOST-network or REMOTE-HOST-FTP"
exit 1
fi
fi ------------------------本文完----------------------------------------------------- BY : IORI 2007-11-5 IN chinacache
UPLOAD_CONF=/usr/local/squid/etc/upload.conf ##########AUTO_create FTP host IP##################################
OPT=0
rm -f ./ftp.host #####根据客户机的IP地址来创建本网段可用的FTP主机地址############################# if [ -f /etc/sysconfig/network-scripts/ifcfg-eth0 ];then
IP=`cat /etc/sysconfig/network-scripts/ifcfg-eth0|awk -F = '/IPADDR/{print $2}'`
echo $IP|grep -i -E "^192" > /dev/null
if [ $? == 0 ];then
for ip in 111 112 113 114
do
echo "${IP%.*}.$ip" >> ./ftp.host
done
else
OPT=1
fi
fi if [ $OPT == 1 ];then IP=`cat /etc/sysconfig/network-scripts/ifcfg-eth1|awk -F = '/IPADDR/{print $2}'`
echo $IP|grep -i -E "^172" > /dev/null
if [ $? == 0 ];then
for ip in 111 112 113 114
do
echo "${IP%.*}.$ip" >> ./ftp.host
done
else
exit 1
fi
fi
###################################################################################
##############取FTP上传信息######################################################### IP=`cat $UPLOAD_CONF|awk -F = '/LDCIP/ {print $2}'`
USER=`cat $UPLOAD_CONF|awk -F = '/USER/ {print $2}'`
PASSWD=`cat $UPLOAD_CONF|awk -F = '/PASSWD/ {print $2}'`
#######冗余判断##################################################################### IP=${IP:-"`head -n1 ./ftp.host`"}
USER=${USER:-"fromcf"}
PASSWD=${PASSWD:-"fromcf102090"} ##########################测试主程序############################################### /usr/bin/curl -u $USER:$PASSWD ftp://$IP -o /dev/null -s if test $? != 0;then while read IP2
do
/usr/bin/curl -u $USER:$PASSWD ftp://$IP2 -o /dev/null -s if [ $? == 0 ];then
sed "s/$IP/$IP2/g" ./upload.conf > ./upload.conf.tmp&&cp -f ./upload.conf.tmp ./upload.conf&&rm -f ./upload.conf.tmp
ERR=0
break
else
ERR=1
fi done < ./ftp.host if [ $ERR != 0 ];then
echo "Please check you HOST-network or REMOTE-HOST-FTP"
exit 1
fi
fi ------------------------本文完----------------------------------------------------- BY : IORI 2007-11-5 IN chinacache
相关阅读 更多 +