Please whitelist cPanel in your adblocker so that you’re able to see our version release promotions, thanks!

The Community Forums

Interact with an entire community of cPanel & WHM users!

Change the reseller for a user in shell

Discussion in 'Workarounds and Optimization' started by Nahoo, Jun 6, 2019.

  1. Nahoo

    Nahoo Member

    Joined:
    Oct 7, 2004
    Messages:
    22
    Likes Received:
    0
    Trophy Points:
    151
    Location:
    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.
     
  2. sparek-3

    sparek-3 Well-Known Member

    Joined:
    Aug 10, 2002
    Messages:
    1,923
    Likes Received:
    177
    Trophy Points:
    343
    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.
     
  3. cPanelMichael

    cPanelMichael Technical Support Community Manager Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    47,529
    Likes Received:
    2,181
    Trophy Points:
    363
    cPanel Access Level:
    Root Administrator
    Twitter:
    Hello,

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

    Thank you.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
Loading...

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice