Skip to content

Instantly share code, notes, and snippets.

@4np
Last active December 21, 2015 08:49
Show Gist options
  • Save 4np/6280634 to your computer and use it in GitHub Desktop.
Save 4np/6280634 to your computer and use it in GitHub Desktop.
Shell script to batch-suspend Twilio sub accounts based on regular expressions and whitelists. Also see https://gist.github.com/4np/5583898 for command line interactions with Twilio. See comment section for screenshots...
#!/bin/sh
# Batch Auto Suspender for Twilio Sub Accounts
# Copyright (C) 2013 Jeroen Wesbeek <work AT osx.eu>
##################### USAGE #######################
#
# Run the script command line or using a cron job:
# ~ $ ./TwilioAutoSuspend.sh
#
# To try out your patterns and whitelist, use the
# -p (pretent) argument. This will only output to
# stdin and not actually perform any changes
# ~ $ ./TwilioAutoSuspend.sh -p
#
################## CONFIGURATION ##################
TWILIO_MASTER_SID=put here your master SID
TWILIO_MASTER_TOKEN=put here your master token
# define the suspension patterns (regular expression)
declare -a SUSPENSION_PATTERNS=( 'uk$' '^demo' );
# define the white list (exact match)
declare -a WHITE_LIST=( 'demo uk' 'demo' );
#################### LICENSE ######################
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
###################################################
TWILIO_BASE_URL=https://api.twilio.com/2010-04-01/
TWILIO_CREDENTIALS=$TWILIO_MASTER_SID":"$TWILIO_MASTER_TOKEN
# function to check if a sub account matches the suspend patterns
matchesSuspendPattern() {
local __checkvar=$1
# iterate over suspension pattern(s)
local total=${#SUSPENSION_PATTERNS[@]}
local i=0
local result=0
for (( i=0; i<=$(( $total - 1 )); i++))
do
if [ `echo $__checkvar | grep -e ${SUSPENSION_PATTERNS[$i]} | wc -l` -gt 0 ]; then
result=1
fi
done
# return result
echo $result
}
# function to check if a sub account is whitelisted
isWhiteListed() {
local __checkvar=$1
# iterate over whitelisted sub accounts
local total=${#WHITE_LIST[@]}
local i=0
local result=0
for (( i=0; i<=$(( $total - 1 )); i++))
do
if [ "$__checkvar" == "${WHITE_LIST[$i]}" ]; then
result=1
fi
done
# result result
echo $result
}
# main routine
main() {
# pretend mode?
local __pretend_mode=0
if [ "$1" == "-p" ]; then
echo "\033[1;35m-----------------------------------------==={ \033[1;39mRUNNING IN PRETEND / DRY RUN MODE\033[1;35m }===-----------------------------------------\033[0;00m"
echo ""
__pretend_mode=1
fi
# init statistics counters
local __total_active=0
local __total_suspended=0
local __total_closed=0
local __suspended=0
local __skipped=0
local __failed=0
local __active=0
local __closed=0
# get all sub accounts for this master account
local subaccounts_raw=`curl --stderr /dev/null -X GET $TWILIO_BASE_URL"Accounts.json" -u $TWILIO_CREDENTIALS`
local subaccounts=(`echo $subaccounts_raw|jshon -e accounts -a -e friendly_name -u|sed 's/ /%/g'`)
local statusses=(`echo $subaccounts_raw|jshon -e accounts -a -e status -u`)
local sids=(`echo $subaccounts_raw|jshon -e accounts -a -e sid -u`)
local total=${#subaccounts[*]}
# iterate over sub accounts
local i=0
for (( i=0; i<=$(( $total - 1 )); i++))
do
# get sub account name and status
local subaccount=`echo ${subaccounts[$i]}|sed 's/%/ /g'`
local status=${statusses[$i]}
local sid=${sids[$i]}
local spaces=$(( 60 - ${#subaccount} ))
local tabs=$(printf "%*s" $spaces)
# check if this sub account is active
if [ "$sid" == "$TWILIO_MASTER_SID" ]; then
# do nothing
tabs=""
elif [ "$status" == "active" ]; then
# check if this account matches the pattern(s)
if [ $( matchesSuspendPattern "$subaccount" ) -gt 0 ]; then
if [ $( isWhiteListed "$subaccount" ) -gt 0 ]; then
(( __skipped++ ))
if [ $__pretend_mode -gt 0 ]; then
echo "\033[1;32mskipped\033[1;33m $subaccount\033[0;00m$tabs(sid: \033[1;39m$sid\033[0;00m) \033[1;39mwhitelisted\033[0;00m"
fi
else
if [ $__pretend_mode -gt 0 ]; then
echo "\033[1;31msuspended\033[1;33m $subaccount\033[0;00m$tabs(sid: \033[1;39m$sid\033[0;00m)"
(( __suspended++ ))
(( __total_suspended++ ))
else
# suspend sub account
local result=`curl --stderr /dev/null -XPOST $TWILIO_BASE_URL"Accounts/"$sid".json" -d "Status=suspended" -u $TWILIO_CREDENTIALS | jshon -e status | sed 's/"//g'`
if [ "$result" == "suspended" ]; then
echo "suspended "$subaccount"$tabs(sid: "$sid")"
(( __suspended++ ))
(( __total_suspended++ ))
else
echo "failed $subaccount$tabs(sid: $sid) could not suspend subaccount"
(( __failed++ ))
(( __total_active++ ))
fi
fi
fi
else
if [ $__pretend_mode -gt 0 ]; then
echo "active (no match) $subaccount$tabs(sid: $sid)"
fi
(( __total_active++ ))
fi
elif [ "$status" == "suspended" ]; then
if [ $__pretend_mode -gt 0 ]; then
echo "already suspended $subaccount$tabs(sid: $sid)"
fi
(( __total_suspended++ ))
elif [ "$status" == "closed" ]; then
if [ $__pretend_mode -gt 0 ]; then
echo "closed $subaccount$tabs(sid: $sid)"
fi
(( __total_closed++ ))
elif [ $__pretend_mode -gt 0 ]; then
echo "?????? $subaccount$tabs(sid: $sid)"
fi
done
# output statistics
if [ $__pretend_mode -gt 0 ]; then
echo ""
echo "\033[1;35m--------------------------------------------------==={ \033[1;39mSTATISTICS\033[1;35m } ===------------------------------------------------------\033[0;00m"
echo "\033[1;33mtotals \033[0;00m: suspended \033[1;31m$__total_suspended\033[0;00m active \033[1;32m$__total_active\033[0;00m closed \033[1;35m$__total_closed\033[0;00m total \033[1;39m$(( $total - 1 ))\033[0;00m"
echo "\033[1;33mthis run \033[0;00m: suspended \033[1;31m$__suspended\033[0;00m skipped \033[1;32m$__skipped\033[0;00m failed \033[1;35m$__failed\033[0;00m"
fi
}
# execute...
main $1
@4np
Copy link
Author

4np commented Aug 20, 2013

image

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment