Skip to content

Instantly share code, notes, and snippets.

@sujaypillai
Created April 22, 2019 06:26
Show Gist options
  • Save sujaypillai/7d932870badf6b94af7c1da8e8182b87 to your computer and use it in GitHub Desktop.
Save sujaypillai/7d932870badf6b94af7c1da8e8182b87 to your computer and use it in GitHub Desktop.
Clear users synced from AD in UCP
#!/bin/bash
delete_users() {
mapfile -t < <(curl -s -X GET "https://${HOST}:${PORT}/accounts/?filter=non-admins&limit=1024" -H "accept: application/json" -H "Authorization: Bearer ${AUTHTOKEN}"| jq .accounts[].name)
len=${#MAPFILE[@]}
echo "Fetched $len users for deleting....."
for ((i=0;i<$len;i++)); do
echo "$i user ::"
userName=$(echo ${MAPFILE[$i]}|sed 's/"//g');
echo $(curl -s -X DELETE "https://${HOST}:${PORT}/accounts/$userName" -H "accept: application/json" -H "Authorization: Bearer ${AUTHTOKEN}");
done
}
## Get the AUTH token from UCP
read -p "UCP host (e.g ucp.example.com or IP): " HOST
read -p "UCP user (admin-user): " USER
read -sp "UCP pass: " PASS
if [ -z $PORT ]; then
export PORT=443
fi
export AUTHTOKEN=$(curl -s -X POST -d "{ \"username\":\"$USER\",\"password\":\"$PASS\" }" "https://${HOST}:${PORT}/auth/login" | awk -F ':' '{print $2}' | tr -d '\"{}')
## Get the total number of users in the system
export USERSCOUNT=$(curl -s -X GET "https://${HOST}:${PORT}/accounts/?filter=users&limit=1" -H "accept: application/json" -H "Authorization: Bearer ${AUTHTOKEN}" | jq .usersCount)
export ADMINUSERS=$(curl -s -X GET "https://${HOST}:${PORT}/accounts/?filter=admins&limit=all" -H "accept: application/json" -H "Authorization: Bearer ${AUTHTOKEN}" | jq .usersCount)
let USERSTODELETE=$USERSCOUNT - $ADMINUSERS
echo -en "\n"
printf "\nFound '${USERSCOUNT}' users & in '${HOST}'\n"
DEFAULT_OPTION="n"
read -p "Do you really want to delete all users in system (y|n) : " USER_OPTION
if [ "$USER_OPTION" == "$DEFAULT_OPTION" ]; then
exit 1
else
echo "Lets clear the system.... \n"
let USERSLEFT=0
while [[ "$USERSLEFT" -le "$USERSTODELETE" ]]
echo "Users Left >> " $USERSLEFT
do
USERSLEFT=$(curl -s -X GET "https://${HOST}:${PORT}/accounts/?filter=non-admins&limit=all" -H "accept: application/json" -H "Authorization: Bearer ${AUTHTOKEN}" | jq .usersCount)
delete_users
done
fi
exit 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment