Change the reseller for a user in shell

Nahoo

Member
Oct 7, 2004
23
1
153
London, UK
cPanel Access Level
Root Administrator
Twitter
I've created a bash script to set a user's reseller owner.

Code:
#!/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                   #
#                                                              #
#  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 just needs two arguments, one for the user and the second for the reseller's username. There are some sanity checks for user and reseller existence too.
 

sparek-3

Well-Known Member
Aug 10, 2002
2,005
222
368
cPanel Access Level
Root Administrator
You can also use

/usr/local/cpanel/bin/whmapi1 modifyacct user=$user owner=$reseller

Where $user is the username of the account and $reseller is the new reseller user that is to own the account.

Of course, this doesn't have any of the sanity checks you have in place, but if $user doesn't exist or $reseller isn't a reseller, then it won't be able to do anything.
 

cPanelMichael

Administrator
Staff member
Apr 11, 2011
47,908
2,218
463
Hello,

I've moved this thread to our Workarounds and Optimizations forum category.

Thank you.