多服务器scp文件

有时候需要在多个服务器scp多个文件,一个个登录很繁琐,写个脚本来搞。

前提,先做ssh的证书登录。用ssh-keygen和ssh-copy-id,这个查阅其他文档,或者自己摸索一下也不难。我不久前也写过一个,和这个类似的。


#!/bin/bash
SLEEPTIME=5

SVR_ALL="172.41.17.127 172.41.17.128 172.41.17.129 172.41.17.130 172.41.17.131 172.41.17.132 172.41.17.135 172.41.17.136 172.41.17.184 172.41.17.185"
SVR_OTHER="172.41.17.127 172.41.17.128 172.41.17.129 172.41.17.130 172.41.17.131 172.41.17.132 172.41.17.135 172.41.17.184 172.41.17.185"
yourlist=$1
source=$2
target=$3

if [ "$yourlist" == "" -o "$source" == "" -o "$target" == "" ]; then
  echo scp_multi.sh server_list source target
  echo input server ip you want to deploy. saparated by ,
  echo or input ALL,OTHER to deploy all server.
  echo all server list: $SVR_LIST
  exit -1
elif [ "$yourlist" == "ALL" ];then
  SVR_LIST=$SVR_ALL
elif [ "$yourlist" == "OTHER" ];then
  SVR_LIST=$SVR_OTHER
else
    SVR_LIST=`echo $yourlist | awk -F "," '{for (i=1;i <= NF ;i++){print $i}}'`
fi
echo input server list : $SVR_LIST


  

echo will scp \"$source\" to $SVR_LIST \"$target\"
sleep 5
for server in $SVR_LIST;do
  echo scp $source $server:$target
  scp $source $server:$target
done

echo done all!



你可能感兴趣的:(工作日记)