The Community Forums

Interact with an entire community of cPanel & WHM users!
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Remote SQL Access update script needed

Discussion in 'cPanel Developers' started by CommodorePeter, Jun 4, 2009.

  1. CommodorePeter

    CommodorePeter Registered

    Joined:
    Jun 4, 2009
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    1
    Location:
    Toronto
    I want to write a cron job that would contain a cpanel update script that would do the following:

    1) It would read the allowed ACCESS HOSTS from the REMOTE ACCESS HOSTS list.

    2) It would then resolve any Dynamic DNS addresses in the lists to absolute IP addresses and put them back in the list.

    3) It would keep a list of old IP addresses and delete them from the list when they no longer are part of the resolved set.

    You see the problem? Populating the ACCESS HOSTS list with Dynamic DNS addresses has no effect as they don't correctly resolve so I need to do it myself. Anyone have a another solution?

    So I need to know two things:
    A) The appropriate API to read and wite the Cpanel feature.
    B) The Apache or other routine that I can call the net to find the ip address of DDNS.

    Ideas? My ISP is like DUH on this problem. I'm using Cpanel 11.24.4-STABLE

    Thanks for any ideas.
     
  2. cPanelDavidG

    cPanelDavidG Technical Product Specialist

    Joined:
    Nov 29, 2006
    Messages:
    11,279
    Likes Received:
    8
    Trophy Points:
    38
    Location:
    Houston, TX
    cPanel Access Level:
    Root Administrator
    You can use the following API1 function to add a remote host:

    Code:
    <cpanel Mysql="addhost($FORM{'host'})">
    You can use the following API1 function to remove a remote host:

    Code:
    <cpanel Mysql="delhost($FORM{'host'})">
    The following API2 function is used for viewing existing remote hosts:

    Code:
    <?cp MysqlFE::listhosts() ?>
    You can use Matt's PHP class to more easily interact with our APIs: http://forums.cpanel.net/f42/xmlapi-php-class-111897.html
     
  3. dbnettle

    dbnettle Member

    Joined:
    Jul 27, 2009
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    1
    Adding and deleting hosts programmatically is exactly what I am looking to do myself as well. However, I'm having trouble integrating the above examples into the PHP class provided by Matt. I'm fairly new to PHP in general. The rest of my script, though, is in PHP. So it would be great to use it as such.

    By the way, I have found that I can get the new IP address in PHP by using the gethostbyname function.

    Code:
    $ip = gethostbyname($host);
    Thanks in advance for any assistance you might be able to provide.

    -dbnettle
     
  4. cPanelDavidG

    cPanelDavidG Technical Product Specialist

    Joined:
    Nov 29, 2006
    Messages:
    11,279
    Likes Received:
    8
    Trophy Points:
    38
    Location:
    Houston, TX
    cPanel Access Level:
    Root Administrator
    Have you had a look at api1_example.php and api2_example.php yet in the zip archive where Matt's PHP class is?

    Right now there is a slight snag with using API2 via the class. If there are no arguments (such as the above API2 function), just pass "" as the final argument to the PHP function calling the API2 function.
     
  5. dbnettle

    dbnettle Member

    Joined:
    Jul 27, 2009
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    1
    I have looked specifically at that one since the above poster indicated that this was an API 1 call. Is there a specification document for API 1? I have only been able to located the specifications for API 2.

    Here is what I came up with that does not seem to be working.

    Code:
    print $xmlapi->api1_query($cpuser, "Mysql", "addhost", $ip );
    It's producing the following error:

    Code:
    PHP Fatal error:  Call to a member function api1_query() on a non-object in /home/useraccount/public_html/dev/db_remotehost_update.php on line 45
    Line 45 corresponds to the line of code above.

    -dbnettle
     
  6. cPanelDavidG

    cPanelDavidG Technical Product Specialist

    Joined:
    Nov 29, 2006
    Messages:
    11,279
    Likes Received:
    8
    Trophy Points:
    38
    Location:
    Houston, TX
    cPanel Access Level:
    Root Administrator
    The API2 documentation is currently incomplete. We are actively working on documenting both API1 and API2.

    Try using array($ip) rather than just $ip.
     
  7. dbnettle

    dbnettle Member

    Joined:
    Jul 27, 2009
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    1
    This returned the same error as before.

    -dbnettle
     
  8. cPanelDavidG

    cPanelDavidG Technical Product Specialist

    Joined:
    Nov 29, 2006
    Messages:
    11,279
    Likes Received:
    8
    Trophy Points:
    38
    Location:
    Houston, TX
    cPanel Access Level:
    Root Administrator
    Do you have the following line of code anywhere above that line of code in the file:

    PHP:
    $xmlapi = new xmlapi($ip);
     
  9. dbnettle

    dbnettle Member

    Joined:
    Jul 27, 2009
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    1
    Okay, I feel ever so slightly silly. I did have the following code, however, late last night I had for some reason commented out some of the lines. Below is all of the code that pertains to the xmlapi calls.

    Code:
    include("xmlapi.php.inc");
    
    $xmlapi = new xmlapi($db_host);
    $xmlapi->password_auth($cpuser,$cppass);
    $xmlapi->set_debug(1);
    print $xmlapi->api1_query($cpuser, "Mysql", "addhost", array($ip));
    The above returns the following errors. I have obfuscated the IP address below:

    Code:
    RAW API1 CALL: Mysqladdhost1<IP ADDRESS OMITTED> QUERY: https://localhost:2087/xml-api/cpanel?user=useraccount&xmlin=%3Ccpanelaction%3E%3Cmodule%3EMysql%3C%2Fmodule%3E%3Cfunc%3Eaddhost%3C%2Ffunc%3E%3Capiversion%3E1%3C%2Fapiversion%3E%3Cargs%3E<IP ADDRESS OMITTED>%3C%2Fargs%3E%3C%2Fcpanelaction%3E RAW XML:
    
    Warning: simplexml_load_string() [function.simplexml-load-string]: Entity: line 6: parser error : Entity 'reg' not defined in /home/useraccount/public_html/dev/xmlapi.php.inc on line 87
    
    Warning: simplexml_load_string() [function.simplexml-load-string]: <title>cPanel&reg; 11</title> in /home/useraccount/public_html/dev/xmlapi.php.inc on line 87
    
    Warning: simplexml_load_string() [function.simplexml-load-string]: ^ in /home/useraccount/public_html/dev/xmlapi.php.inc on line 87
    
    Warning: simplexml_load_string() [function.simplexml-load-string]: Entity: line 22: parser error : Entity 'nbsp' not defined in /home/useraccount/public_html/dev/xmlapi.php.inc on line 87
    
    Warning: simplexml_load_string() [function.simplexml-load-string]: <td>&nbsp;</td> in /home/useraccount/public_html/dev/xmlapi.php.inc on line 87
    
    Warning: simplexml_load_string() [function.simplexml-load-string]: ^ in /home/useraccount/public_html/dev/xmlapi.php.inc on line 87
    
    Warning: simplexml_load_string() [function.simplexml-load-string]: Entity: line 60: parser error : Entity 'copy' not defined in /home/useraccount/public_html/dev/xmlapi.php.inc on line 87
    
    Warning: simplexml_load_string() [function.simplexml-load-string]: &copy; cPanel, Inc. 2009 in /home/useraccount/public_html/dev/xmlapi.php.inc on line 87
    
    Warning: simplexml_load_string() [function.simplexml-load-string]: ^ in /home/useraccount/public_html/dev/xmlapi.php.inc on line 87
    object(SimpleXMLElement)#2 (2) { ["head"]=> object(SimpleXMLElement)#3 (3) { ["link"]=> array(2) { [0]=> object(SimpleXMLElement)#5 (1) { ["@attributes"]=> array(3) { ["rel"]=> string(13) "shortcut icon" ["href"]=> string(31) "/unprotected/cpanel/favicon.ico" ["type"]=> string(12) "image/x-icon" } } [1]=> object(SimpleXMLElement)#7 (1) { ["@attributes"]=> array(3) { ["rel"]=> string(10) "stylesheet" ["href"]=> string(29) "/unprotected/cpanel/style.css" ["type"]=> string(8) "text/css" } } } ["meta"]=> object(SimpleXMLElement)#6 (1) { ["@attributes"]=> array(2) { ["http-equiv"]=> string(12) "Content-Type" ["content"]=> string(24) "text/html; charset=utf-8" } } ["title"]=> string(9) "cPanel 11" } ["body"]=> object(SimpleXMLElement)#4 (1) { ["div"]=> object(SimpleXMLElement)#8 (3) { ["@attributes"]=> array(1) { ["id"]=> string(4) "wrap" } ["div"]=> array(3) { [0]=> object(SimpleXMLElement)#9 (2) { ["@attributes"]=> array(1) { ["id"]=> string(7) "top-whm" } [0]=> string(5) " " } [1]=> object(SimpleXMLElement)#10 (2) { ["@attributes"]=> array(1) { ["id"]=> string(3) "mid" } ["div"]=> object(SimpleXMLElement)#13 (4) { ["@attributes"]=> array(2) { ["id"]=> string(12) "content-wrap" ["align"]=> string(6) "center" } ["form"]=> object(SimpleXMLElement)#14 (3) { ["@attributes"]=> array(2) { ["action"]=> string(7) "/login/" ["method"]=> string(4) "post" } ["input"]=> array(2) { [0]=> object(SimpleXMLElement)#17 (1) { ["@attributes"]=> array(3) { ["type"]=> string(6) "hidden" ["name"]=> string(11) "login_theme" ["value"]=> string(6) "cpanel" } } [1]=> object(SimpleXMLElement)#19 (1) { ["@attributes"]=> array(3) { ["type"]=> string(6) "hidden" ["name"]=> string(8) "goto_uri" ["value"]=> string(209) "/xml-api/cpanel?user=useraccount&xmlin=%3Ccpanelaction%3E%3Cmodule%3EMysql%3C%2Fmodule%3E%3Cfunc%3Eaddhost%3C%2Ffunc%3E%3Capiversion%3E1%3C%2Fapiversion%3E%3Cargs%3E<IP ADDRESS OMITTED>%3C%2Fargs%3E%3C%2Fcpanelaction%3E" } } } ["table"]=> object(SimpleXMLElement)#18 (2) { ["@attributes"]=> array(4) { ["width"]=> string(4) "100%" ["class"]=> string(5) "login" ["cellpadding"]=> string(1) "0" ["cellspacing"]=> string(1) "0" } ["tr"]=> array(4) { [0]=> object(SimpleXMLElement)#20 (1) { ["td"]=> array(2) { [0]=> object(SimpleXMLElement)#24 (2) { ["@attributes"]=> array(1) { ["align"]=> string(4) "left" } ["b"]=> string(5) "Login" } [1]=> object(SimpleXMLElement)#25 (1) { ["nbsp"]=> object(SimpleXMLElement)#26 (0) { } } } } [1]=> object(SimpleXMLElement)#21 (1) { ["td"]=> array(2) { [0]=> string(9) "Username:" [1]=> object(SimpleXMLElement)#27 (2) { ["@attributes"]=> array(1) { ["class"]=> string(11) "login_lines" } ["input"]=> object(SimpleXMLElement)#28 (1) { ["@attributes"]=> array(4) { ["type"]=> string(4) "text" ["id"]=> string(4) "user" ["name"]=> string(4) "user" ["size"]=> string(2) "16" } } } } } [2]=> object(SimpleXMLElement)#22 (2) { ["@attributes"]=> array(1) { ["class"]=> string(4) "row2" } ["td"]=> array(2) { [0]=> string(9) "Password:" [1]=> object(SimpleXMLElement)#29 (2) { ["@attributes"]=> array(1) { ["class"]=> string(11) "login_lines" } ["input"]=> object(SimpleXMLElement)#30 (1) { ["@attributes"]=> array(4) { ["type"]=> string(8) "password" ["id"]=> string(4) "pass" ["name"]=> string(4) "pass" ["size"]=> string(2) "16" } } } } } [3]=> object(SimpleXMLElement)#23 (1) { ["td"]=> object(SimpleXMLElement)#31 (2) { ["@attributes"]=> array(2) { ["colspan"]=> string(1) "2" ["style"]=> string(18) "text-align: center" } ["input"]=> object(SimpleXMLElement)#32 (1) { ["@attributes"]=> array(4) { ["type"]=> string(6) "submit" ["id"]=> string(5) "login" ["value"]=> string(5) "Login" ["class"]=> string(12) "input-button" } } } } } } } ["br"]=> array(2) { [0]=> object(SimpleXMLElement)#15 (0) { } [1]=> object(SimpleXMLElement)#16 (0) { } } ["script"]=> string(420) " /* Must not include external javascript -jnk 06.20.09 */ var init = function() { document.getElementById("user").value = ''; document.getElementById("pass").value = ''; document.getElementById("user").focus(); }; if( window.addEventListener ) { window.addEventListener('load',init,false); } else if( document.addEventListener ) { document.addEventListener('load',init,false); } " } } [2]=> object(SimpleXMLElement)#11 (2) { ["@attributes"]=> array(1) { ["id"]=> string(3) "bot" } [0]=> string(1) " " } } ["copy"]=> object(SimpleXMLElement)#12 (0) { } } } } SIMPLEXML OBJ:
    Warning: simplexml_load_string() [function.simplexml-load-string]: Entity: line 6: parser error : Entity 'reg' not defined in /home/useraccount/public_html/dev/xmlapi.php.inc on line 89
    
    Warning: simplexml_load_string() [function.simplexml-load-string]: <title>cPanel&reg; 11</title> in /home/useraccount/public_html/dev/xmlapi.php.inc on line 89
    
    Warning: simplexml_load_string() [function.simplexml-load-string]: ^ in /home/useraccount/public_html/dev/xmlapi.php.inc on line 89
    
    Warning: simplexml_load_string() [function.simplexml-load-string]: Entity: line 22: parser error : Entity 'nbsp' not defined in /home/useraccount/public_html/dev/xmlapi.php.inc on line 89
    
    Warning: simplexml_load_string() [function.simplexml-load-string]: <td>&nbsp;</td> in /home/useraccount/public_html/dev/xmlapi.php.inc on line 89
    
    Warning: simplexml_load_string() [function.simplexml-load-string]: ^ in /home/useraccount/public_html/dev/xmlapi.php.inc on line 89
    
    Warning: simplexml_load_string() [function.simplexml-load-string]: Entity: line 60: parser error : Entity 'copy' not defined in /home/useraccount/public_html/dev/xmlapi.php.inc on line 89
    
    Warning: simplexml_load_string() [function.simplexml-load-string]: &copy; cPanel, Inc. 2009 in /home/useraccount/public_html/dev/xmlapi.php.inc on line 89
    
    Warning: simplexml_load_string() [function.simplexml-load-string]: ^ in /home/useraccount/public_html/dev/xmlapi.php.inc on line 89
    I'm not sure where to go from here. Thank you for all the help you have provided.

    -dbnettle
     
  10. cPanelDavidG

    cPanelDavidG Technical Product Specialist

    Joined:
    Nov 29, 2006
    Messages:
    11,279
    Likes Received:
    8
    Trophy Points:
    38
    Location:
    Houston, TX
    cPanel Access Level:
    Root Administrator
    Looks like you're having HTML returned, possibly a login screen.

    Please ensure you have passed the user credentials of the WHM reseller correctly.

    If you are authenticating to the API as a cPanel user rather than a reseller, be sure to use the cPanel ports by adding this above your API call to use a cPanel port instead:

    PHP:
    $xmlapi->set_port(2083);
     
  11. dbnettle

    dbnettle Member

    Joined:
    Jul 27, 2009
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    1
    This did it! I'm including my code below for future reference for anyone who happens upon this thread. Thank you very much for the assistance.

    Code:
    include("xmlapi.php.inc");
    
    $xmlapi = new xmlapi($db_host);
    $xmlapi->set_port(2083);
    $xmlapi->password_auth($cpuser,$cppass);
    $xmlapi->set_debug(0);
    $xmlapi->api1_query($cpuser, "Mysql", "delhost", array($old_ip));
    $xmlapi->api1_query($cpuser, "Mysql", "addhost", array($ip));
    Thanks again!

    -dbnettle
     
Loading...

Share This Page