Created
January 14, 2025 02:05
-
-
Save tanfwc/9c036dbf89244ab63f7f2cf1f4acf65f to your computer and use it in GitHub Desktop.
Script to mass exclude sub-domain for cpanel autossl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Function to exclude subdomains from AutoSSL for a specific user | |
exclude_autossl_domains() { | |
USER=$1 | |
# Get the main domain for the user using uapi | |
DOMAIN=$(uapi --user="$USER" DomainInfo list_domains | awk '/main_domain:/ {print $2}' | tr -d '"') | |
# Handle case if the domain extraction fails | |
if [ -z "$DOMAIN" ]; then | |
echo "Error: No main domain found for user $USER." | |
return 1 | |
fi | |
# Get any addon domains (empty list handled gracefully) | |
ADDON_DOMAINS=$(uapi --user="$USER" DomainInfo list_domains | awk '/addon_domains:/ {getline; print $0}' | tr -d '[]" ,') | |
# Get any subdomains (empty list handled gracefully) | |
SUB_DOMAINS=$(uapi --user="$USER" DomainInfo list_domains | awk '/sub_domains:/ {getline; print $0}' | tr -d '[]" ,') | |
# Combine all domains into a single string | |
DOMAINS="$DOMAIN $ADDON_DOMAINS $SUB_DOMAINS" | |
# Prepare the list of system subdomains for exclusion | |
EXCLUDED_DOMAINS=() | |
for DOMAIN in $DOMAINS; do | |
EXCLUDED_DOMAINS+=("cpanel.$DOMAIN") | |
EXCLUDED_DOMAINS+=("webdisk.$DOMAIN") | |
EXCLUDED_DOMAINS+=("cpcontacts.$DOMAIN") | |
EXCLUDED_DOMAINS+=("cpcalendars.$DOMAIN") | |
done | |
# Convert the array to a comma-separated string | |
EXCLUDED_DOMAINS_STR=$(IFS=,; echo "${EXCLUDED_DOMAINS[*]}") | |
# Add exclusions via UAPI | |
echo "Excluding the following domains for user $USER: $EXCLUDED_DOMAINS_STR" | |
uapi --user="$USER" SSL add_autossl_excluded_domains domains="$EXCLUDED_DOMAINS_STR" | |
} | |
# Get the list of all users from the `whmapi1 list_users` command output | |
USERS=$(whmapi1 list_users | grep -A 10 'data:' | grep -oP '^\s*- \K\w+') | |
# Loop through each user and exclude AutoSSL for their system subdomains | |
for USER in $USERS; do | |
# Exclude AutoSSL subdomains for this user | |
exclude_autossl_domains "$USER" | |
done | |
echo "AutoSSL exclusions completed for all users." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment