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 "\033[31m`date` ERROR : $message \033[0m"
echo -e "\033[31m`date` ERROR : $message \033[0m">>autoplog
;;
INFO) echo -e "\033[32m`date` INFO : $message \033[0m"
echo -e "\033[32m`date` INFO : $message \033[0m">>autoplog
;;
FAILED) echo -e "\033[34m`date` FAILED : $message\033[0m"
echo -e "\033[34m`date` FAILED : $message \033[0m">>autoplog
;;
WARNING) echo -e "\033[33m `date` WARNING : $message\033[0m"
echo -e "\033[33m`date` WARNING : $message\033[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

 

posted @ 2017-08-03 10:51  QQQnull  阅读(262)  评论(0编辑  收藏  举报