前提是要执行的用户做好互信
创建ssh.sh脚本(批量互信脚本)
cat ssh.sh

  1. #!/bin/bash
  2. #生成密钥文件authorized_keys
  3. #在脚本目录下新建ip.list文本文件
  4. #录入信息格式如下:(ip 账户 密码)
  5. #此shell每次运行将重新生成秘钥,需要将全部的主机信息添加至ip.list
  6. #如果需要新增主机,将主机信息录入至ip.list(清空再录入)之后,注释shell中的 #ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa
  7. #172.19.168.100 csap 2wsx@WSX
  8. #172.19.168.101 csap 2wsx@WSX
  9. #172.19.168.102 csap 2wsx@WSX
  10. #172.19.168.103 csap 2wsx@WSX
  11. #注:运行启动后,第一个(y/n)?需要手工输入 y
  12. ssh-keygen -t rsa -P '' -f /home/ocdp/.ssh/id_rsa
  13. cat ip.list|while read line
  14. do
  15. ip=`echo $line|cut -d' ' -f1`
  16. user=`echo $line|cut -d' ' -f2`
  17. passwd=`echo $line|cut -d' ' -f3`
  18. #将生成的公钥文件id_rsa.pub 拷贝至集群其他Ambari Agent主机,并加入Ambari Agent的授权列表
  19. expect -c "
  20. spawn ssh-copy-id -i /home/ocdp/.ssh/id_rsa.pub $user@$ip
  21. expect {
  22. \"*assword:\" {send \"$passwd\r\";}
  23. \"yes/no\" {send \"yes\r\";}
  24. \"Enter file in which to save the key*\" {send \"\r\";}
  25. \"Enter passphrase*\" {send \"\r\";}
  26. \"Enter same passphrase again:\" {send \"\r\";}
  27. \"Overwrite (y/n)\" {send \"\r\";}
  28. }
  29. expect eof"
  30. done

创建ip.list(ip列表)
cat ip.list

  1. 10.170.29.11 ocdp UiK77gzMqkmpQuoZ5D7xiBOUBFJF0@QJ
  2. 10.170.29.12 ocdp UiK77gzMqkmpQuoZ5D7xiBOUBFJF0@QJ
  3. 10.170.29.13 ocdp UiK77gzMqkmpQuoZ5D7xiBOUBFJF0@QJ
  4. 10.170.29.14 ocdp UiK77gzMqkmpQuoZ5D7xiBOUBFJF0@QJ
  5. 10.170.29.15 ocdp UiK77gzMqkmpQuoZ5D7xiBOUBFJF0@QJ
  6. 10.170.29.16 ocdp UiK77gzMqkmpQuoZ5D7xiBOUBFJF0@QJ
  7. 10.170.29.17 ocdp UiK77gzMqkmpQuoZ5D7xiBOUBFJF0@QJ
  8. 10.170.29.18 ocdp UiK77gzMqkmpQuoZ5D7xiBOUBFJF0@QJ
  9. 10.170.29.19 ocdp UiK77gzMqkmpQuoZ5D7xiBOUBFJF0@QJ
  10. 10.170.29.20 ocdp UiK77gzMqkmpQuoZ5D7xiBOUBFJF0@QJ
  11. 10.170.29.21 ocdp UiK77gzMqkmpQuoZ5D7xiBOUBFJF0@QJ
  12. 10.170.29.22 ocdp UiK77gzMqkmpQuoZ5D7xiBOUBFJF0@QJ
  13. 10.170.29.23 ocdp UiK77gzMqkmpQuoZ5D7xiBOUBFJF0@QJ
  14. 10.170.29.24 ocdp UiK77gzMqkmpQuoZ5D7xiBOUBFJF0@QJ
  15. 10.170.29.25 ocdp UiK77gzMqkmpQuoZ5D7xiBOUBFJF0@QJ
  16. 10.170.29.26 ocdp UiK77gzMqkmpQuoZ5D7xiBOUBFJF0@QJ
  17. 10.170.29.27 ocdp UiK77gzMqkmpQuoZ5D7xiBOUBFJF0@QJ
  18. 10.170.29.28 ocdp UiK77gzMqkmpQuoZ5D7xiBOUBFJF0@QJ
  19. 10.170.29.29 ocdp UiK77gzMqkmpQuoZ5D7xiBOUBFJF0@QJ
  20. 10.170.29.30 ocdp UiK77gzMqkmpQuoZ5D7xiBOUBFJF0@QJ
  21. 10.170.29.31 ocdp UiK77gzMqkmpQuoZ5D7xiBOUBFJF0@QJ
  22. 10.170.29.32 ocdp UiK77gzMqkmpQuoZ5D7xiBOUBFJF0@QJ
  23. 10.170.29.33 ocdp UiK77gzMqkmpQuoZ5D7xiBOUBFJF0@QJ
  24. 10.170.29.34 ocdp UiK77gzMqkmpQuoZ5D7xiBOUBFJF0@QJ
  25. 10.170.29.35 ocdp UiK77gzMqkmpQuoZ5D7xiBOUBFJF0@QJ
  26. 10.170.29.36 ocdp UiK77gzMqkmpQuoZ5D7xiBOUBFJF0@QJ
  27. 10.170.29.37 ocdp UiK77gzMqkmpQuoZ5D7xiBOUBFJF0@QJ
  28. 10.170.29.38 ocdp UiK77gzMqkmpQuoZ5D7xiBOUBFJF0@QJ
  29. 10.170.29.39 ocdp UiK77gzMqkmpQuoZ5D7xiBOUBFJF0@QJ

创建runRemoteCmd.sh脚本(批量执行脚本)

cat runRemoteCmd.sh

  1. #!/bin/bash
  2. #set -x
  3. bin=`dirname "$0"`
  4. bin=`cd "$bin"; pwd`
  5. if [ $# -lt 2 ]
  6. then
  7. echo "Usage: ./runRemoteCmd.sh Command MachineTag"
  8. echo "Usage: ./runRemoteCmd.sh Command MachineTag confFile"
  9. exit
  10. fi
  11. cmd=$1
  12. tag=$2
  13. if [ 'a'$3'a' == 'aa' ]
  14. then
  15. confFile="$bin"/deploy.conf
  16. else
  17. confFile=$
  18. fi
  19. if [ -f $confFile ]
  20. then
  21. for server in `cat $confFile|grep -v '^#'|grep ','$tag','|awk -F',' '{print $1}'`
  22. do
  23. echo "*******************$server***************************"
  24. ssh $server "source ~/.bash_profile; $cmd"
  25. done
  26. else
  27. echo "Error: Please assign config file or run deploy.sh command with deploy.conf in same directory"
  28. fi

用法:sh runRemoteCmd.sh ‘命令’ all
例如:sh runRemoteCmd.sh ‘ls -ll’ all
sh runRemoteCmd.sh ‘ls -ll’ dn

创建deploy.sh脚本(批量分发脚本)

cat deploy.sh

#!/bin/bash
#set -x

bin=`dirname "$0"`
bin=`cd "$bin"; pwd`

if [ $# -lt 3 ]
then
  echo "Usage: ./deply.sh srcFile(or Dir) descFile(or Dir) MachineTag" 
  echo "Usage: ./deply.sh srcFile(or Dir) descFile(or Dir) MachineTag confFile" 
  exit
fi

src=$1
dest=$2
tag=$3
#me=`hostname`
if [ 'a'$4'a' == 'aa' ]
then
  confFile="$bin"/deploy.conf
else
  confFile=$4
fi

if [ -f $confFile ]
then
  if [ -f $src ]
  then
    for server in `cat $confFile|grep -v '^#'|grep ','$tag','|awk -F',' '{print $1}'`
    do
       scp $src $server":"${dest}
    done
  elif [ -d $src ]
  then
    for server in `cat $confFile|grep -v '^#'|grep ','$tag','|awk -F',' '{print $1}'`
    do
       scp -r $src $server":"${dest}
    done
  else
      echo "Error: No source file exist" 
  fi

else
  echo "Error: Please assign config file or run deploy.sh command with deploy.conf in same directory" 
fi

用法:sh deploy.sh 要分发的文件 目标文件 all
例如:sh deploy.sh /root/lxl/a.txt /root/lxl/ all

创建配置文件deploy.conf

cat deploy.conf

172.19.204.11,all,dn,nn
172.19.204.12,all,dn,nn
172.19.204.13,all,dn,
172.19.204.14,all,dn,
172.19.204.15,all,dn,
172.19.204.16,all,dn,
172.19.204.17,all,dn,
172.19.204.18,all,dn,
172.19.204.19,all,dn,
172.19.204.20,all,dn,
172.19.204.21,all,dn,