实践Shell脚步修改Nginx upstream配置文件

change_nginx_upstream_conf.sh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/bin/bash
#function name
function_name=$1
#pool name
pool_name=$2
#pool corresponding ip list, comma-seperated
pool_ip_lists=$3
#pool corresponding tomcat port
pool_tomcat_port=$4
#upstream file location
ngx_upstream_file=$5


#检测pool在nginx upstream配置文件中是否存在
function check_pool_in_ngx_upstream() {
    grep -E "upstream\s+${pool_name}\s+\{" ${ngx_upstream_file} >> /dev/null
    if [ $? -eq 0 ];then
        echo -e "
#!/bin/bash
#function name
function_name=$1
#pool name
pool_name=$2
#pool corresponding ip list, comma-seperated
pool_ip_lists=$3
#pool corresponding tomcat port
pool_tomcat_port=$4
#upstream file location
ngx_upstream_file=$5


#检测pool在nginx upstream配置文件中是否存在
function check_pool_in_ngx_upstream() {
    grep -E "upstream\s+${pool_name}\s+\{" ${ngx_upstream_file} >> /dev/null
    if [ $? -eq 0 ];then
        echo -e "\033[36m ${pool_name} in ${ngx_upstream_file}. \033[0m"
    else
        echo -e "\033[31m ${pool_name} not in ${ngx_upstream_file}. \033[0m"
        exit 1
    fi
}

#显示pool在nginx upstream配置文件中对应内容
function show_pool_in_ngx_upstream() {
    #pool name开始行
    pool_name_first_line=`egrep -n "upstream\s+${pool_name}\s+\{" ${ngx_upstream_file} | cut -d ":" -f1`
    #方法块结束行
    line_list=`grep -n "}" ${ngx_upstream_file} | cut -d ":" -f1`
    #pool name结束行
    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 -E -i '/upstream\s+'${pool_name}'\s+\{/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 "upstream\s+${pool_name}\s+\{" ${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
    esac
else
    echo "variables count not eq 5.please check the usage."
fi
nginx -t -c ${ngx_upstream_file}
33[36m ${pool_name} in ${ngx_upstream_file}.
#!/bin/bash
#function name
function_name=$1
#pool name
pool_name=$2
#pool corresponding ip list, comma-seperated
pool_ip_lists=$3
#pool corresponding tomcat port
pool_tomcat_port=$4
#upstream file location
ngx_upstream_file=$5


#检测pool在nginx upstream配置文件中是否存在
function check_pool_in_ngx_upstream() {
    grep -E "upstream\s+${pool_name}\s+\{" ${ngx_upstream_file} >> /dev/null
    if [ $? -eq 0 ];then
        echo -e "\033[36m ${pool_name} in ${ngx_upstream_file}. \033[0m"
    else
        echo -e "\033[31m ${pool_name} not in ${ngx_upstream_file}. \033[0m"
        exit 1
    fi
}

#显示pool在nginx upstream配置文件中对应内容
function show_pool_in_ngx_upstream() {
    #pool name开始行
    pool_name_first_line=`egrep -n "upstream\s+${pool_name}\s+\{" ${ngx_upstream_file} | cut -d ":" -f1`
    #方法块结束行
    line_list=`grep -n "}" ${ngx_upstream_file} | cut -d ":" -f1`
    #pool name结束行
    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 -E -i '/upstream\s+'${pool_name}'\s+\{/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 "upstream\s+${pool_name}\s+\{" ${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
    esac
else
    echo "variables count not eq 5.please check the usage."
fi
nginx -t -c ${ngx_upstream_file}
33[0m" else echo -e "
#!/bin/bash
#function name
function_name=$1
#pool name
pool_name=$2
#pool corresponding ip list, comma-seperated
pool_ip_lists=$3
#pool corresponding tomcat port
pool_tomcat_port=$4
#upstream file location
ngx_upstream_file=$5


#检测pool在nginx upstream配置文件中是否存在
function check_pool_in_ngx_upstream() {
    grep -E "upstream\s+${pool_name}\s+\{" ${ngx_upstream_file} >> /dev/null
    if [ $? -eq 0 ];then
        echo -e "\033[36m ${pool_name} in ${ngx_upstream_file}. \033[0m"
    else
        echo -e "\033[31m ${pool_name} not in ${ngx_upstream_file}. \033[0m"
        exit 1
    fi
}

#显示pool在nginx upstream配置文件中对应内容
function show_pool_in_ngx_upstream() {
    #pool name开始行
    pool_name_first_line=`egrep -n "upstream\s+${pool_name}\s+\{" ${ngx_upstream_file} | cut -d ":" -f1`
    #方法块结束行
    line_list=`grep -n "}" ${ngx_upstream_file} | cut -d ":" -f1`
    #pool name结束行
    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 -E -i '/upstream\s+'${pool_name}'\s+\{/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 "upstream\s+${pool_name}\s+\{" ${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
    esac
else
    echo "variables count not eq 5.please check the usage."
fi
nginx -t -c ${ngx_upstream_file}
33[31m ${pool_name} not in ${ngx_upstream_file}.
#!/bin/bash
#function name
function_name=$1
#pool name
pool_name=$2
#pool corresponding ip list, comma-seperated
pool_ip_lists=$3
#pool corresponding tomcat port
pool_tomcat_port=$4
#upstream file location
ngx_upstream_file=$5


#检测pool在nginx upstream配置文件中是否存在
function check_pool_in_ngx_upstream() {
    grep -E "upstream\s+${pool_name}\s+\{" ${ngx_upstream_file} >> /dev/null
    if [ $? -eq 0 ];then
        echo -e "\033[36m ${pool_name} in ${ngx_upstream_file}. \033[0m"
    else
        echo -e "\033[31m ${pool_name} not in ${ngx_upstream_file}. \033[0m"
        exit 1
    fi
}

#显示pool在nginx upstream配置文件中对应内容
function show_pool_in_ngx_upstream() {
    #pool name开始行
    pool_name_first_line=`egrep -n "upstream\s+${pool_name}\s+\{" ${ngx_upstream_file} | cut -d ":" -f1`
    #方法块结束行
    line_list=`grep -n "}" ${ngx_upstream_file} | cut -d ":" -f1`
    #pool name结束行
    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 -E -i '/upstream\s+'${pool_name}'\s+\{/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 "upstream\s+${pool_name}\s+\{" ${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
    esac
else
    echo "variables count not eq 5.please check the usage."
fi
nginx -t -c ${ngx_upstream_file}
33[0m" exit 1 fi } #显示pool在nginx upstream配置文件中对应内容 function show_pool_in_ngx_upstream() { #pool name开始行 pool_name_first_line=`egrep -n "upstream\s+${pool_name}\s+\{" ${ngx_upstream_file} | cut -d ":" -f1` #方法块结束行 line_list=`grep -n "}" ${ngx_upstream_file} | cut -d ":" -f1` #pool name结束行 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 -E -i '/upstream\s+'${pool_name}'\s+\{/a \tserver '${ip}':'${pool_tomcat_port}';' ${ngx_upstream_file} done echo -e "
#!/bin/bash
#function name
function_name=$1
#pool name
pool_name=$2
#pool corresponding ip list, comma-seperated
pool_ip_lists=$3
#pool corresponding tomcat port
pool_tomcat_port=$4
#upstream file location
ngx_upstream_file=$5


#检测pool在nginx upstream配置文件中是否存在
function check_pool_in_ngx_upstream() {
    grep -E "upstream\s+${pool_name}\s+\{" ${ngx_upstream_file} >> /dev/null
    if [ $? -eq 0 ];then
        echo -e "\033[36m ${pool_name} in ${ngx_upstream_file}. \033[0m"
    else
        echo -e "\033[31m ${pool_name} not in ${ngx_upstream_file}. \033[0m"
        exit 1
    fi
}

#显示pool在nginx upstream配置文件中对应内容
function show_pool_in_ngx_upstream() {
    #pool name开始行
    pool_name_first_line=`egrep -n "upstream\s+${pool_name}\s+\{" ${ngx_upstream_file} | cut -d ":" -f1`
    #方法块结束行
    line_list=`grep -n "}" ${ngx_upstream_file} | cut -d ":" -f1`
    #pool name结束行
    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 -E -i '/upstream\s+'${pool_name}'\s+\{/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 "upstream\s+${pool_name}\s+\{" ${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
    esac
else
    echo "variables count not eq 5.please check the usage."
fi
nginx -t -c ${ngx_upstream_file}
33[31m ====添加完成如下:====
#!/bin/bash
#function name
function_name=$1
#pool name
pool_name=$2
#pool corresponding ip list, comma-seperated
pool_ip_lists=$3
#pool corresponding tomcat port
pool_tomcat_port=$4
#upstream file location
ngx_upstream_file=$5


#检测pool在nginx upstream配置文件中是否存在
function check_pool_in_ngx_upstream() {
    grep -E "upstream\s+${pool_name}\s+\{" ${ngx_upstream_file} >> /dev/null
    if [ $? -eq 0 ];then
        echo -e "\033[36m ${pool_name} in ${ngx_upstream_file}. \033[0m"
    else
        echo -e "\033[31m ${pool_name} not in ${ngx_upstream_file}. \033[0m"
        exit 1
    fi
}

#显示pool在nginx upstream配置文件中对应内容
function show_pool_in_ngx_upstream() {
    #pool name开始行
    pool_name_first_line=`egrep -n "upstream\s+${pool_name}\s+\{" ${ngx_upstream_file} | cut -d ":" -f1`
    #方法块结束行
    line_list=`grep -n "}" ${ngx_upstream_file} | cut -d ":" -f1`
    #pool name结束行
    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 -E -i '/upstream\s+'${pool_name}'\s+\{/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 "upstream\s+${pool_name}\s+\{" ${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
    esac
else
    echo "variables count not eq 5.please check the usage."
fi
nginx -t -c ${ngx_upstream_file}
33[0m" } #在nginx upstream配置文件删除pool对应的ip地址 function delete_ip_from_upstream() { pool_name_first_line=`egrep -n "upstream\s+${pool_name}\s+\{" ${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 "
#!/bin/bash
#function name
function_name=$1
#pool name
pool_name=$2
#pool corresponding ip list, comma-seperated
pool_ip_lists=$3
#pool corresponding tomcat port
pool_tomcat_port=$4
#upstream file location
ngx_upstream_file=$5


#检测pool在nginx upstream配置文件中是否存在
function check_pool_in_ngx_upstream() {
    grep -E "upstream\s+${pool_name}\s+\{" ${ngx_upstream_file} >> /dev/null
    if [ $? -eq 0 ];then
        echo -e "\033[36m ${pool_name} in ${ngx_upstream_file}. \033[0m"
    else
        echo -e "\033[31m ${pool_name} not in ${ngx_upstream_file}. \033[0m"
        exit 1
    fi
}

#显示pool在nginx upstream配置文件中对应内容
function show_pool_in_ngx_upstream() {
    #pool name开始行
    pool_name_first_line=`egrep -n "upstream\s+${pool_name}\s+\{" ${ngx_upstream_file} | cut -d ":" -f1`
    #方法块结束行
    line_list=`grep -n "}" ${ngx_upstream_file} | cut -d ":" -f1`
    #pool name结束行
    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 -E -i '/upstream\s+'${pool_name}'\s+\{/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 "upstream\s+${pool_name}\s+\{" ${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
    esac
else
    echo "variables count not eq 5.please check the usage."
fi
nginx -t -c ${ngx_upstream_file}
33[31m this is lowest configure.
#!/bin/bash
#function name
function_name=$1
#pool name
pool_name=$2
#pool corresponding ip list, comma-seperated
pool_ip_lists=$3
#pool corresponding tomcat port
pool_tomcat_port=$4
#upstream file location
ngx_upstream_file=$5


#检测pool在nginx upstream配置文件中是否存在
function check_pool_in_ngx_upstream() {
    grep -E "upstream\s+${pool_name}\s+\{" ${ngx_upstream_file} >> /dev/null
    if [ $? -eq 0 ];then
        echo -e "\033[36m ${pool_name} in ${ngx_upstream_file}. \033[0m"
    else
        echo -e "\033[31m ${pool_name} not in ${ngx_upstream_file}. \033[0m"
        exit 1
    fi
}

#显示pool在nginx upstream配置文件中对应内容
function show_pool_in_ngx_upstream() {
    #pool name开始行
    pool_name_first_line=`egrep -n "upstream\s+${pool_name}\s+\{" ${ngx_upstream_file} | cut -d ":" -f1`
    #方法块结束行
    line_list=`grep -n "}" ${ngx_upstream_file} | cut -d ":" -f1`
    #pool name结束行
    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 -E -i '/upstream\s+'${pool_name}'\s+\{/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 "upstream\s+${pool_name}\s+\{" ${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
    esac
else
    echo "variables count not eq 5.please check the usage."
fi
nginx -t -c ${ngx_upstream_file}
33[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 "
#!/bin/bash
#function name
function_name=$1
#pool name
pool_name=$2
#pool corresponding ip list, comma-seperated
pool_ip_lists=$3
#pool corresponding tomcat port
pool_tomcat_port=$4
#upstream file location
ngx_upstream_file=$5


#检测pool在nginx upstream配置文件中是否存在
function check_pool_in_ngx_upstream() {
    grep -E "upstream\s+${pool_name}\s+\{" ${ngx_upstream_file} >> /dev/null
    if [ $? -eq 0 ];then
        echo -e "\033[36m ${pool_name} in ${ngx_upstream_file}. \033[0m"
    else
        echo -e "\033[31m ${pool_name} not in ${ngx_upstream_file}. \033[0m"
        exit 1
    fi
}

#显示pool在nginx upstream配置文件中对应内容
function show_pool_in_ngx_upstream() {
    #pool name开始行
    pool_name_first_line=`egrep -n "upstream\s+${pool_name}\s+\{" ${ngx_upstream_file} | cut -d ":" -f1`
    #方法块结束行
    line_list=`grep -n "}" ${ngx_upstream_file} | cut -d ":" -f1`
    #pool name结束行
    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 -E -i '/upstream\s+'${pool_name}'\s+\{/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 "upstream\s+${pool_name}\s+\{" ${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
    esac
else
    echo "variables count not eq 5.please check the usage."
fi
nginx -t -c ${ngx_upstream_file}
33[36m delete ${pool_name} from ${ngx_upstream_file} where ip = ${ip}.
#!/bin/bash
#function name
function_name=$1
#pool name
pool_name=$2
#pool corresponding ip list, comma-seperated
pool_ip_lists=$3
#pool corresponding tomcat port
pool_tomcat_port=$4
#upstream file location
ngx_upstream_file=$5


#检测pool在nginx upstream配置文件中是否存在
function check_pool_in_ngx_upstream() {
    grep -E "upstream\s+${pool_name}\s+\{" ${ngx_upstream_file} >> /dev/null
    if [ $? -eq 0 ];then
        echo -e "\033[36m ${pool_name} in ${ngx_upstream_file}. \033[0m"
    else
        echo -e "\033[31m ${pool_name} not in ${ngx_upstream_file}. \033[0m"
        exit 1
    fi
}

#显示pool在nginx upstream配置文件中对应内容
function show_pool_in_ngx_upstream() {
    #pool name开始行
    pool_name_first_line=`egrep -n "upstream\s+${pool_name}\s+\{" ${ngx_upstream_file} | cut -d ":" -f1`
    #方法块结束行
    line_list=`grep -n "}" ${ngx_upstream_file} | cut -d ":" -f1`
    #pool name结束行
    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 -E -i '/upstream\s+'${pool_name}'\s+\{/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 "upstream\s+${pool_name}\s+\{" ${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
    esac
else
    echo "variables count not eq 5.please check the usage."
fi
nginx -t -c ${ngx_upstream_file}
33[0m" fi done done #删除文件中的空行 sed -i '/^$/d' ${ngx_upstream_file} echo -e "
#!/bin/bash
#function name
function_name=$1
#pool name
pool_name=$2
#pool corresponding ip list, comma-seperated
pool_ip_lists=$3
#pool corresponding tomcat port
pool_tomcat_port=$4
#upstream file location
ngx_upstream_file=$5


#检测pool在nginx upstream配置文件中是否存在
function check_pool_in_ngx_upstream() {
    grep -E "upstream\s+${pool_name}\s+\{" ${ngx_upstream_file} >> /dev/null
    if [ $? -eq 0 ];then
        echo -e "\033[36m ${pool_name} in ${ngx_upstream_file}. \033[0m"
    else
        echo -e "\033[31m ${pool_name} not in ${ngx_upstream_file}. \033[0m"
        exit 1
    fi
}

#显示pool在nginx upstream配置文件中对应内容
function show_pool_in_ngx_upstream() {
    #pool name开始行
    pool_name_first_line=`egrep -n "upstream\s+${pool_name}\s+\{" ${ngx_upstream_file} | cut -d ":" -f1`
    #方法块结束行
    line_list=`grep -n "}" ${ngx_upstream_file} | cut -d ":" -f1`
    #pool name结束行
    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 -E -i '/upstream\s+'${pool_name}'\s+\{/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 "upstream\s+${pool_name}\s+\{" ${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
    esac
else
    echo "variables count not eq 5.please check the usage."
fi
nginx -t -c ${ngx_upstream_file}
33[31m ====删除完成如下:====
#!/bin/bash
#function name
function_name=$1
#pool name
pool_name=$2
#pool corresponding ip list, comma-seperated
pool_ip_lists=$3
#pool corresponding tomcat port
pool_tomcat_port=$4
#upstream file location
ngx_upstream_file=$5


#检测pool在nginx upstream配置文件中是否存在
function check_pool_in_ngx_upstream() {
    grep -E "upstream\s+${pool_name}\s+\{" ${ngx_upstream_file} >> /dev/null
    if [ $? -eq 0 ];then
        echo -e "\033[36m ${pool_name} in ${ngx_upstream_file}. \033[0m"
    else
        echo -e "\033[31m ${pool_name} not in ${ngx_upstream_file}. \033[0m"
        exit 1
    fi
}

#显示pool在nginx upstream配置文件中对应内容
function show_pool_in_ngx_upstream() {
    #pool name开始行
    pool_name_first_line=`egrep -n "upstream\s+${pool_name}\s+\{" ${ngx_upstream_file} | cut -d ":" -f1`
    #方法块结束行
    line_list=`grep -n "}" ${ngx_upstream_file} | cut -d ":" -f1`
    #pool name结束行
    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 -E -i '/upstream\s+'${pool_name}'\s+\{/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 "upstream\s+${pool_name}\s+\{" ${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
    esac
else
    echo "variables count not eq 5.please check the usage."
fi
nginx -t -c ${ngx_upstream_file}
33[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 esac else echo "variables count not eq 5.please check the usage." fi nginx -t -c ${ngx_upstream_file}
1
2
3
4
# 新增upstream服务器
$ ./change_nginx_upstream_conf.sh add insurance-cloud 10.1.2.3 6789 nginx1.conf
# 删除upstream服务器
$ ./change_nginx_upstream_conf.sh delete insurance-cloud 10.1.2.3 6789 nginx1.conf

参考

Shell脚本修改Nginx upstream配置文件