Linux command batch execution script and file distribution script

1. Linux command to execute scripts in batches (executed on multiple servers at the same time)

vi batchexec.sh
#!/bin/sh
#打开方式 命令

#输入命令
CMDNAME=$1
for i in node2 node3 node4
        do
                echo "----------$i----------"
                ssh $i "$CMDNAME"
        done

2. Sorting and distribution script

#安装 rsync
yum -y install rsync
vi xsync.sh
#!/bin/bash
#校验参数是否合法

#如果没有传递参数
if(($#==0))
then
          echo 请输入要同步的文件~
            exit;
fi

#节点数组
nodes=(node2 node3 node4)

#拼接要同步的文件的绝对路径
#获取父路径,加入-P是防止软链接文件绝对路径读取错误
dirpath=$(cd `dirname $1`; pwd -P)
filename=`basename $1`

echo dirpath是$dirpath
echo filename是$filename

echo 要同步的文件路径是: $dirpath/$filename

#循环执行rsync同步文件到集群的每台机器
#for i in node2 node3 node4
for i in "${nodes[@]}"
do
         echo ----------------$i------------------
         # -o StrictHostKeyChecking=no 第一次ssh也不用输入yes
         ssh -o StrictHostKeyChecking=no $i 'echo "ok"'
         rsync -av --rsync-path="sudo rsync" $dirpath/$filename $i:$dirpath
  done

Guess you like

Origin blog.csdn.net/xfp1007907124/article/details/132555359