Loop through all accounts and run api calls?

MACscr

Well-Known Member
Sep 30, 2003
198
5
168
cPanel Access Level
Root Administrator
I am making two different scripts, one that runs a cpanel plugin and is obviously limited to the current user. The other script just runs from a cron job as root, but I am not sure how you specify what cpanel account i want it to fetch the data for. If I am doing something like so:

PHP:
$r = $cpanel->api2('Email', 'listdomainforwards');
var_dump($r);
Is there a way for me to specify the cpanel account as well? I think there might be a way to do all of this using the xmlapi, but I would rather not and since this is running internally, there is no reason to do any type of authentication. Am I missing something?
 

KostonConsulting

Well-Known Member
Verifed Vendor
Jun 17, 2010
255
1
68
San Francisco, CA
cPanel Access Level
Root Administrator
What are you using to connect to cPanel if you're not using the cPanel XML API PHP class? Where is the $cpanel->api2() function defined?

In the cPanel XML API PHP class, you call API2 like so and can set the user:

Code:
api2_query($user, $module, $function, $args = array())
The only way I'm aware of not using auth credentials as root is to load up Cpanel::XML in Perl like so:

Code:
#!/usr/bin/perl
BEGIN{ unshift(@INC,'/usr/local/cpanel'); }
use Cpanel::XML ();
use Data::Dumper ();
use XML::Simple ();

my $args = {
        'cpanel_xmlapi_module' => 'Email',
        'cpanel_xmlapi_func' => 'listdomainforwards',
        'cpanel_xmlapi_apiversion' => '2',
        'cpanel_xmlapi_user' => $user
    };
my $xml = Cpanel::XML::cpanel_exec_fast( $args );
my $xmlRef = XML::Simple::XMLin($xml);
print Data::Dumper::Dumper($xmlRef);

You can 'ls /var/cpanel/users' to get a list of all the cPanel users.
 

KostonConsulting

Well-Known Member
Verifed Vendor
Jun 17, 2010
255
1
68
San Francisco, CA
cPanel Access Level
Root Administrator
LiveAPI can only run within the cPanel web server (cpsrvd). You can't run it from the command line as root as the permissions to access the cPanel account are built into cPanel's web server (running on ports 2083 and 2087). If you are running something from the command line, you are no longer in cPanel so you'll need to connect to cPanel via API which means using the XMLAPI to connect.