cPanel Remote Bandwidth Script

Brandon

Former Senior Account Rep
Community Support
Messages
19,181
Reaction score
28
Points
48
Have you ever wanted to show how much bandwidth you have used?

Well do I have a solution for you...the Brandon's cPanel Bandwidth Checker!!

PHP:
<?php

/*############################
Bandwidth From cPanel
Brandon's cPanel Bandwidth Checker!!!
Brandon, for x10 <brandon@x10hosting.com>
Support Info: http://www.blnetworks.net/showthread.php?p=197#post197
############################*/

// EDIT BELOW
$username = "username"; // cPanel Username
$password = "password"; // cPanel Password
$domain = "domain"; // cPanel Domain
$theme = "x"; // cPanel Theme



// DO NOT EDIT THIS CODE
ini_set("display_errors", "0");
$file = file_get_contents("http://$username:$password@$domain:2082/frontend/$theme/index.html") or die("<b>Critical Error, Please check your configuration</b>");
$string1 = strpos($file, "Bandwidth (this month)");
$file = substr($file,$string1);
$string2 = strpos($file, "Megabytes");
$length = strlen($file);
$take =  $length - $string2;
$finally = substr($file,0,-$take);
$number = explode("<td class=\"index2\">", $finally);
$number = explode(" ",$number[1]);
$bandwidth = $number[0];


// START EDITING
/*
 $bandwith is now the amount of bandwith you have used!
We are echoing it, but you can do whatever with it*/
echo $bandwidth;

?>

Edit the user name, password, domain, and theme...don't edit the do not edit unless you know what edits to edit!!!

You can remove "echo $bandwidth;" if you like, and include this in any script and add $bandwidth to the script were you want.

This was coded for an upcoming signature I am making, but I decided to release it.

If you find a bug, or me, I will fix it and release a new update:biggrin:

Also if you use it, i would appreciate a reply on how it works for you, maybe some rep, or just a thanks.
 
Last edited:

Zenax

Active Member
Messages
1,377
Reaction score
4
Points
38
Have you ever wanted to show how much bandwidth you have used?

Well do I have a solution for you...the Brandon's cPanel Bandwidth Checker!!

Edit the user name, password, domain, and theme...don't edit the do not edit unless you know what edits to edit!!!

You can remove "echo $bandwidth;" if you like, and include this in any script and add $bandwidth to the script were you want.

This was coded for an upcoming signature I am making, but I decided to release it.

If you find a bug, or me, I will fix it and release a new update:biggrin:

Also if you use it, i would appreciate a reply on how it works for you, maybe some rep, or just a thanks.


The script can be modified to read any of the statistics. I managed to change it to read the amount of disk space that I had left! All you have to do is change the first bit in bold, to the text that appears on the left hand side of the table.

Example

PHP:
$string1 = strpos($file, "Bandwidth (this month)");

now becomes

PHP:
$string1 = strpos($file, "Disk space available");

Notice the difference. It basically searchs that document for that piece of text, and then displays the value its worth.

The way I figured it out was by changing the text, and then comparing the value I got to the one shown in cPanel and it honestly works!

Nice script Brandon! A very good job well done!

Regards,
Zenax

Edit: Sorry for the long length, ill remember in future. btw I gave you rep brandon!
 
Last edited:

Brandon

Former Senior Account Rep
Community Support
Messages
19,181
Reaction score
28
Points
48
I guess it could work like that, but i did math and stuff, it could still work though:pigeon:
 

Derek

Community Support Force
Community Support
Messages
12,882
Reaction score
186
Points
63
Thanks for it i might use it for like attachments..
 

Derek

Community Support Force
Community Support
Messages
12,882
Reaction score
186
Points
63
Yeah like i say Blah space blah used..
 

Zenax

Active Member
Messages
1,377
Reaction score
4
Points
38
The script would be useful for thing such as filehosting services like filevip.com as it could show how much space there is available etc?!?
 

bigguy

Retired
Messages
10,984
Reaction score
10
Points
38
Nice little snippet. I might give this a try, thanks. :)
 

Derek

Community Support Force
Community Support
Messages
12,882
Reaction score
186
Points
63
Yup thats what its for..
 

dest581

New Member
Messages
348
Reaction score
0
Points
0
It isn't a replacement for cpanel, since it grabs the data directly from cpanel. It's a way to display the amount used to your users, who (hopefully :D) cannot view your cpanel.
 

Derek

Community Support Force
Community Support
Messages
12,882
Reaction score
186
Points
63
Also if someone views the source can they see your cpanel info?
 

dest581

New Member
Messages
348
Reaction score
0
Points
0
In a normal situation, it will not be viewed. Here are the ways that your info could be revealed:

1) PHP breaks. Apache simply sends out the php script, without processing it
2) Someone gets FTP access and downloads it that way

I personally wouldn't use this script because of the tiny chance of hacking. Instead, I'd modify the script so that it updates a txt file with the bandwidth amount. I would then put that script in a non-web-accessable directory, and set up a cron job to run it every 5 minutes.
 

Brandon

Former Senior Account Rep
Community Support
Messages
19,181
Reaction score
28
Points
48
Before you criticize my 5 minute work, there is another way of doing it this way.

PHP:
 <?php 
class cpanel 
{ 
    /**  
    * @var        resource        contains curl resource for communication with cpanel  
    **/ 
    var $socket ; 
    /** 
    * @param    string            username          your cpanel username 
    * @param    string            password          your cpanel password 
    * @param    string            hostname          your cpanel hostname 
    * @param    bool            ssl                  indicate ssl availability 
    * @param    string            theme              indicate the theme being used 
    * @return    void 
    * 
    * This will give you the default settings of using ssl and X as the theme 
    * <code><?php $cpanel = new cpanel( 'username', 'password', 'hostname' ); ?></code> 
    *  
    * This will allow you to disable SSL 
    * <code><?php $cpanel = new cpanel( 'username', 'password', 'hostname', false ); ?></code> 
    *  
    * This will allow you to disable SSL and change the theme 
    * <code><?php $cpanel = new cpanel( 'username', 'password', 'hostname', false, 'mytheme' ); ?></code> 
    **/ 
    function cpanel( $username, $password, $hostname, $ssl = true, $theme = 'x' ) 
    { 
        $this->ssl = $ssl ; 
        $this->username = $username ; 
        $this->password = $password ; 
        $this->hostname = $hostname ; 
        $this->request = $request ; 
        $this->theme = $theme ; 
         
        if( !( $this->socket = @curl_init( ) ) ): die('FATAL : Cannot initialize curl'); 
            return; 
        else:     
            if( $this->ssl ): 
                curl_setopt( $this->socket, CURLOPT_SSL_VERIFYPEER, 0 );                 
                curl_setopt( $this->socket, CURLOPT_SSL_VERIFYHOST, 0 ); 
            endif; 
             
            curl_setopt( $this->socket, CURLOPT_FOLLOWLOCATION, 1 ); 
            curl_setopt( $this->socket, CURLOPT_HEADER, 0 ); 
            curl_setopt( $this->socket, CURLOPT_RETURNTRANSFER, 1 ); 
            curl_setopt( $this->socket, CURLOPT_HTTPHEADER,  
            array( sprintf( "Authorization: Basic %s", base64_encode( sprintf( '%s:%s', $this->username, $this->password ) ) ) ) 
            ); 
        endif;     
         
        if( ereg( 'Login Attempt Failed', $this->make_request( sprintf( '/frontend/%s/', $this->theme ) ) ) ) 
            die("Cannot login to cpanel"); 
    } 
    /** 
    * @param    string            request            the page to retrieve from cpanel 
    * @return    string            returns raw data 
    * 
    * This would retrieve the index page from cpanel 
    * <code><?php $cpanel->make_request( "/frontend/$cpanel->theme" ); ?> 
    **/ 
    function make_request( $request ) 
    { 
        if( $this->ssl ) 
            curl_setopt( $this->socket, CURLOPT_URL, sprintf( "https://%s:2083%s",$this->hostname,  $request ) ); 
        else 
            curl_setopt( $this->socket, CURLOPT_URL, sprintf( "http://%s:2082%s",$this->hostname,  $request ) ); 
         
        return curl_exec( $this->socket ); 
    } 
    /** 
    * @return    string            returns html tables of table from cpanel index 
    * 
    * This example method returns all your account information from cpanel index, unformatted 
    * <code><?php echo $cpanel->account_tables( ); ?></code> 
    **/ 
    function account_tables( ) 
    { 
        preg_match_all( '#(<table width="275" border="0" cellspacing="3" cellpadding="4">.*?[^<]</table>)#si',  
                        $this->make_request( sprintf( "/frontend/%s/", $this->theme ) ),  
                        $return  
        ); 
        return implode( "<br />", $return[0] ); 
    } 
    /** 
    * This method closes the curl resource 
    * 
    * <code><?php $cpanel->_close( ); ?></code> 
    **/     
    function _close( ) 
    { 
        return @curl_close( $this->socket ); 
    } 
} 
$cpanel = new cpanel( "username", "password", "hostname" ); 

echo $cpanel->account_tables( ); 

$cpanel->_close( ); 
?>

That of course uses the HTTP autentication.


I will also work on changing it for cron at a later date, I am busy writing a few classes for my site.
 
Last edited:

galaxyAbstractor

Community Advocate
Community Support
Messages
5,508
Reaction score
35
Points
48
Oh My God!!! Just what I wanted just in time! I must be able to feel these thing happen. Everytime I want something it pops up right in front of me the same day. What a pity it doesen't do the same thing with money...

Hey brandon, is it possible to do the script show total bandwidth used over time. So if my site have used 50 gb under 1 year it will show that? So it show total bandwidth used from the implementent of the script?

Some things I noticed:

1. the variable $number is two diffferent values:
PHP:
$number = explode("<td class=\"index2\">", $finally);
$number = explode(" ",$number[1]);

2. It doesen't work: http://jagf.net/bandwidth.php
 
Last edited:
Top