使用脚本同步文件

1、配置root用户的ssh免密登陆

1)su root

2)ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa

3)ssh-copy-id root@s101(s102,s103,s104)

2、将所有节点的"jps"命令创建符号链接:sudo ln -s /soft/jdk/bin/ /usr/local/bin/jps

3、nano xcall.sh ====== 编写脚本,并放到"/usr/local/bin"目录下,作用:远程在其他节点执行命令,并将结果显示在当前节点

#!/bin/bash
for((i=101; i<=105; i++)) ; do
    tput setaf 2
    echo -------- s$i $@ --------
    tput setaf 7
    ssh s$i $@
done

4、使用root用户在所有机器上安装rsync:xcall.sh yum install -y rsync

5、nano xsync.sh ====== 编写脚本,并放到"/usr/local/bin"目录下,作用:将文件发送到其他节点的相同目录下

#!/bin/bash
name=`whoami`
# 指定文件所在文件夹名称
dir=`dirname $1`
# 指定文件的文件名
filename=`basename $1`
# 进入到dir中
cd $dir
# 得到当前目录的绝对路径
fullpath=`pwd`
for((i=102 ; i<=105; i++)) ; do
    tput setaf 2
    echo -------- s$i $@ --------
    tput setaf 7
    rsync -lr $filename "$name"@s"$i":$fullpath
done

6、举例:xsync.sh /etc/hosts ====== 同步hosts文件

猜你喜欢

转载自blog.csdn.net/foyemazl/article/details/81363006