#!/bin/bash # info: interactively delete or change WordPress admin passwords for a given domain # options: DOMAIN # # d → delete user (with content reassignment) # c → change password (random 10-char alnum) # s → skip # x → exit #----------------------------------------------------------# # Variable & Function # #----------------------------------------------------------# [ "$(whoami)" != "root" ] && { echo "You must be root to run this command."; exit 1; } source /etc/profile DOMAIN="$1" [ -z "$DOMAIN" ] && { echo "Usage: v-change-wp-admins-pass DOMAIN"; exit 1; } USER="$(/usr/local/vesta/bin/v-search-domain-owner "$DOMAIN")" [ -z "$USER" ] && { echo "Domain $DOMAIN does not exist."; exit 1; } WP_PATH="/home/$USER/web/$DOMAIN/public_html" [ ! -f "$WP_PATH/wp-config.php" ] && { echo "WordPress is not installed on this domain."; exit 1; } # WP-CLI wrapper WP_RUN=(/usr/local/vesta/bin/v-run-wp-cli $DOMAIN --skip-plugins --skip-themes) return_code=$? if [ $return_code -ne 0 ]; then echo "WP-CLI error:" cat /home/$USER/web/$DOMAIN/wp-cli-error.log exit $return_code fi # random 10-char password gen_pass() { tr -dc 'A-Za-z0-9' /dev/null | tail -n +2) [ -z "$ADMIN_LIST_CSV" ] && { echo "No administrator accounts found."; exit 0; } printf "%-6s %-20s %s\n" "ID" "Username" "Email" echo "$ADMIN_LIST_CSV" | while IFS=',' read -r PID PLOGIN PEMAIL; do printf "%-6s %-20s %s\n" "$PID" "$PLOGIN" "$PEMAIL" done echo echo "For each admin choose: (d) delete, (c) change password, (s) skip, (x) exit." # interactive loop while IFS=',' read -r ID LOGIN EMAIL; do [ -n "$EMAIL" ] && TARGET="$LOGIN <$EMAIL>" || TARGET="$LOGIN" while true; do echo "-------------------------------------" read -r -p "Action for \"$TARGET\" [d/c/s/x]? " ACT < /dev/tty case "$ACT" in [Dd]* ) # read -r -p "Really DELETE \"$TARGET\" ? (y/n, default: y) " CONF < /dev/tty CONF="y" if [[ ! "$CONF" =~ ^[Nn]$ ]]; then # build an array of OTHER admin usernames mapfile -t OTHER_USERS < <(echo "$ADMIN_LIST_CSV" | awk -F',' -v cur="$ID" '$1!=cur {print $2}') if [ "${#OTHER_USERS[@]}" -eq 0 ]; then echo "Cannot delete the only administrator account." break fi DEFAULT_USER="${OTHER_USERS[0]}" echo "Available admin usernames for reassignment: ${OTHER_USERS[*]}" while true; do read -r -p "Reassign content to which username? [default: $DEFAULT_USER] " REASSIGN < /dev/tty REASSIGN=${REASSIGN:-$DEFAULT_USER} if printf '%s\n' "${OTHER_USERS[@]}" | grep -qx "$REASSIGN"; then break else echo "Invalid username. Please choose one of: ${OTHER_USERS[*]}" fi done # delete by username, reassign by username "${WP_RUN[@]}" user delete "$LOGIN" --reassign="$REASSIGN" --yes --skip-plugins --skip-themes if [ $? -eq 0 ]; then echo "$TARGET deleted (content reassigned to $REASSIGN)." else cat /home/$USER/web/$DOMAIN/wp-cli-error.log echo "Failed to delete $TARGET." fi else echo "Deletion cancelled." fi break ;; [Cc]* ) NEW_PASS=$(gen_pass) "${WP_RUN[@]}" user update "$LOGIN" --user_pass="$NEW_PASS" --skip-plugins --skip-themes if [ $? -eq 0 ]; then echo "Password for username '$TARGET' changed to: $NEW_PASS" echo "Password for username '$TARGET' changed to: $NEW_PASS" >> /home/$USER/web/$DOMAIN/wp-admin-password-change.txt chown $USER:$USER /home/$USER/web/$DOMAIN/wp-admin-password-change.txt chmod 600 /home/$USER/web/$DOMAIN/wp-admin-password-change.txt else cat /home/$USER/web/$DOMAIN/wp-cli-error.log echo "Failed to change password for $TARGET." fi break ;; [Ss]* ) echo "Skipping $TARGET." break ;; [Xx]* ) echo "Exiting." exit 0 ;; * ) echo "Please answer d, c, s, or x." ;; esac done done <<< "$ADMIN_LIST_CSV" #----------------------------------------------------------# # flush cache and refresh all security salts # #----------------------------------------------------------# echo "-------------------------------------" echo echo "Flushing cache and refreshing salts..." "${WP_RUN[@]}" cache flush "${WP_RUN[@]}" config shuffle-salts WP_CACHE_KEY_SALT --force "${WP_RUN[@]}" config shuffle-salts echo "Cache flushed and salts refreshed." echo echo "Done." if [ -f /home/$USER/web/$DOMAIN/wp-admin-password-change.txt ]; then echo "-------------------------------------" echo "For website $DOMAIN - new wp-admin passwords have been set." echo "-------------------------------------" cat /home/$USER/web/$DOMAIN/wp-admin-password-change.txt echo "-------------------------------------" echo "" read -r -p "== Press Enter to continue..." fi exit 0