Hook to enable IPv6 after account creation

snewton

Active Member
Aug 4, 2015
41
7
58
Redford, MI
cPanel Access Level
Root Administrator
What I want to do is enable IPv6 by default on accounts when they are created.

I've registered a hook to run the following script, post account creation:

Code:
#!/bin/sh
/usr/local/cpanel/bin/whmapi1 ipv6_enable_account user=$user range=SHARED
It doesn't appear to do anything upon account creation, so I enabled debugging and in the cPanel error log I get:

Code:
metadata:
  command: ipv6_enable_account
  reason: "Missing parameter:  user"
  result: 0
  version: 1
So it appears $user is not being passed.

Any suggestions to get this to work? A different method perhaps?

Thanks in advance!
 

snewton

Active Member
Aug 4, 2015
41
7
58
Redford, MI
cPanel Access Level
Root Administrator
What I ended up doing was changing the script to:

Code:
#!/bin/bash
ls -1 /var/cpanel/users | while read USER; do
/usr/local/cpanel/bin/whmapi1 ipv6_enable_account user=${USER} range=SHARED;
done
Now when a new account is created, it enables IPv6 for all accounts.

I had existing accounts, so the new test account I created took a while to create since it was enabling IPv6 on all the other accounts.

Going to "Assign IPv6 Address" in WHM and enabling all existing accounts prior would probably be preferred if you have existing accounts.

After all accounts have IPv6 enabled, the new account creation runs quickly.

Again, if anyone has any suggestions for a better way of doing it, I'd love to hear.

Thanks!
 
  • Like
Reactions: cPanelTJ

cPanelTJ

Product Owner
Staff member
Jan 29, 2019
95
45
93
Houston, TX
cPanel Access Level
Root Administrator
Twitter
I needed a refresher on using a bash script with a hook, but it appears there isn't really a clear and easy way to get the event's variables into a bash script. Are you comfortable using Perl, PHP, or another language?
 
Last edited:

plesk4lyf

Active Member
PartnerNOC
May 21, 2018
32
4
8
Sydney
cPanel Access Level
Root Administrator
Code:
#!/bin/sh
/usr/local/cpanel/bin/whmapi1 ipv6_enable_account user=$user range=SHARED
Are you sure that $user is a valid variable?

From what the developer documentation has, it sounds like your code needs to parse JSON to get the user first:

Important:
  • Hook action code must read the entire input stream until end of line (EOL) and it must treat it as a JSON-encoded data structure. After the system decodes the JSON string, the native data structure is a hash.
  • Scripts may use any language that the Linux shell can execute.
 

snewton

Active Member
Aug 4, 2015
41
7
58
Redford, MI
cPanel Access Level
Root Administrator
Code:
#!/bin/sh
/usr/local/cpanel/bin/whmapi1 ipv6_enable_account user=$user range=SHARED
Are you sure that $user is a valid variable?

From what the developer documentation has, it sounds like your code needs to parse JSON to get the user first:

Important:
  • Hook action code must read the entire input stream until end of line (EOL) and it must treat it as a JSON-encoded data structure. After the system decodes the JSON string, the native data structure is a hash.
  • Scripts may use any language that the Linux shell can execute.
Nope, I assumed it was, but found that it wasn't, which is why I opened this thread to see if anyone had any suggestions.
 

plesk4lyf

Active Member
PartnerNOC
May 21, 2018
32
4
8
Sydney
cPanel Access Level
Root Administrator
Hey @snewton,

This will do what you want.

1. Make a file /scripts/postwwwacct

2. Put this into it:
Code:
#!/bin/bash

ARRAY=([email protected])
ARRAYLENGTH=${#ARRAY[@]}
for (( ARG=0 ; ARG < $ARRAYLENGTH ; ARG++ ))
do

if [ "${ARRAY[$ARG]}" = "user" ] ; then
    USERNAME=${ARRAY[$ARG +1]}
    test -f /var/cpanel/users/$USERNAME && /usr/local/cpanel/bin/whmapi1 ipv6_enable_account user="$USERNAME" range=SHARED
fi

done
3. Make it executable with # chmod +x /scripts/postwwwacct

When you make a test account, you should see the whmapi1 output under "Running post creation scripts".

I've tested it on WHM82 and it works fine.

This uses the old "script hooks" method:

It's a deprecated method ( so cPanel will probably cry ) but still works. It'll give you time to work out whatever the new method is.
 

cPanelTJ

Product Owner
Staff member
Jan 29, 2019
95
45
93
Houston, TX
cPanel Access Level
Root Administrator
Twitter
The Standardized Hooks method can be just as straightforward, if not even more simple. Using the describe() method, it could look something like this:

Perl:
#!/usr/local/cpanel/3rdparty/bin/perl

package NewAccountHook;

use strict;
use warnings;

sub describe {
    my $hook = {
        'category' => 'Whostmgr',
        'event' => 'Accounts::Create',
        'stage' => 'post',
        'hook' => 'NewAccountHook::actions',
        'exectype' => 'module',
    };
    return $hook;
}

sub actions {

    my ( $context, $data ) = @_;
    my $user = $data->{'user'};

    system("whmapi1 ipv6_enable_account user=$user range=SHARED --output=json");
}

1;
If anything related to using Standardized Hooks feels daunting, I sincerely would appreciate your feedback. We hope to take a look at making this system more intuitive to use in the near future.
 

snewton

Active Member
Aug 4, 2015
41
7
58
Redford, MI
cPanel Access Level
Root Administrator
The Standardized Hooks method can be just as straightforward, if not even more simple. Using the describe() method, it could look something like this:

Perl:
#!/usr/local/cpanel/3rdparty/bin/perl

package NewAccountHook;

use strict;
use warnings;

sub describe {
    my $hook = {
        'category' => 'Whostmgr',
        'event' => 'Accounts::Create',
        'stage' => 'post',
        'hook' => 'NewAccountHook::actions',
        'exectype' => 'module',
    };
    return $hook;
}

sub actions {

    my ( $context, $data ) = @_;
    my $user = $data->{'user'};

    system("whmapi1 ipv6_enable_account user=$user range=SHARED --output=json");
}

1;
If anything related to using Standardized Hooks feels daunting, I sincerely would appreciate your feedback. We hope to take a look at making this system more intuitive to use in the near future.
Thanks @cPanelTJ

What we be the proper method of registering it?
 

cPanelTJ

Product Owner
Staff member
Jan 29, 2019
95
45
93
Houston, TX
cPanel Access Level
Root Administrator
Twitter
Simply:

/usr/local/cpanel/bin/manage_hooks add module NewAccountHook

You'll need to ensure that the perl module is in any of the following directories:
Code:
/opt/cpanel/perl5/514/site_lib/x86_64-linux-64int
/opt/cpanel/perl5/514/site_lib
/var/cpanel/perl5/lib
(reference step 10 in this tutorial: Create a Standardized Hook)
 
Last edited:

cPanelTJ

Product Owner
Staff member
Jan 29, 2019
95
45
93
Houston, TX
cPanel Access Level
Root Administrator
Twitter
Excellent to hear! I did trim down the directory list in my previous post to show the supported directories that we'd recommend storing this module. Speaking with a developer internally, /usr/local/cpanel/ may have some risk in future updates where your file could be overwritten or cause conflict with newly added functionality.
 

snewton

Active Member
Aug 4, 2015
41
7
58
Redford, MI
cPanel Access Level
Root Administrator
Excellent to hear! I did trim down the directory list in my previous post to show the supported directories that we'd recommend storing this module. Speaking with a developer internally, /usr/local/cpanel/ may have some risk in future updates where your file could be overwritten or cause conflict with newly added functionality.
I did in fact use "/usr/local/cpanel" too! :D

I've changed it to "/var/cpanel/perl5/lib".

One last thing. When creating an account in WHM manually, I see the following towards the top of the output now:

Code:
{"data":{"ipv6":{"testacc":"<ipaddress-removed>"},"failures":{},"fail_cnt":0},"metadata":{"result":1,"reason":"OK","version":1,"command":"ipv6_enable_account"}}
Any chances of suppressing that?

Again, thanks for all your help!