博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
shell脚本批量生成配置文件
阅读量:7123 次
发布时间:2019-06-28

本文共 2952 字,大约阅读时间需要 9 分钟。

  如果管理的站点和服务器较多的情况下,每次修改配置文件都相当痛苦。因而想到了用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 $PATHusage () {         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 indexdo         #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}.cfgrm $PWD/${WEBTEMP}/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfgservice 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

  如果存在大量需要手工修改配置文件的情况下,或者批量生成一些类似的文件时可以考虑采用此种方式。

转载地址:http://ooxel.baihongyu.com/

你可能感兴趣的文章
《易学C++(第2版)》——1.7 C++学习的常见问题
查看>>
《Google软件测试之道》—第1章1.3节组织结构
查看>>
jvm系列(七):jvm调优-工具篇
查看>>
Processing编程学习指南3.1 程序的运行流程
查看>>
ROS机器人程序设计(原书第2版)2.2 理解ROS计算图级
查看>>
《破茧成蝶——用户体验设计师的成长之路》一1.3 用户体验设计的特征
查看>>
R语言数据挖掘2.2.4.3 R语言实现
查看>>
Predicate和Consumer接口– Java 8中java.util.function包下的接口
查看>>
《Dreamweaver CS6完美网页制作——基础、实例与技巧从入门到精通》——2.3 网页色彩搭配知识...
查看>>
企业上云实战分享
查看>>
SSM框架Web程序的流程(Spring SpringMVC Mybatis)
查看>>
阿里云人工智能识别篮球动作视频
查看>>
Ali Kernel introduction
查看>>
前端常见兼容问题系列5:¥符号在部分Android APP的WebView中不见了
查看>>
基于Reactjs实现webapp(加精)
查看>>
超强、超详细Redis数据库入门教程
查看>>
《C++语言基础》实践项目——多重继承
查看>>
京颐集团上云之路:如何助力中小型医疗行业信息化与全面上云?
查看>>
Python yield与实现
查看>>
mongodb一些使用技巧或注意事项记录
查看>>