微服务环境部署时,几十台机器需要部署系统,编写文件传输脚本如下.
默认读取同级目录下的data_list.conf 传输配置文件
Linux需要安装依赖软件Tcl、expect.
#!/bin/bash
data_list="data_list.conf"
if [ "$1" != "" ];then
data_list="$1"
fi
echo "开始时间:"`date "+%Y-%m-%d %H:%M:%S"`
cat $data_list | while read ip user pass sfile tdir
do
#echo "IP is $ip"
#echo "USER is $user"
#echo "PASS is $pass"
#echo "SFILE is $sfile"
#echo "TDIR is $tdir"
#echo \"command is : scp -r "$sfile" "$user"@"$ip":"$tdir"\"
#sup_sfile=$(echo -e "${sfile}" | awk -F '\\|' '{print $0}')
if [[ $sfile =~ '|' ]];then
path="${sfile%%|*}"
#echo "path is $path"
sup_sfile="$(echo -e "${sfile#*|}" | sed 's/|/ /g' )"
for s_sfile in $sup_sfile
do
#echo "s_sfile is $path$s_sfile"
/usr/bin/expect -c "
set timeout 3;
spawn ssh -f -o StrictHostKeyChecking=no "$user"@"$ip" mkdir -p "$tdir"
expect {
\"(yes/no)?\" { send \"yes\r\";exp_continue }
\"*ssword:\" { send \"$pass\r\";exp_continue }
}
spawn scp -r "$path$s_sfile" "$user"@"$ip":"$tdir"
expect {
\"(yes/no)?\" { send \"yes\r\";exp_continue }
\"*ssword:\" { send \"$pass\r\";exp_continue }
}
"
sleep 1
done
else
sup_sfile="$(echo -e "${sfile}" | sed 's/|/ /g' )"
for s_sfile in $sup_sfile
do
#echo "s_sfile is $s_sfile"
/usr/bin/expect -c "
set timeout 3;
spawn ssh -f -o StrictHostKeyChecking=no "$user"@"$ip" mkdir -p "$tdir"
expect {
\"(yes/no)?\" { send \"yes\r\";exp_continue }
\"*ssword:\" { send \"$pass\r\";exp_continue }
}
spawn scp -r "$s_sfile" "$user"@"$ip":"$tdir"
expect {
\"(yes/no)?\" { send \"yes\r\";exp_continue }
\"*ssword:\" { send \"$pass\r\";exp_continue }
}
"
sleep 1
done
fi
done
echo "结束时间:"`date "+%Y-%m-%d %H:%M:%S"`
配置文件如下 data_list.conf
也可以自行定义多个conf文件,执行shell时进行指定参数即可
10.11.149.42 tomcat Edxd@123 /app/tomcat/scms/easyloan/system-springboot-1.0.0-SNAPSHOT.jar|/app/tomcat/scms/easyloan/spring-boot.sh|/app/tomcat/scms/easyloan/startall.sh|/app/tomcat/scms/easyloan/stopall.sh|/app/tomcat/scms/easyloan/rules|/app/tomcat/scms/easyloan/kettle|/app/tomcat/scms/easyloan/template|/app/tomcat/scms/easyloan/writeList.properties /app/tomcat/scms/easyloan
10.11.149.51 tomcat Edxd@123 /app/tomcat/scms/easyloan/dataApplication-springboot-1.0.0-SNAPSHOT.jar|/app/tomcat/scms/easyloan/spring-boot.sh|/app/tomcat/scms/easyloan/startall.sh|/app/tomcat/scms/easyloan/stopall.sh|/app/tomcat/scms/easyloan/rules|/app/tomcat/scms/easyloan/kettle|/app/tomcat/scms/easyloan/template /app/tomcat/scms/easyloan
10.11.149.52 tomcat Edxd@123 /app/tomcat/scms/easyloan/dataApplication-springboot-1.0.0-SNAPSHOT.jar|/app/tomcat/scms/easyloan/spring-boot.sh|/app/tomcat/scms/easyloan/startall.sh|/app/tomcat/scms/easyloan/stopall.sh|/app/tomcat/scms/easyloan/rules|/app/tomcat/scms/easyloan/kettle|/app/tomcat/scms/easyloan/template /app/tomcat/scms/easyloan
10.11.149.61 tomcat Edxd@123 /app/tomcat/scms/easyloan/afterloan-springboot-1.0.0-SNAPSHOT.jar|/app/tomcat/scms/easyloan/spring-boot.sh