使用ftp进行主机间文件传输脚本样例

写作背景:weblogic相关包,集群内不同主机间进行分发,如果集群下主机过多、需要分发的文件过多,手动拖拽不仅耗时,而且容易出错(文件错或者路径错,一旦出错,影响很大)。

1、文件结构,脚本pushfile.sh负责分发文件,入参共4个

target_ip=$1 目标主机IP
curr_path=$2 当前主机路径
target_path=$3 目标主机路径
filename=$4 文件名

webusr@iomtimer1:/weblogic/ftpput >ls
pushfile.sh puttimerall.sh puttimerear.sh puttimerorder.sh
webusr@iomtimer1:/weblogic/ftpput >pwd
/weblogic/ftpput
webusr@iomtimer1:/weblogic/ftpput >

webusr@iomtimer1:/weblogic/ftpput >cat pushfile.sh
##sh pushfile.sh 132.77.255.37 /weblogic/ftpput /weblogic/ftpput/test test.sh
##将ear包和jar包传递到相同的路径
path_script=`pwd`
target_ip=$1
curr_path=$2
target_path=$3
filename=$4
TODAY=`date +%Y%m%d%H%M`
file_bak=$filename$TODAY
echo `date`开始执行
# scp $curr_path/$filename webusr@$target_ip:$target_path/
# scp /weblogic/ftpput/test.sh [email protected]:/weblogic/ftpput/test
cd $curr_path
echo 'ftp connect start'
sftp webusr@$target_ip << EOF
cd $target_path
chmod 777 $filename
rename $filename $file_bak
put $filename
bye
EOF
echo 'ftp connect end'
echo `date`执行结束
cd $path_script
webusr@iomtimer1:/weblogic/ftpput >

2、单个文件分发,如ear包,orderapp.jar包

webusr@iomtimer1:/weblogic/ftpput >cat puttimerear.sh
##sh pushfile.sh target_ip curr_path target_path filename
sh /weblogic/ftpput/pushfile.sh 132.77.255.37 /weblogic/timerdomain/applications /weblogic/timerdomain/applications sps888.ear
#sh pushfile.sh 132.77.255.37 /weblogic/timerdomain/applications /weblogic/timerdomain/applications sps888.ear

webusr@iomtimer1:/weblogic/ftpput >cat puttimerorder.sh
##sh pushfile.sh target_ip curr_path target_path filename
sh /weblogic/ftpput/pushfile.sh 132.77.255.37 /weblogic/timerdomain/libapp /weblogic/timerdomain/libapp orderapp.jar
#sh pushfile.sh 132.77.255.37 /weblogic/timerdomain/applications /weblogic/timerdomain/libapp orderapp.jar

3、整体分发

webusr@iomtimer1:/weblogic/ftpput >cat puttimerall.sh
sh /weblogic/ftpput/puttimerear.sh
sh /weblogic/ftpput/puttimerorder.sh
webusr@iomtimer1:/weblogic/ftpput >

4、补充说明

前提是在主机间建立信任,免密码访问,客户端对服务端有sftp权限,网络连通等。

猜你喜欢

转载自www.cnblogs.com/wzh313/p/9071700.html