1 overview
At work, you need to patch or install the software in your environment. If there are too many machines, it is possible to miss the machine, or the version of some machines is inconsistent. If you can achieve the same deployment, not only can reduce human error, but also greatly improve productivity
In this article, I'll show you how to set up a timed task with crontab to automate the deployment of the installation script by placing the script under the specified path for a unified deployment
2 Environment Preparation
The environment is as follows
A machine in the environment
Server side: 192.168.32.75,
Client: Two centos6:ip 192.168.32.61 and IP 192.168.32.62 three centos7:ip 192.168.32.71 and IP 192.168.32.72 and 192.168.32.73
B because the expect command did not implement a silent copy on the CentOS6, the test copy was unsuccessful, so I chose to CentOS7 as the deployed server, IP 192.168.32.75.
C Specifies the folder where the script is placed
Server-side settings Create two folders
The first/root/autoln, this path with the path to place the script that needs to be deployed automatically, this path of the. Sh script, once downloaded to the server side, will be named. bak files, these. bak files, if not needed, can be scheduled to be cleaned or placed under other paths.
The second folder/root/autoscript.bak placement of the auto-issued script
Client creates two folders
The first folder/root/autoscript used to temporarily store the server-side script.
The second folder/root/autoscript.bak will run the completed script back to this path, in addition to the scheduled run scripts do not clean up, other backup scripts can be scheduled cleanup
C involves three scripts
Autosend.sh deployed on the server side, the script under the server path/root/autoln is automatically distributed to clients in the environment
This script runs automatically through the crontab setup, where the time cannot be too short, too short, say 1 minutes, and the first minute of the task is not completed, and the two-minute task has already started. So the task time needs to be based on the situation to be determined. In this experimental environment, the test, three scripts, two minutes is more appropriate.
The script path is placed under/root/autoscript.sh
Server-side autorun settings are as follows
CRONTAB-E*/2 * * * */root/autoscript.bak/autosend.sh
Autorun.sh deployed on the client, executes the script under the specified path
This script is automatically run through the crontab settings, the time can not be set too frequently, will burden the system, the experiment set to three minutes to run the script
The client auto-run settings are as follows
CRONTAB-E*/3 * * * */root/autoscript.bak/autorun.sh
sendscript.sh this script to achieve semi-automation, the script will be the total path variable scriptpath path under the file, according to the requirements of the manager, enter the path specified under the. sh file, the file under this path will be issued to the client. The client is installed through an automatic installation script. The script path is not required.
3 Script Content
3.1 Server-side auto-issued script
autosend.sh script content is as follows
#!/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/autolndstpath=/root/autoscript/date = ' Date +%f-%h-%m ' CD  $scriptpathscriptnu = ' ls | grep -e .sh$ | wc -l ' if [ $scriptnu -eq 0 ];then# echo "Nothing to do" exit 8elsefor script in *.sh;dofor 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: $dstpathexpect {\ "*assword\" {set timeout 300; send \ "xxxxxxxx\r\"; }\ "Yes/no\" { send \ "yes\r\"; exp_continue; }}expect efo "&>/dev/null if [ $? -eq 0 ];thenecho "$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 fidonemv $script $script". $ date.bakdonefiecho "congratulation!" Unset netunset scriptpathunset dstpathunset scriptexit
3.2 Client Auto-install script
autorun.sh script content is as follows
#!/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/autoscriptbakdir=/root/autoscript.bakdate= ' date +%f- %h-%m ' cd $PATHSCRiptnu= ' ls | grep .sh | wc -l ' if [ $scriptnu -eq 0 ]; then# echo "Nothing to do" exit 8elsefor script in *.sh;do echo "$script exist" $path/$scriptmv $script $ bakdir/"$script". " $date ".bakwall " script $script done "Donefiunset pathunset bakdirunset dateunset scriptnuexit
3.3 Semi-automated scripts issued
sendscript.sh script content is as follows
#!/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/scriptdstpath=/root/ autoscript/read -p "Please input script name you want to send under dir autoscript: " scriptif [ -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 8fifor 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: $dstpathexpect {\ "*assword\" {set timeout 300; send \ "xxxxxxxx\r\"; }\ "yes/no\" { send \ "Yes\r\"; exp_continue; }}expect efo "&>/dev/null if [ $? -eq 0 ];thenecho "$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 fiecho " congratulation! " Doneunset netunset scriptpathunset dstpathunset scriptexit
4 Summary
Through the above script, you can automate the installation of scripts, where the path or folder in the script can be set according to the adjustment variables.
This article is from the "Self-learning Linux" blog, so be sure to keep this source http://ghbsunny.blog.51cto.com/7759574/1961159
Linux Automation Deployment Scripts