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 cPanel Password via Perl Script

Discussion in 'cPanel Developers' started by smartypantsii, Oct 16, 2014.

  1. smartypantsii

    smartypantsii Registered

    Dec 21, 2012
    Likes Received:
    Trophy Points:
    cPanel Access Level:
    Root Administrator

    I have been writing a Perl script to change the password of a cPanel account. The script works great when a password without some special characters is used, but when special characters are in the password it fails.

    example password: _#*yu5cE?$np

    Anyone have any recommendations to work around this problem?

    #This script updates a cPanel password through the cPanel API
    #It can also generate a random password with the -r flag
    #Written by
    #Jason Alan Barnes
    #2nd Line Shared Support
    #Providence, Utah, USA
    #Last Revision Sept 25th, 2014
    use warnings;
    use strict;
    use Getopt::Long qw(:config bundling);
    use LWP::UserAgent;
    use LWP::Protocol::https;
    use XML::Simple;
    use Data::Dumper;
    use utf8;
    binmode STDOUT, ":encoding(UTF-8)";
    sub rndStr{ join'', @_[ map{ rand @_ } 1 .. shift ] }
    #Get command Line flags
    my $randpass = 0;
    my $result = GetOptions(
            'r' => \$randpass
    print $randpass;
    my $username = lc(shift(@ARGV)); chomp($username);
    if (!$username){
            die("Username missing!\n\nUsage: remotechgpass [option] [username]\n\t-r\tGenerate a random password\n\n");
    my $password = rndStr( 12, 'A'..'Z', 0..9, 'a'..'z', '-', '_', '.', '^', '@', '#', '$', '%', '*', ':', '?', '!', '&', '~');
    #Set Password
    if (not $randpass){
            print "Enter Password: ";
            $password = <>;
            chomp $password;
    #Read in hash file for authenticating to cPanel API
    open FILE, "</root/.accesshash"
            or die "cannot open < /root/.accesshash: $!";
    my $hash = do { local $/; <FILE> };
    $hash =~ s/\n//g;
    #Prepare Authentication header
    my $auth = "WHM root:" . $hash;
    #Initialize User Agent
    my $ua = LWP::UserAgent->new;
    #Create the request to API2 throught the XML-API and access the getalwaysaccept function to check if the change is needed
    my $checkrequest =
                      HTTP::Request->new( GET => "$username&pass=$password" );
    $checkrequest->header( Authorization => $auth );
    #Run the request
    my $response = $ua->request($checkrequest);
    #print $response->content;
    my $xml_hashref = XMLin($response->content);
    if ($xml_hashref->{'passwd'}->{'status'} == 0){
            print "Password change failed\n";
            print "cPanel Output: ", $xml_hashref->{'passwd'}->{'statusmsg'}, "\n\n";
    } elsif ($xml_hashref->{'passwd'}->{'status'} == 1){
            print "New Password: $password\n";
            print "cPanel Output: ", $xml_hashref->{'passwd'}->{'statusmsg'}, "\n\n";
    } else {
            print "Password change failed\nError connecting to API!\n\n";
    #End of script
  2. cPAndrewHodge

    cPAndrewHodge cPanel UI Developer Staff Member

    Dec 10, 2012
    Likes Received:
    Trophy Points:
    cPanel Access Level:
    Root Administrator
    You should try escaping the characters. The API Shell in WHM will give you sample URL's so you can see what we create and what will work.

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