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.

Bash script example hook

Discussion in 'cPanel Developers' started by rahnev, May 23, 2017.

Tags:
  1. rahnev

    rahnev Active Member

    Joined:
    Jul 6, 2016
    Messages:
    30
    Likes Received:
    1
    Trophy Points:
    8
    Location:
    Bulgaria
    cPanel Access Level:
    Root Administrator
    Hello all,
    can somebody show me working example of hook, written in bash script? I want to do some custom actions when alias is created for example but cannot find a bash hook example. How can I catch input data and parse it by myself? I saw in documentation there is a template skeleton for php and perl.
    Thanks in advance.
     
  2. Anoop P Alias

    Anoop P Alias Well-Known Member

    Joined:
    Mar 31, 2015
    Messages:
    65
    Likes Received:
    6
    Trophy Points:
    8
    Location:
    Kochi,Kerala,India
    cPanel Access Level:
    Root Administrator
    the hook give you a json ds in stdin .Bash wont be a good choice to parse json. You can use python ,which is similar to bash or go with php/perl . Basically any high level language with support for stuff like json,yaml etc be good enough .
     
  3. rahnev

    rahnev Active Member

    Joined:
    Jul 6, 2016
    Messages:
    30
    Likes Received:
    1
    Trophy Points:
    8
    Location:
    Bulgaria
    cPanel Access Level:
    Root Administrator
    Can you give me a working example with perl for example for catching all input data and parse it?
     
  4. cPanelMichael

    cPanelMichael Forums Analyst
    Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    37,064
    Likes Received:
    1,287
    Trophy Points:
    363
    cPanel Access Level:
    Root Administrator
    Hello,

    You can find the corresponding API function for a hook by enabling "Debug mode is on. The system displays information about every stage for every hookable event, even if no hooks exist for that stage." under the "Development" tab in "WHM >> Tweak Settings". Then, perform the specific action you want to hook into via cPanel or WHM, and watch the output in /usr/local/cpanel/logs/error_log.

    For example, the following cPanel API 1 function is utilized when adding an Alias in cPanel:

    Cpanel::Api1::Park::park

    You can find documentation on hooks with cPanel functions at:

    Guide to Standardized Hooks - Cpanel Functions - Software Development Kit - cPanel Documentation

    As far as checking for conditions before a hook is ran, an example is documented at:

    Guide to Standardized Hooks - Checks - Software Development Kit - cPanel Documentation

    Thank you.
     
  5. rahnev

    rahnev Active Member

    Joined:
    Jul 6, 2016
    Messages:
    30
    Likes Received:
    1
    Trophy Points:
    8
    Location:
    Bulgaria
    cPanel Access Level:
    Root Administrator
    @cPanelMichael, Thanks for the info but I am searching for an example hook, written in bash script. I want to know how can I catch the input and I will find a way to parse it for my needs.
     
  6. Anoop P Alias

    Anoop P Alias Well-Known Member

    Joined:
    Mar 31, 2015
    Messages:
    65
    Likes Received:
    6
    Trophy Points:
    8
    Location:
    Kochi,Kerala,India
    cPanel Access Level:
    Root Administrator
    Hi please find an example of a hook i wrote in python
    raw.githubusercontent.com/AnoopAlias/XtendWeb/ndeploy4/scripts/cpanel_functions_hook_post.py

    As mentioned before ..doing the same would be a bit tough in bash since bash does not support json. You may get some luck with text processing tools like sed or awk , but thats just a workaround and the easier way to do this would be using a high level language that support json. Perl,php,python,ruby etc would be easier here .
     
    cPanelMichael likes this.
Loading...

Share This Page