zoukankan      html  css  js  c++  java
  • shell自动打补丁

    #!/bin/bash
    
    #todo:运行前设置主机间SSH信任,将要更新的主机的IP放在mip中
    #变量设置
    #待更新主机ip存放在mip文件中
    if [ ! -f mip ]
    then
    echo "mip doesn't exist"
    exit 1
    fi
    ip=$(cat mip|awk '{print $1}')
    #远程登陆用户
    user="root"
    #目标文件
    tfile="/root/youfindthis"
    #目标路径
    tpath="/root"
    #更新文件
    pfile="/root/youupdate"
    #日志文件autoplog
    
    
    
    
    #获得总数并测试是否建立信任
    s=0
    for i in $ip
    do
    ssh -o NumberOfPasswordPrompts=0 -o StrictHostKeyChecking=no $i "echo"
    if [ $? -eq 255 ]
    then 
    ssh-copy-id $i
    fi
    s=`expr $s + 1`
    done
    
    
    ####LOG :log type message 日志记录
    log()
    {
    local type=$1
    local message=$2
    case $type in
    ERROR) echo -e "33[31m`date` ERROR : $message 33[0m"
    echo -e "33[31m`date` ERROR : $message 33[0m">>autoplog
    ;;
    INFO) echo -e "33[32m`date` INFO : $message 33[0m"
    echo -e "33[32m`date` INFO : $message 33[0m">>autoplog
    ;;
    FAILED) echo -e "33[34m`date` FAILED : $message33[0m"
    echo -e "33[34m`date` FAILED : $message 33[0m">>autoplog
    ;;
    WARNING) echo -e "33[33m `date` WARNING : $message33[0m"
    echo -e "33[33m`date` WARNING : $message33[0m">>autoplog
    ;;
    esac
    }
    
    ####### CHECK FILE######## checkfile user tfile ip 查找文件
    checkfile()
    {
    local user=$1
    local tfile=$2
    local i=$3
    check()
    {
    rm -f ."$i"findfin
    echo `ssh -t -p 22 $user@$i "if [ -f $tfile ]; then echo "exist";else echo "notexist";fi"`
    ssh -t -p 22 $user@$i "if [ -f $tfile ]; then echo "exist";else echo "notexist";fi"
    if [ $? -eq 0 ]
    then
    touch ."$i"findfin
    fi
    }
    local fexist=`check &`
    sleep 1
    if [ ! -f ."$i"findfin ]
    then
      log ERROR "$user@$i ssh link failed"
      rm -f ."$i"findfin
      return 1
    fi
    rm -f ."$i"findfin
    if [[ $fexist =~ "not" ]]
    then
      log FAILED "$user@$i :$tfile doesn't exist"
      return 2
    else
      log INFO "$user@$i :$tfile exists" 
      return 0
    fi
    }
    
    
    
    ####### BACK UP ######### backup user tfile ip 备份文件
    backup()
    {
    local user=$1
    local tfile=$2
    local i=$3
    bp()
    {
    rm -f ."$i"backupfin
    echo `ssh -t -p 22 $user@$i "if mv $tfile $tfile.backup; then echo succeed;else echo failed;fi;"`
    touch ."$i"backupfin
    }
    local backupresult=`bp &`
    sleep 1
    if [ ! -f  ."$i"backupfin ]
    then
      log ERROR "$user@$i ssh link failed"
      rm -f ."$i"backupfin
      return 1
    fi
    rm -f ."$i"backupfin
    if [[ $backupresult =~ "failed" ]]
    then
      log FAILED "$user@$i :$tfile backup failed!!!"
      return 1
    else
      log INFO "$user@$i :$tfile backup succeeded! $tfile.backup"
      return 0
    fi
    
    }
    
    
    
    #######DISTRIBUTE FILE######## distributefile user pfile ip tpath 发布文件 
    
    distributefile()
    {
    local user=$1
    local pfile=$2
    local i=$3
    local tpath=$4
    scp $pfile $user@$i:$tpath
    if [ $? -eq 0 ]
    then
      log INFO "$user@$i :$pfile update succeeded!"
      return 0
    else
      log ERROR "$user@$i :$pfile update failed!"  
      return 1
    fi
    }
    
    
    #######autopackage####### autopackage user pfile ip tfile tpath 主流程
    autopackage()
    {
    local user=$1
    local pfile=$2
    local i=$3
    local tfile=$4
    local tpath=$5
    checkfile $user $tfile $i
    if [ $? -eq 0 ]
    then
      backup $user $tfile $i 
      if [ $? -eq 0 ]
      then
        distributefile $user $pfile $i $tpath
        if [ $? -eq 0 ]
        then
          rm -f ."$i"updatefin
          touch ."$i"updatefin
          return 0
        else
          return 1
        fi
      else 
        return 2
      fi
    else
      return 3
    fi
    }
    
    
    ########  MAIN   ########
    if [ ! -f autoplog ]
    then
      touch autoplog
    fi
    if [ ! -f $pfile ]
    then
      log WARNING "$pfile doesn't exist!!!!!!"
      exit 1
    fi
    for i in $ip
      do
      autopackage $user $pfile $i $tfile $tpath &
      done
    wait
    #统计信息
    scount=0
    fcount=0
    rm -f .failedip
    touch .failedip
    for i in $ip
    do 
    if [ -f ."$i"updatefin ]
    then 
      let "scount+=1"
      rm -f ."$i"updatefin
    else
      echo $i>>.failedip
      let "fcount+=1"
    fi
    done
    echo
    echo "=====total:$s succeeded:$scount failed:$fcount====="
    if [ ! $fcount -eq 0 ]
    then
      echo
      echo "failed list"
      cat .failedip
    fi
    echo
    echo "log has been recorded in `pwd`/autoplog"
    echo
  • 相关阅读:
    常用电子元器件及应用
    局域网传输-LED灯搭建局域网:数据传输可达每秒3Gb
    硬件设计原理图Checklist 参考案例二 【转载】
    基于LiFi可见光通信技术的研究及应用转化调查
    T&F 圆桌:儿童智能玩具离我们还有多远?
    一座金矿:数千亿规模智能玩具市场
    【Java】+ 图片与base64互转 + 字符串与base64互转
    【Java】+ 【wss】 + WebSocketClient
    【IDEA】+SVN
    【杂项】+事件管理之时间4象限
  • 原文地址:https://www.cnblogs.com/QQQmadNULL/p/7278499.html
Copyright © 2011-2022 走看看