Skip to main content

Trouble with understanding tokens

Comments

2 comments

  • cPanelMichael
    Hello, Could you provide us with a step-by-step list of the actions you are taking so we can attempt to reproduce the issue? Thanks!
    0
  • Spork Schivago
    @jaydz49 I use tokens to access WHM's API calls as root in a perl script I wrote. This is how I do it. First, log into WHM and create the token under Home >> Development >> Manage API Tokens. I click on Generate Token and give it a good name, something that might help you remember what the token is for...After hitting Generate Token, it'll ask you to copy and save the token. This is important. If someone gains access to your token, they'll essentially have root access to WHM. Now, in my perl script, I use it like so:
    #!/usr/local/cpanel/3rdparty/bin/perl # A simple perl script that demonstrates how to use a WHM # API token to list user accounts. use strict; use LWP::UserAgent; use LWP::Protocol::https; use MIME::Base64; use IO::Socket::SSL; use URI::Escape; # Setup a variable to hold the username we're going to log into WHM as. my $user = 'root'; # Setup a variable to hold the username we want to search for (* = all). my $username = '*'; # Use the API token instead of a password or access hash. my $token = ''; # Setup a variable to hold the authorization string. my $auth = "WHM " . $user . ":" . $token; # Setup the user agent. my $ua = LWP::UserAgent->new( ssl_opts => { verify_hostname => 0, SSL_verify_mode => 'SSL_VERIFY_NONE', SSL_use_cert => 0 }, ); # list cPanel accounts. print "Attempting to list all accounts...\n"; my $request = HTTP::Request->new( POST => "https://127.0.0.1:2087/json-api/listaccts?api.version=1&search=$username&searchtype=user" ); $request->header( Authorization => $auth ); my $response = $ua->request($request); print $response->content;
    Where is replaced with my actual token that I copied and pasted. I make the file executable by doing something like:
    chmod 700 ./listaccts.pl
    Then when I execute ./listaccts.pl, I see all the accounts on my system. You would do something similar, but replace
    my $request = HTTP::Request->new( POST => "https://127.0.0.1:2087/json-api/listaccts?api.version=1&search=$username&searchtype=user" );
    with something like:
    my $request = HTTP::Request->new( POST => "https://127.0.0.1:2087/json-api/createacct?api.version=1&username=user&domain=example.com&plan=package_name&featurelist=default&quota=0&password=12345luggage&ip=n&cgi=1&hasshell=1&contactemail=user%40seconddomain.com&cpmod=paper_lantern&maxftp=5&maxsql=5&maxpop=10&maxlst=5&maxsub=1&maxpark=1&maxaddon=1&bwlimit=500&language=en&useregns=1&hasuseregns=1&reseller=0&forcedns=1&mailbox_format=mdbox&mxcheck=local&max_email_per_hour=500&max_defer_fail_percentage=80&owner=root" );
    You'd obviously probably not hardcode the username and stuff like that. You'd probably want to use some variables, like $username, $domainname, $password, etc. If you need an example to create a certain account, let me know, and I'll try to create one for you. Just give me stuff like the username you want, the domain name, etc. But I think you'll be able to figure it out with the info I shared. If you have any questions, please let me know. I suggest just starting with the listaccts example I posted, just so you can verify you're logging into WHM as root successfully and you're able to call at least the listaccts API. After that, you can just modify the code and play around with various other API calls. Thanks!
    0

Please sign in to leave a comment.