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.

Cpanel plugin to run a bash script

Discussion in 'cPanel Developers' started by SeerKan, Feb 28, 2012.

  1. SeerKan

    SeerKan Registered

    Joined:
    Oct 23, 2007
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    1
    Hello,

    I am a beginner in programming especially in cpanel plugins. I am trying to create a simple plugin for cpanel that will basically run a bash script on the server.
    I created and installed the plugin, I have the icon in cpanel but I'm stuck at running the bash script.


    The code I have until now:

    /usr/local/cpanel/base/frontend/x3/phpfunctions/phpfunctions.html

    PHP:
    <cpanel setvar="headerimg=../images/topsubdomains.gif">
    <
    cpanel setvar="dprefix=../">
    <
    cpanel Branding="include(stdheader.html)">
    <
    div class="body-content">

      <
    cpanel phpfunctions="bar()">

    </
    div>
    <
    cpanel Branding="include(stdfooter.html)">
    /usr/local/cpanel/Cpanel/phpfunctions.pm

    PHP:
    package Cpanel:Advanced:phpfunctions;
    use 
    lib '/usr/local/cpanel';
    use 
    strict;
    use 
    Carp                 ();

    our $VERSION '1.0';

    require 
    5.004;

    sub phpfunctions_init { return 1; }

    sub phpfunctions_bar {
      
    system ("/scripts/phpfunctions.sh");
      return 
    1;
    }
    /scripts/phpfunctions.sh

    PHP:
    echo "it works"
    I also enabled debuging and I see the error :

    PHP:
    warn [phpfunctions::barEncountered error in phpfunctions::barphpfunctions::bar() failedUndefined subroutine &Cpanel::phpfunctions::phpfunctions_bar called at (eval 76line 1.
    My target is to create a button "activate" in the cpanel page phpfunctions.html, and when the user clicks the button the script will execute the .sh script that will contain echo $i (where $i is the cpanel username were the button was clicked). After that it's all bash scripting and I can handle it.

    Any help is appreciated.

    Best regards.
     
  2. cPanelDavidN

    cPanelDavidN Integration Developer
    Staff Member

    Joined:
    Dec 17, 2009
    Messages:
    571
    Likes Received:
    1
    Trophy Points:
    18
    Location:
    Houston, TX
    cPanel Access Level:
    Root Administrator
    Hi SeerKan,

    Your module seems to be in order to perform API1 functions, as documented here, Writing cPanel Modules.

    I think it might be failing due to your Perl package declaration. It should be with double colons:
    Code:
    package Cpanel::Advanced::phpfunctions;
    That said, I would use the API2 module syntax since you can return data structures (which can be used in the frontend, via templates...it also provides much more refined answer if you use remote API calls)

    Code:
    package Cpanel::Advanced::phpfunctions; 
    
    use strict; 
    use Carp (); 
    
    our $VERSION = '1.0'; 
    
    require 5.004; 
    
    sub api2 {
        my ($func) = @_;
        my $api_ref = {
            'bar'   => {},
        };
        return $api_ref->{$func};
    }
    
    sub api2_bar { 
      system ("/scripts/phpfunctions.sh"); 
      return 1; 
    }
    
    ##### more functions here #####
    1;
    
    A couple of things to note: unless you are intending to call this module outside a cPanel runtime process, you can/should probably remove the 'use lib' pragma. The module should be in /usr/local/cpanel/Cpanel and @INC will already contain that path. Also, the Perl that will load and run this module is only 5.6 (albeit, with enhancements). And the available modules will be limited, at least comparatively for the average Perl coder....just something to keep in mind ;) You can add any modules you need to /usr/local/cpanel/perl/ (that path is also in @INC) but they cannot be XS (must be pure Perl), and must work with Perl 5.6.

    FYI: we have a good example of a Custom cPanel Module on github: https://github.com/CpanelInc/Custom-cPanel-Module

    Best Regards,
    -DavidN
     
Loading...

Share This Page