Shell复制文件到多台服务器 Shell脚本实现复制文件到多台服务器的代码分享

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Shell复制文件到多台服务器 Shell脚本实现复制文件到多台服务器的代码分享

  2021-03-19 我要评论
想了解Shell脚本实现复制文件到多台服务器的代码讲解的相关内容吗,在本文为您仔细讲解Shell复制文件到多台服务器的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Shell脚本,复制文件,多台服务器,下面大家一起来学习吧。

在多机集群环境中,经常面临修改配置文件后拷贝到多台服务器的情况,传统的执行scp比较麻烦,所以写了以下shell脚本,可以将指定文件拷贝到多台机器。

使用方法请参见HELP部分代码。

#!/bin/bash
 help()
 {
  cat << HELP
    --------------HELP------------------------
    This shell script can copy file to many computers.
    Useage:
        copytoall filename(full path form /home) targetpathfrom/ username ip1 ip2 ip3....
    Example:
        copytoall /home/casliyang/Hadoop-2.2.0/etc/hadoop/core-site.xml /home/casliyang/hadoop-2.2.0/etc/hadoop/ casliyang 192.168.0.5 192.168.0.6 192.168.0.7 192.168.0.8
    ------------------------------------------
 HELP
  exit 0
 }

 currentdate=$(date +%Y-%m)

 echo $currentdate " execute copytoall"

 if [ $1 = "-h" ] ; then 
    help
    exit 0
 fi

 file=$1
 shift
 targetpath=$1
 shift
 user=$1
 shift
 tempip=0

 if [ -f $file ] ; then
    while [ $# -gt 0 ] ; do
        tempip=$1
        shift
        scp $file ${user}@${tempip}:${targetpath}
    done

else

    echo "wrong file!"

    exit 0
 fi

猜您喜欢

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们