zoukankan      html  css  js  c++  java
  • shell 脚本同时对远程多台机器执行命令

    脚本1:需要机器之间免密

    ssh-copy-id [-i [identity_file]] [user@]machine
    #!/bin/bash
    # -------------------------------------------------------------------------------
    # Author:   Loya.Chen
    # Description: Execute commands on multiple remote hosts at the same time.
    # -------------------------------------------------------------------------------
    set -e
    Usage() {
      echo "Usage: $0 host1 host2 ... 'command'"
    }
    if [ $# -lt 2 ] ;then
      Usage
      exit 0
    else
      cmd=${!#}
    fi
    logfile=$(mktemp)
    i=1
    success=0
    failed=0
    for ip in $@;do
      if [ $i -eq $# ];then
        break
      fi
      ssh $ip $cmd &> $logfile
      if [ $? -eq 0 ];then
        #((success++))
        success=$(($success+1))
        echo -e "
    33[32m$ip | success 33[0m 
    "
        cat $logfile
      else
        ((failed++))
        echo -e "
    33[31m$ip | failed 33[0m
     "
        cat $logfile
      fi
      ((i++))
    done
    echo -e '
    -------------------------'
    echo -e "33[32msuccess: $success | failed: $failed 33[0m"
    echo '-------------------------'

    方法2:这种方式可以不用免密,但是需要在执行命令的机器用户名和密码一致

    #!/bin/bash
    # -------------------------------------------------------------------------------
    # Author:   JiangTao.Yu
    # Description: Execute commands on multiple remote hosts at the same time.
    # -------------------------------------------------------------------------------
    set -e
    Usage() {
      echo "Usage: $0 host1 host2 ... 'command'"
    }
    if [ $# -lt 2 ] ;then
      Usage
      exit 0
    else
      cmd=${!#}
    fi
    logfile=$(mktemp)
    i=1
    success=0
    failed=0
    for ip in $@;do
      if [ $i -eq $# ];then
        break
      fi
      sshpass -p "mypasswd" ssh -o "StrictHostKeyChecking no" hduser@$ip $cmd &> $logfile
      if [ $? -eq 0 ];then
        #((success++))
        success=$(($success+1))
        echo -e "
    33[32m$ip | success 33[0m 
    "
        cat $logfile
      else
        ((failed++))
        echo -e "
    33[31m$ip | failed 33[0m
     "
        cat $logfile
      fi
      ((i++))
    done
    echo -e '
    -------------------------'
    echo -e "33[32msuccess: $success | failed: $failed 33[0m"
    echo '-------------------------'

     方法3、从文件读取ip、username、passwd

    #!/bin/bash
    set -e
    success=0
    failed=0
    while read  ip username passwd
    do
       ip=$ip
       username=$username
       passwd=$passwd
       sshpass -p $passwd  ssh -n -o "StrictHostKeyChecking no" $username@$ip "free -m"
       if [ $? -eq 0 ];then
          success=$(($success+1))
          echo -e "
    33[32m$ip | success33[0m
    " 
       else
           failed=$(($failed+1))
           echo -e "
    33[32m$ip | failed33[0m
    " 
       fi
    
    done < ip_user_passwd.txt
    echo -e '
    -------------------------'
    echo -e "33[32msuccess: $success | failed: $failed 33[0m"
    

     

    [root@test ~]# cat ip_user_passwd.txt
    192.168.0.235   hduser  mypasswd
    192.168.4.50    root    123456
    192.168.4.50    root    123456
    

      

    借鉴:https://www.jb51.net/article/119541.htm

  • 相关阅读:
    纯C分割 字符串 devC++版本 vs各种奇怪的问题,
    C的字符串操作 split
    MyArray框架搭建与实现
    三目运算符(C++)
    ER_NOT_SUPPORTED_AUTH_MODE: Client does not support authentication protocol requested by server; consider upgrading MySQL client
    sqlServer not in 分页
    web 移动端开发手机查看效果
    webpack 安装babel处理ES6语法
    webpack 的环境搭建
    web 服务器添加数据到 SQL server
  • 原文地址:https://www.cnblogs.com/yjt1993/p/10283110.html
Copyright © 2011-2022 走看看