传接文件到多个远程机器的脚本

传送文件到多个远程机器的脚本

写一个传送文件到远程机器的脚本

因为在进行升级操作的时候,一些应用的文件都需要替换,如果一个一个替换会很麻烦也很慢,所以有必要写个脚本进行传送。

因为这些应用在各个远程机器上的部署路径都是一样的,脚本就相对简单很多了。

ps:中式英文都是经过谷歌认证的传接文件到多个远程机器的脚本

#!/bin/bash
remote_ip="192.168.161.5 192.168.161.5"
jar_dir="/opt/jar"
if [ $# -eq "0" ]
then
echo "must have one file to send"
exit 0
elif [ $# -gt "1" ]
then
echo "only send one file per times"
exit 0
fi
file_type=`echo $1|awk -F. '{print $2}'`
for ip in $remote_ip
do
if [ -n "$file_type" ]
then
if [ "$file_type" = "jar" ]
then
echo "scp $jar_dir/$1 $ip:$jar_dir"
scp $jar_dir/$1 $ip:$jar_dir
fi
else
echo "this file $1 is invalid"
exit 0
fi
done

经过测试,通过

-bash-3.00# ./send_file.sh test.jar
scp /opt/jar/test.jar 192.168.161.5:/opt/jar
test.jar             100% |*********************************************************************************|     0       00:00   
scp /opt/jar/test.jar 192.168.161.5:/opt/jar
test.jar             100% |*********************************************************************************|     0       00:00

 

可以根据要传送的文件加路径变量即可。

后面想了想,优化优化了上面那个脚本,底下这样也行

用case语句代替if更灵活些

 

-bash-3.00# more send_file2.sh
#!/bin/bash
remote_ip="192.168.161.5 192.168.161.5"
jar_dir="/opt/jar"
if [ $# -eq "0" ]
then
echo "must hava one file to send"
exit 0
elif [ $# -gt "1" ]
then
echo "only send one file per times"
exit 0
fi
file_type=`echo $1|awk -F. '{print $2}'`
for ip in $remote_ip
do
case "$file_type" in
jar)
echo "scp $jar_dir/$1 $ip:$jar_dir"
scp $jar_dir/$1 $ip:$jar_dir
;;
*)
echo "this file $1 is invalid"
break
;;
esac
done

 

测试

-bash-3.00# ./send_file2.sh test.jar
scp /opt/jar/test.jar 192.168.161.5:/opt/jar
test.jar             100% |*********************************************************************************|     0       00:00   
scp /opt/jar/test.jar 192.168.161.5:/opt/jar
test.jar             100% |*********************************************************************************|     0       00:00

 

也无问题。