HEX
Server: nginx/1.18.0
System: Linux m1-ws1-ams3 5.4.0-148-generic #165-Ubuntu SMP Tue Apr 18 08:53:12 UTC 2023 x86_64
User: root (0)
PHP: 7.4.33
Disabled: NONE
Upload Files
File: /opt/aphex/scripts/extract_admins.sh
#!/bin/bash

# File containing the list of domains
DOMAIN_LIST_FILE="sitelst.txt"
# Output CSV file
OUTPUT_CSV_FILE="/opt/aphex/scripts/admin_usernames.csv"

# Check if the domain list file exists
if [[ ! -f "$DOMAIN_LIST_FILE" ]]; then
    echo "Domain list file $DOMAIN_LIST_FILE not found!"
    exit 1
fi

# Initialize the CSV file with headers
echo "Domain,Admin Username" > "$OUTPUT_CSV_FILE"

# Read each domain from the file
while IFS= read -r domain; do
    # Trim whitespace
    domain=$(echo "$domain" | xargs)
    
    # Check if the directory for the domain exists
    if [[ -d "/opt/aphex/sites/$domain" ]]; then
        # Navigate to the WordPress installation directory
	echo "$domain"
        cd "/opt/aphex/sites/$domain" || continue
        
        # Check if WP-CLI is available and fetch the admin username
        if wp --allow-root core is-installed --path=/opt/aphex/sites/$domain; then
            admin_user=$(wp --allow-root user list --role=administrator --field=user_login --path=/opt/aphex/sites/$domain | head -n 1 2>&1)
	    echo "$admin_user"
            echo "$domain,$admin_user" >> "$OUTPUT_CSV_FILE"
	    if [[ $? -ne 0 ]]; then
               echo "Failed to write to CSV file: $OUTPUT_CSV_FILE"
            fi
        else
            admin_user=""
	    echo "$domain,$admin_user" >> "$OUTPUT_CSV_FILE"
	    if [[ $? -ne 0 ]]; then
               echo "Failed to write to CSV file: $OUTPUT_CSV_FILE"
            fi
        fi
        
        # Write the result to the CSV file
  
    else
        # Domain directory does not exist
        echo "$domain," >> "$OUTPUT_CSV_FILE"
    fi
done < "$DOMAIN_LIST_FILE"

echo "Processing complete. Results are saved in $OUTPUT_CSV_FILE."