Change cPanel Password via Perl Script
Hello,
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?
#!/usr/bin/perl
#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, " };
$hash =~ s/\n//g;
#Prepare Authentication header
my $auth = "WHM root:" . $hash;
$ENV{PERL_LWP_SSL_VERIFY_HOSTNAME}=0;
#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 => "https://127.0.0.1:2087/xml-api/passwd?user=$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-
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. 0
Please sign in to leave a comment.
Comments
1 comment