1. 程式人生 > >linux 自動化部署腳本

linux 自動化部署腳本

linux 自動化 部署腳本

1 概述

在工作中,需要對環境中的機器打補丁或者安裝軟件。如果機器太多,有可能會漏掉機器,或者有些機器上版本不一致。如果能實現同一的部署,不僅能降低人為導致的錯誤,而且能大大提高工作效率

本文,我將介紹通過crontab設置定時任務,來實現自動化部署安裝腳本,只需將腳本放在指定的路徑下,就可以實現統一部署

2 環境準備

環境如下

A 環境中機器

服務器端: 192.168.32.75,

客戶端: 兩臺CentOS6 :ip 192.168.32.61 和 ip 192.168.32.62 三臺CentOS7 :ip 192.168.32.71 和 ip 192.168.32.72和 192.168.32.73

B 因為expect命令在CentOS6上沒有實現靜默拷貝,測試拷貝不成功,所以我就選擇把CentOS7作為部署的服務器,ip 192.168.32.75。

C 指定腳本放置的文件夾

服務器端設置創建兩個文件夾

第一個 /root/autoln,這個路徑用放置需要自動部署的腳本的路徑,這個路徑下的.sh的腳本,一旦被下載到服務器端後,就會被從命名為.bak的文件,這些.bak的文件,如果不需要用,可以定時做清理或者放到其他路徑下。

第二個文件夾 /root/autoscript.bak 放置自動下發腳本

客戶端創建兩個文件夾

第一個文件夾 /root/autoscript 用來臨時存放服務器端傳過來的腳本

第二個文件夾 /root/autoscript.bak 將運行完成後的腳本備份到這個路徑,除了定時運行腳本不要清理,其他備份的腳本可以定時清理

C 涉及三個腳本

autosend.sh 部署在服務器端,將服務器路徑/root/autoln 下的腳本自動下發到環境中的客戶端

這個腳本通過crontab 設置自動運行,這裏時間不能太短,太短的話,比如1分鐘,還在第一分鐘執行的任務還沒完成,第二分鐘的任務就已經開始了。所以這個任務時間需要根據下發的情況來定。在本次實驗環境中,經測試,三個腳本,兩分鐘內是比較合適的。

腳本路徑放在/root/autoscript.sh下

服務器端自動運行設置如下

crontab -e
*/2 * * * *  /root/autoscript.bak/autosend.sh

autorun.sh 部署在客戶端,執行指定路徑下的腳本

這個腳本通過crontab 設置自動運行,時間也不能設置太頻繁,會對系統造成負擔,本次實驗設置為三分鐘運行一次腳本

客戶端自動運行設置如下

crontab -e
*/3 * * * * /root/autoscript.bak/autorun.sh

sendscript.sh 這腳本實現半自動化,將腳本總路徑變量scriptpath路徑下的文件,根據管理者的需求,輸入該路徑下指定的.sh文件後,這個路徑下的文件會被下發到客戶端。客戶端會通過自動安裝腳本進行安裝。該腳本路徑不要求。

3 腳本內容

3.1 服務器端自動下發腳本

autosend.sh 腳本內容如下

#!/bin/bash
#
#******************************************************************************
#Author:               Sunny
#Date:                 2017-08-30
#FileName:             sendscript.sh
#version:              1.0
#Your change info:     
#Description:          For sending script to other host by auto
#Copyright(C):         2017  All rihts reserved
#*****************************************************************************
net="192.168.32"
scriptpath=/root/autoln
dstpath=/root/autoscript/
date=`date +%F-%H-%M`
cd $scriptpath
scriptnu=`ls | grep -e .sh$ | wc -l`
if [ $scriptnu -eq 0 ];then
#   echo "nothing to do"
   exit 8
else
for  script in *.sh;do
for ip in 61 62 71 72 73;do
  if ping -c 1 -W 1  $net.$ip &>/dev/null;then
  
expect -c "
spawn scp  $scriptpath/$script  [email protected]$net.$ip:$dstpath
expect {
\"*assword\" {set timeout 300; send \"xxxxxxxx\r\"; }
\"yes/no\" { send \"yes\r\"; exp_continue; }
}
expect efo"&>/dev/null
   if [ $? -eq 0 ];then
echo "$script had been send to $net.$ip:$dstpath"
wall "$script had been send to $net.$ip:$dstpath"
else 
echo "$script did not send to $net.$ip:$dstpath,please check"
wall "$script did not send to $net.$ip:$dstpath,please check"
fi
   else 
     wall "$net.$ip is down,please check"
 echo "skip the host,task continue"
 continue
  fi
done
mv $script "$script".$date.bak
done
fi
echo "Congratulation!"
unset net
unset scriptpath
unset dstpath
unset script
exit

3.2 客戶端自動安裝腳本

autorun.sh腳本內容如下

#!/bin/bash
#
#******************************************************************************
#Author:               Sunny
#Date:                 2017-08-30
#FileName:             autorun.sh
#version:              1.0
#Your change info:     
#Description:          For run script under autoscript by cron schedule
#Copyright(C):         2017  All rihts reserved
#*****************************************************************************
path=/root/autoscript
bakdir=/root/autoscript.bak
date=`date +%F-%H-%M`
cd $path
scriptnu=`ls | grep .sh | wc -l`
if [ $scriptnu -eq 0 ];then
#   echo "nothing to do"
   exit 8
else
for script in *.sh;do
    echo "$script exist"
$path/$script
mv $script $bakdir/"$script"."$date".bak
wall "script $script done"
done
fi
unset path
unset bakdir
unset date
unset scriptnu
exit

3.3 半自動化下發腳本

sendscript.sh腳本內容如下

#!/bin/bash
#
#******************************************************************************
#Author:               Sunny
#Date:                 2017-08-30
#FileName:             sendscript.sh
#version:              1.0
#Your change info:     
#Description:          For sending script to other host by manual
#Copyright(C):         2017  All rihts reserved
#*****************************************************************************
net="192.168.32"
scriptpath=/root/script
dstpath=/root/autoscript/
read -p "Please input script name you want to send under dir autoscript: " script
if [ -z "$script" ];then
    echo "Your input is nothing,please re-input"
      exit 6
  elif [ -e "$scriptpath"/"$script" ];then
    echo "The $script is exist,it will be sent"
   else 
   echo "$script does not exit,please check"
        exit 8
fi
for ip in 61 62 71 72 73;do
  if ping -c 1 -W 1  $net.$ip &>/dev/null;then
  
expect -c "
spawn scp  $scriptpath/$script  [email protected]$net.$ip:$dstpath
expect {
\"*assword\" {set timeout 300; send \"xxxxxxxx\r\"; }
\"yes/no\" { send \"yes\r\"; exp_continue; }
}
expect efo"&>/dev/null
   if [ $? -eq 0 ];then
echo "$script had been send to $net.$ip:$dstpath"
else 
echo "$script did not send to $net.$ip:$dstpath,please check"
fi
  else 
     echo "$net.$ip is down,please check"
 echo "skip the host,task continue"
 continue
  fi
echo "Congratulation!"
done
unset net
unset scriptpath
unset dstpath
unset script
exit

4 總結

通過以上的腳本,可以實現腳本的自動化安裝,其中腳本中的路徑或者文件夾可以自己根據調整變量進行設置。

本文出自 “自學linux” 博客,請務必保留此出處http://ghbsunny.blog.51cto.com/7759574/1961159

linux 自動化部署腳本