Using CURL to login to Cpanel

Status
Not open for further replies.

tyler03

New member
Apr 30, 2008
13
0
0
Ok, so I'm trying to use CURL to login to my cpanel (and eventually add on subdomains from a list for me automatically), but it refuses to login for me. Here's what I've got so far...

PHP:
<?php
// INIT CURL
$ch = curl_init();

// SET URL FOR THE POST FORM LOGIN
curl_setopt($ch, CURLOPT_URL, 'www.example.com:2082');

// ENABLE HTTP POST
curl_setopt ($ch, CURLOPT_POST, 1);

// SET POST PARAMETERS : FORM VALUES FOR EACH FIELD
curl_setopt ($ch, CURLOPT_POSTFIELDS, 'login_theme=cpanel&user=USERNAME&pass=PASSWORD');

// IMITATE CLASSIC BROWSER'S BEHAVIOUR : HANDLE COOKIES
curl_setopt ($ch, CURLOPT_COOKIEJAR, 'http://www.example.com/curl/cookie.txt');

# Setting CURLOPT_RETURNTRANSFER variable to 1 will force cURL
# not to print out the results of its query.
# Instead, it will return the results as a string return value
# from curl_exec() instead of the usual true/false.
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);

// EXECUTE 1st REQUEST (FORM LOGIN)
$store = curl_exec ($ch);
echo curl_exec($ch);

// CLOSE CURL
curl_close ($ch);

?>

However all it does is echo the cpanel login screen instead of the screen you get once you're logged in...any idea where I'm going wrong here?
 


Tyler ... it's going to be much easier just setting up wild card sub-domains and retrieving the information from your database. What you are doing really sounds like a pain in the ass way of adding subdomains.
 
This is what I have for hostmonster. Shouldn't take much to mod. I forget if it uses their CPanel or custom interfact though.
PHP:
<?php 
function createSubdomain($createURL,$dLoc)
{
$docRoot="public_html/".$dLoc;
$docRoot=str_replace("//","/",$docRoot);
$rootDomain=getDomain($createURL);
$subdomain=getSub($createURL);
$createString="rootdomain=".urlencode($rootDomain)."&domain=".urlencode($subdomain)."&dir=".urlencode($docRoot)."&go=Create";
echo $createString."<br><br><br>";

$url="http://YOUR_IP_HERE:2082/frontend/hostmonster/subdomain/index.html";
$useragent="Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_USERPWD, 'YOUR_USERNAME_HERE:YOUR_PASSWORD_HERE');
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
curl_setopt($ch, CURLOPT_RETURNTRANSFER  ,1);
curl_setopt($ch, CURLOPT_COOKIEJAR, "c:/cookie-hm-jar.txt");
curl_setopt($ch, curlOPT_COOKIEFILE, "c:/cookie-hm-jar.txt");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION  ,1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
$data=curl_exec($ch);
if(curl_error($ch))
{
    echo curl_error($ch)."<br>";
}

curl_setopt($ch, CURLOPT_URL, 'http://YOUR_IP_HERE:2082/frontend/hostmonster/subdomain/doadddomain.html');
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $createString);
$data=curl_exec($ch);
$data=str_replace("<meta ","<dud ",$data);
curl_close($ch);
if(stristr($data,"has been created")===FALSE)
{
    die($data);
}
}
function getSub($url)
{
    $spl=explode(".",$url);
    return($spl[0]);
}
function getDomain($url)
{
    $spl=explode(".",$url);
    return($spl[1].".".$spl[2]);
}
?>

It's a library for a script I use, but might be a good starting point for whoever needs it.
 
Status
Not open for further replies.