博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Shell脚本修改Nginx upstream配置文件
阅读量:5116 次
发布时间:2019-06-13

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

#!/bin/bash######################################################    Name:           change_nginx_upstream_conf.sh#    Version:        V1.0#    Author:         运维菜鸟#    Description:    更改nginx upstream配置文件    #    Create Date:    2017-07-03#    Email:            ######################################################function namefunction_name=$1#pool namepool_name=$2#pool corresponding ip listpool_ip_lists=$3#pool corresponding tomcat portpool_tomcat_port=$4#upstream file locationngx_upstream_file=$5#检测pool在nginx upstream配置文件中是否存在function check_pool_in_ngx_upstream() {    grep -E "${pool_name}[^-]" ${ngx_upstream_file} >> /dev/null    if [ $? -eq 0 ];then        echo -e "\033[36m the ${pool_name} in ${ngx_upstream_file}. \033[0m"    else        echo -e "\033[31m the ${pool_name} not in ${ngx_upstream_file}. \033[0m"        exit 1    fi}#显示pool在nginx upstream配置文件中对应内容function show_pool_in_ngx_upstream() {    pool_name_first_line=`egrep -n "${pool_name}[^-]" ${ngx_upstream_file} | cut -d ":" -f1`    line_list=`grep -n "^}" ${ngx_upstream_file} | cut -d ":" -f1`    pool_name_end_line=${pool_name_first_line}    for line in ${line_list[*]};do        if [ $line -gt ${pool_name_first_line} ];then            pool_name_end_line=${line}            break;        fi    done    sed -n "${pool_name_first_line},${pool_name_end_line}p" ${ngx_upstream_file}}#增加pool进nginx upstream配置文件function add_pool_to_upstream() {    #pool对应ip地址列表,多个ip以逗号改开    pool_ip=`awk 'BEGIN{list="'${pool_ip_lists}'";split(list,ip_list,",");for(ip in ip_list){print ip_list[ip];}}'`    for ip in ${pool_ip[*]};do        echo "add ${pool_name} ${ip} in ${ngx_upstream_file}"        sed -i '/upstream '${pool_name}'[^-]*{/a\\tserver '${ip}':'${pool_tomcat_port}';' ${ngx_upstream_file}    done    echo -e "\033[31m ====添加完成如下:==== \033[0m"}#在nginx upstream配置文件删除pool对应的ip地址function delete_ip_from_upstream() {    pool_name_first_line=`egrep -n "${pool_name}[^-]" ${ngx_upstream_file} | cut -d ":" -f1`    line_list=`grep -n "^}" ${ngx_upstream_file} | cut -d ":" -f1`    pool_name_end_line=${pool_name_first_line}    for line in ${line_list[*]};do        if [ $line -gt ${pool_name_first_line} ];then            pool_name_end_line=${line}            break;        fi    done    #获取pool对应配置行数    line_count=`sed -n "${pool_name_first_line},${pool_name_end_line}p" ${ngx_upstream_file} | wc -l`    #如果某个pool的配置行数等于3,则不能进行删除操作    if [ ${line_count} -eq 3 ];then        echo -e "\033[31m this is lowest configure. \033[0m"    fi    #删除pool_ip_lists中包含的ip地址    for ((i=${pool_name_first_line};i<=${pool_name_end_line};i++));do        pool_ip=`awk 'BEGIN{list="'${pool_ip_lists}'";split(list,ip_list,",");for(ip in ip_list){print ip_list[ip];}}'`        line_context=`sed -n ''${i}'p' ${ngx_upstream_file}`        for ip in ${pool_ip[*]};do            echo "this line ${line_context} has ${ip}" | egrep "${ip}:${pool_tomcat_port}"            if [ $? -eq 0 ];then                #将包含删除ip的行,替换为空行                sed -i ''${i}'s/.*'${ip}':'${pool_tomcat_port}'.*//ig' ${ngx_upstream_file}                #sed -i ''${i}'d' ${ngx_upstream_file}                echo -e "\033[36m delete ${pool_name} from ${ngx_upstream_file} where ip = ${ip}. \033[0m"            fi        done    done    #删除文件中的空行    sed -i '/^$/d' ${ngx_upstream_file}    echo -e "\033[31m ====删除完成如下:==== \033[0m"}#调用方法if [ $# -eq 5 ];then    case $1 in        add)            check_pool_in_ngx_upstream;            show_pool_in_ngx_upstream;            add_pool_to_upstream;            show_pool_in_ngx_upstream;            ;;        delete)            check_pool_in_ngx_upstream;            show_pool_in_ngx_upstream;            delete_ip_from_upstream;            show_pool_in_ngx_upstream;            ;;        *)            $"Usage: {sh change_nginx_upstream_conf.sh add chat-frontier-web 10.10.13.194 8080 /etc/nginx/conf.d/upstream.conf|sh change_nginx_upstream_conf.sh add chat-frontier-web 10.10.13.194 8080 /etc/nginx/conf.d/upstream.conf}"            exit 3    esacelse    echo "variables count not eq 5.please check the usage."fi

 

转载于:https://www.cnblogs.com/crysmile/p/7116421.html

你可能感兴趣的文章
js window.open 参数设置
查看>>
032. asp.netWeb用户控件之一初识用户控件并为其自定义属性
查看>>
Ubuntu下安装MySQL及简单操作
查看>>
前端监控
查看>>
clipboard.js使用方法
查看>>
移动开发平台-应用之星app制作教程
查看>>
leetcode 459. 重复的子字符串(Repeated Substring Pattern)
查看>>
伪类与超链接
查看>>
centos 7 redis-4.0.11 主从
查看>>
博弈论 从懵逼到入门 详解
查看>>
永远的动漫,梦想在,就有远方
查看>>
springboot No Identifier specified for entity的解决办法
查看>>
慵懒中长大的人,只会挨生活留下的耳光
查看>>
"远程桌面连接--“发生身份验证错误。要求的函数不受支持
查看>>
【BZOJ1565】 植物大战僵尸
查看>>
VALSE2019总结(4)-主题报告
查看>>
浅谈 unix, linux, ios, android 区别和联系
查看>>
51nod 1428 活动安排问题 (贪心+优先队列)
查看>>
中国烧鹅系列:利用烧鹅自动执行SD卡上的自定义程序(含视频)
查看>>
Solaris11修改主机名
查看>>