1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > linux创建脚本文件auto linux 自动化部署脚本

linux创建脚本文件auto linux 自动化部署脚本

时间:2018-11-08 11:10:46

相关推荐

linux创建脚本文件auto linux 自动化部署脚本

1 概述

在工作中,需要对环境中的机器打补丁或者安装软件。如果机器太多,有可能会漏掉机器,或者有些机器上版本不一致。如果能实现同一的部署,不仅能降低人为导致的错误,而且能大大提高工作效率

本文,我将介绍通过crontab设置定时任务,来实现自动化部署安装脚本,只需将脚本放在指定的路径下,就可以实现统一部署

2 环境准备

环境如下

A 机器,服务器ip: 192.168.32.61

客户端: 一台CentOS6 :192.168.32.62

四台CentOS7 :ip 192.168.32.71 和 ip 192.168.32.72和 192.168.32.73和192.168.32.75

注意 expect命令在CentOS6上没有实现静默拷贝,测试拷贝不成功,原因是我的机器上CentOS上有多网卡影响导致下发异常,关闭其他网卡就正常运行脚本,所以CentOS6或7都可以部署,这里服务器选择,ip 192.168.32.61。

设置主机是如果是CentOS6机器,要把桥接模式的网卡关掉,使用仅主机,否则expect下发脚本会出错。

设置计划任务,建议用crontab -e来设置,而不直接用编辑crontab文件来操作。可能脚本运行不成功。

B 指定脚本放置的文件夹

服务器端设置创建两个文件夹

第一个sharedisk/script/autoproject,这个路径用放置需要自动部署的脚本的路径,这个路径下的.sh的脚本,一旦被下载到服务器端后,就会被从命名为.bak的文件,这些.bak的文件,如果不需要用,可以定时做清理或者放到其他路径下。

第二个文件夹 /sharedisk/script/autoscript 放置自动下发脚本

客户端创建两个文件夹

第一个文件夹 /root/autoscript 用来临时存放服务器端传过来的脚本

第二个文件夹 /root/autoscript.bak 将运行完成后的脚本备份到这个路径,除了自动运行脚本autorun.sh不要清理,其他备份的脚本可以定时清理

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: -08-30

#FileName: sendscript.sh

#version: 1.0

#Your change info:

#Description:For sending script to other host by auto

#Copyright(C): All rihts reserved

#*****************************************************************************

net="192.168.32"

scriptpath=/sharedisk/script/autoscript

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 62 71 72 73 75;do

if ping -c 1 -W 1 $net.$ip &>/dev/null;then

expect -c "

spawn scp $scriptpath/$script root@$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: -08-30

#FileName: autorun.sh

#version: 1.0

#Your change info:

#Description:For run script under autoscript by cron schedule

#Copyright(C): 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: -08-30

#FileName: sendscript.sh

#version: 1.0

#Your change info:

#Description:For sending script to other host by manual

#Copyright(C): 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 root@$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 附录

4 总结

通过以上的脚本,可以实现脚本的自动化安装,其中脚本中的路径或者文件夹可以自己根据调整变量进行设置。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。