shell脚本批量生成配置文件
如果管理的站点和服务器较多的情况下,每次修改配置文件都相当痛苦。因而想到了用shell脚本来批量生成配置文件和配置数据。下面这个脚本是为了批量生成nagios监控配置文件的一个shell脚本程序。其原理是事先定义一个shell脚本模板,然后每个需要监控的站点复制一份模板替换掉模板文件里面的变量。
1、准备模板文件webcheck.template
more webcheck.template ###################WEBURL define start################### define service{ use generic-service ; Name of service template to use host_name webcheck service_description WEBURL check_command check_webpage!-H WEBURL -u INDEX is_volatile 0 max_check_attempts 3 check_interval 1 retry_interval 1 check_period 24x7 notification_interval 5 notification_period 24x7 notification_options w,u,r,c contact_groups admins } ###################WEBURL define end###################
变量为WEBURL和INDEX
2、站点列表文件weblist.txt
www.aaa.com \\/ bbs.bbb.com \\/ www.ccc.com \\/
weblist.txt有两个field,第一个field为域名,第二个field为站点对应的url。如第一个域名为www.aaa.com/
3、批量生成脚本文件create.sh
[root@bogon webcheckes]# more create.sh #!/bin/bash PATH=/bin:$PATH:$HOME/bin:/sbin:/usr/bin:/usr/sbin export PATH #echo $PATH usage () { echo -en "USAGE: $0 [web list] or $0 [template] [web list]\nFor example: $0 host.template host.list(Field : [WEB URL] [INDEX WEB PAGE])\n" 1>&2 exit 1 } if [ $# -gt 2 ];then usage exit 1 fi case "$#" in 2) template=$1 host_list=$2 ;; 1) template='webcheck.template' host_list=$1 ;; 0) # template='webcheck.template' # host_list='host.list' usage ;; esac if [ ! -f "${template}" ];then echo "template : ${template} not exist!" 1>&2 exit 1 fi if [ ! -f "${host_list}" ];then echo "host list : ${host_list} not exist!" 1>&2 exit 1 fi #echo $PWD/${host_list} WEBTEMP="wcalltemp.txt" rm $PWD/${WEBTEMP} #cat $PWD/${host_list} /bin/cat $PWD/${host_list}| while read weburl index do #echo "${ip}"|grep -oP '^\d{1,3}(\.\d{1,3}){3}$' >/dev/null 2>&1 || Field='not ip' #if [ "${Field}" = 'not ip' ];then # echo "${ip} not ip!" 1>&2 # exit 1 #fi #host_cfg="${hostname}-${ip}.cfg" tmppage="webtemp.txt" cp ${template} ${tmppage} sed -i "s/WEBURL/${weburl}/g;s/INDEX/${index}/g" ${tmppage} /bin/cat ${tmppage}>>${WEBTEMP} done /bin/cat webcheck_org.template>webcheck_${host_list}.cfg /bin/cat ${WEBTEMP}>>webcheck_${host_list}.cfg rm $PWD/${WEBTEMP} /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg service nagios restart
起作用的主要是这句,sed -i "s/WEBURL/${weburl}/g;s/INDEX/${index}/g" ${tmppage},说到底是sed命令的功劳。将weblist.txt里面的内容替换掉模板里的WEBURL和INDEX变量。
4、调用方式
sh ./create.sh webcheck.template weblist.txt
或者
sh ./create.sh weblist.txt
如果存在大量需要手工修改配置文件的情况下,或者批量生成一些类似的文件时可以考虑采用此种方式。
扫描公众号,关注更多信息
---------------------------------------------------------------------------------我是分割线--------------------------------------------------------------------------to be a better me, talk is cheap show me the code
版权所有,转载请注明原文链接。
文中有不妥或者错误的地方还望指出,以免误人子弟。如果觉得本文对你有所帮助不妨【推荐】一下!如果你有更好的建议,可以给我留言讨论,共同进步!
再次感谢您耐心的读完本篇文章。
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?