cPanel Reseller Ownership Change

Quickly update the reseller ownership in cPanel

After seeing that cPanel doesn’t offer a quick way to change a reseller for a user, I’ve made a simple script to set this in shell.

#!/bin/sh

################################################################
#                                                              #
#  CPANEL RESELLER CHANGE                                      #
#                                                              #
#  Coded by Noah Hearle, Design Extreme                        #
#  https://designextreme.com                                   #
#                                                              #
#  Created: 2019/06/04                                         #
#  Modified: 2019/06/04                                        #
#                                                              #
#  Quickly change a users’ reseller in shell                   #
#                                                              #
#  Post your comments at:                                      #
#  https://blog.nahoo.co.uk/cpanel-reseller-change-script/ #
#                                                              #
#  Usage: sh ./reseller_change.sh <user> <reseller username>   #
#                                                              #
################################################################

if [ ! -n "$1" ] && [ -n "$2" ]; then
        echo -e "\e[38;5;202mError:\e[0m Please enter the user and the reseller’s username"
        echo 'Usage: sh ./reseller_change.sh <user> <reseller username>'
        exit
fi

if [ ! -n "$1" ]; then
        echo -e "\e[38;5;202mError:\e[0m Please enter the user"
        echo 'Usage: sh ./reseller_change.sh <user> <reseller username>'
        exit
fi

if [ ! -n "$2" ]; then
        echo -e "\e[38;5;202mError:\e[0m Please enter the reseller’s username"
        echo 'Usage: sh ./reseller_change.sh <user> <reseller username>'
        exit
fi

user=$1
reseller=$2

if [ ! -f "/var/cpanel/users/$user" ]; then
        echo -e "\e[38;5;202mError:\e[0m The user: $user doesn’t exist"
        exit
fi

if [ ! -f "/var/cpanel/users/$reseller" ]; then
        echo -e "\e[38;5;202mError:\e[0m The reseller’s username: $reseller doesn’t exist"
        exit
fi

if [ $reseller != 'root' ] && [ $(grep -c "$reseller:" /var/cpanel/resellers) -lt 1 ]; then
        echo -e "\e[38;5;202mError:\e[0m The user: $reseller exists but it is not listed as a reseller"
        exit
fi

if [ $(grep -c 'OWNER=' /var/cpanel/users/$user) -lt 1 ]; then
        echo -e "\e[38;5;202mError:\e[0m Current reseller is not set for user: $user"
        exit
fi

if [ $(grep -c "OWNER=$reseller" /var/cpanel/users/$user) -gt 0 ]; then
        echo -e "\e[33mWarning:\e[0m Reseller: $reseller is already set as the owner for user: $user"
        exit
fi

sed -i "s/OWNER=[a-z][0-9a-z-]*[0-9a-z]/OWNER=$reseller/" /var/cpanel/users/$user
/usr/local/cpanel/scripts/updateuserdomains
echo -e "\e[38;5;118mSuccess:\e[0m Updated $user with new reseller: $reseller"

It will perform checks for existance of all users and check the reseller exists. You can save this as a file such as reseller_update.sh and run using the command: ./reseller_update.sh <user> <reseller>.

Leave a Reply

Your email address will not be published. Required fields are marked *