Last active
February 17, 2022 20:55
-
-
Save troglodyne/a8fc15a7ea895777de2e372b64fc873b to your computer and use it in GitHub Desktop.
[cPanel] Perl Mechanize script for setting all /var/cpanel/clevels.conf values at once
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use strict; | |
use warnings; | |
use WWW::Mechanize; | |
use Term::ReadKey; | |
use Getopt::Long; | |
# Get options | |
my ( $host, $password, %opts ); | |
GetOptions( | |
"host|h=s" => \$host, | |
"CONTACTAIM|aim=i" => \$opts{'CONTACTAIM'}, | |
"CONTACTPUSHBULLET|pushbullet=i" => \$opts{'CONTACTPUSHBULLET'}, | |
"CONTACTPOSTURL|post=i" => \$opts{'CONTACTPOSTURL'}, | |
"CONTACTUIN|uin=i" => \$opts{'CONTACTUIN'}, | |
"CONTACTEMAIL|email=i" => \$opts{'CONTACTEMAIL'}, | |
"CONTACTPAGER|pager=i" => \$opts{'CONTACTPAGER'} | |
); | |
$host //= '127.0.0.1'; | |
# Check inputs | |
if( grep { !$_ } ( values( %opts ) ) ) { | |
print "[WARN] You must pass in ALL keys from /var/cpanel/clevels.conf for this script to work (unless you want it to set the unset values to 0).\n"; | |
print " Example: perl $0 --CONTACTAIM 1 --CONTACTPUSHBULLET 2 --CONTACTPOSTURL 3 --CONTACTUIN 4 --CONTACTEMAIL 5 --CONTACTPAGER 1\n"; | |
} | |
# Grab the password from stdin, as I'm not having this be a passable option so that it doesn't show up in ps | |
print "Please enter the root password for $host to continue: "; | |
ReadMode('noecho'); # Don't show others your pw on the screen | |
chomp( $password = <STDIN> ); | |
ReadMode(0); | |
print "\n"; | |
die("[ERROR] Blank/falsey password passed! Can't continue.\n") if !$password; | |
# Login to WHM | |
$ENV{'PERL_LWP_SSL_VERIFY_HOSTNAME'} = 0; | |
my $mechwarrior = WWW::Mechanize->new( 'onerror' => undef ); # Otherwise it'll quit on the login screen 401ing | |
$mechwarrior->get("https://$host:2087"); | |
$mechwarrior->form_id("login_form"); | |
$mechwarrior->set_fields( 'user' => 'root', 'pass' => $password ); | |
my $login_response = $mechwarrior->submit_form(); | |
die("[FATAL] Login Failed: " . $login_response->status_line . "\n") unless $login_response->is_success; | |
# Format your URL right for POSTing the request | |
my $base_url = $login_response->base; | |
$base_url =~ s/\?.*//g; | |
$base_url .= "scripts2/saveeditcontact"; | |
my $post_response = $mechwarrior->post( $base_url, \%opts ); | |
die("[FATAL] POST request to $base_url failed: " . $post_response->status_line . "\n") unless $post_response->is_success; | |
print "[INFO] Values updated successfully!\n"; | |
exit(0); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment