[Up]常用資訊

[重點文章] 重點文章 [重點文章] 重點文章

2020年4月12日 星期日

[BIG-IP-LTM] 使用shell Script 來啟動或關閉 pool-member

[BIG-IP-LTM] 使用shell Script 來啟動或關閉 pool-member


#!/bin/bash
#
# Command : ./update-pool.sh test-pool disable 10.10.10.10:80,10.10.10.11:80
#
#
# 
if [ -z "$1" ]
then
    # bad arguments - quit
    echo "Syntax: update-pool.sh   "
else
     build member list from arguments
    args=($(echo $3 |tr "," "\n"))
 
    # set pool
    thispool=$1
 
    # set enabled/disabled
    action=$2
 
    # build pool list
    pool=($(curl -sk -u 'admin:password' -H "Content-Type: application/json" -X GET https://x.x.x.x/mgmt/tm/ltm/pool/${thispool}?expandSubcollections=true | sed 's/,/\'$'\n/g' | grep name | grep -v ${thispool} | awk -F"\"" '{ print $4 }'))
 
    # establish the memberstring
    memberstring="{\"members\":["
 
    # loop through the pool members to see if any are one of the arguments
    for i in "${pool[@]}"
    do
        notin=0
 
        # loop through the argument member list
        for j in "${args[@]}"
        do
            if [ "$i" == "$j" ]
            then
                notin=1
            fi
        done
 
        # if the argument member was in the pool - update the memberstring value
        if [ $notin -eq 1 ]
        then
            if [ "$action" == "disable" ]
            then
                memberstring="${memberstring}{\"name\":\"$i\",\"state\":\"user-up\",\"session\":\"user-disabled\"},"
            elif [ "$action" == "enable" ]
            then
                memberstring="${memberstring}{\"name\":\"$i\",\"state\":\"user-up\",\"session\":\"user-enabled\"},"
            fi
        else
            memberstring="${memberstring}{\"name\":\"$i\",\"state\":\"user-up\",\"session\":\"user-enabled\"},"
        fi
 
    done
 
    # remove the last comma and add the end of the POST characters
    memberstring="${memberstring%?}]}"
 
    # prints completed memberstring - enable for testing
    echo "executing: $memberstring"
 
    # execute the cURL PATCH to update the pool
    curl -sk -u 'admin:password' -H "Content-Type: application/json" -X PATCH https://x.x.x.x/mgmt/tm/ltm/pool/${thispool} -d $memberstring
 
fi

沒有留言:

張貼留言