4 * WordPress Site Inventory Scanner
5 * Scans WHM server via SSH for WordPress installations
8const { exec } = require('child_process');
9const fs = require('fs');
10const path = require('path');
11const util = require('util');
12const execPromise = util.promisify(exec);
14const SSH_KEY = `${process.env.HOME}/PreciseITServices.pem`;
15const SSH_USER = 'centos';
16const WHM_HOST = 'precisewebhosting.com.au';
17const OUTPUT_DIR = 'exports/wordpress-inventory';
19// Ensure output directory exists
20if (!fs.existsSync(OUTPUT_DIR)){
21 fs.mkdirSync(OUTPUT_DIR, { recursive: true });
24console.log('========================================');
25console.log('WordPress Site Inventory Scanner');
26console.log('========================================\n');
31async function scanWordPressSites() {
32 console.log('š Scanning for WordPress installations via SSH...\n');
34 // Find all wp-config.php files
35 const findCmd = `ssh -i "${SSH_KEY}" ${SSH_USER}@${WHM_HOST} "find /home*/*/public_html -maxdepth 1 -name 'wp-config.php' 2>/dev/null || true"`;
39 const { stdout } = await execPromise(findCmd);
40 wpConfigPaths = stdout.trim().split('\n').filter(p => p);
42 console.error('ā Failed to scan for WordPress sites:', error.message);
46 console.log(` Found ${wpConfigPaths.length} WordPress installations\n`);
48 if (wpConfigPaths.length === 0) {
49 fs.writeFileSync(path.join(OUTPUT_DIR, 'wordpress_sites.json'), JSON.stringify([], null, 2));
50 console.log('ā
No WordPress sites found\n');
56 for (const wpConfigPath of wpConfigPaths) {
57 const wpPath = path.dirname(wpConfigPath);
58 const username = wpPath.split('/')[2];
60 console.log(` Processing: ${username} (${wpPath})`);
63 // Extract data using remote commands
64 const extractCmd = `ssh -i "${SSH_KEY}" ${SSH_USER}@${WHM_HOST} bash << 'EOF'
66WP_CONFIG="\$WP_PATH/wp-config.php"
69DB_NAME=\$(grep "DB_NAME" "\$WP_CONFIG" | head -1 | sed -e "s/.*['\\"]\(.*\)['\\"].*/\\1/")
70DB_USER=\$(grep "DB_USER" "\$WP_CONFIG" | head -1 | sed -e "s/.*['\\"]\(.*\)['\\"].*/\\1/")
71DB_PASS=\$(grep "DB_PASSWORD" "\$WP_CONFIG" | head -1 | sed -e "s/.*['\\"]\(.*\)['\\"].*/\\1/")
72DB_HOST=\$(grep "DB_HOST" "\$WP_CONFIG" | head -1 | sed -e "s/.*['\\"]\(.*\)['\\"].*/\\1/" || echo "localhost")
73TABLE_PREFIX=\$(grep "table_prefix" "\$WP_CONFIG" | sed -e "s/.*['\\"]\(.*\)['\\"].*/\\1/" || echo "wp_")
75# Get WordPress version
76WP_VERSION=\$(grep "wp_version" "\$WP_PATH/wp-includes/version.php" | head -1 | sed -e "s/.*['\\"]\(.*\)['\\"].*/\\1/" || echo "unknown")
79SIZE=\$(du -sh "\$WP_PATH" 2>/dev/null | cut -f1 || echo "unknown")
81# Get site URL from database
82SITE_URL=\$(mysql -h"\$DB_HOST" -u"\$DB_USER" -p"\$DB_PASS" -D"\$DB_NAME" -N -B -e "SELECT option_value FROM \${TABLE_PREFIX}options WHERE option_name='siteurl' LIMIT 1" 2>/dev/null || echo "unknown")
86grep -q "MULTISITE" "\$WP_CONFIG" 2>/dev/null && IS_MULTISITE="true"
91 "db_name": "\$DB_NAME",
92 "db_user": "\$DB_USER",
93 "db_password": "\$DB_PASS",
94 "db_host": "\$DB_HOST",
95 "table_prefix": "\$TABLE_PREFIX",
96 "wp_version": "\$WP_VERSION",
98 "site_url": "\$SITE_URL",
99 "multisite": \$IS_MULTISITE
104 const { stdout } = await execPromise(extractCmd);
105 const data = JSON.parse(stdout.trim());
110 location: 'public_html',
111 version: data.wp_version,
112 site_url: data.site_url,
114 multisite: data.multisite,
116 db_name: data.db_name,
117 db_user: data.db_user,
118 db_password: data.db_password,
119 db_host: data.db_host,
120 table_prefix: data.table_prefix
124 console.log(` ā
${data.site_url} (${data.wp_version}) - ${data.size}`);
127 console.log(` ā ļø Failed to extract data: ${error.message}`);
132 const outputFile = path.join(OUTPUT_DIR, 'wordpress_sites.json');
133 fs.writeFileSync(outputFile, JSON.stringify(sites, null, 2));
135 console.log('\n========================================');
136 console.log(`ā
WordPress inventory saved to: ${outputFile}`);
137 console.log('========================================\n');
139 console.log('š Summary:');
140 console.log(` WordPress sites found: ${sites.length}\n`);
142 if (sites.length > 0) {
143 console.log('Sites:');
144 sites.forEach(site => {
145 console.log(` ⢠${site.site_url} (${site.version}) - ${site.size} - ${site.path}`);
150 console.log('ā
Inventory complete!');
151 console.log(`š Output: ${OUTPUT_DIR}/\n`);
152 console.log('Next steps:');
153 console.log('1. Review wordpress_sites.json');
154 console.log('2. Run migrate_wordpress_to_github.sh to create repos and migrate\n');
157scanWordPressSites().catch(error => {
158 console.error('\nā Error:', error.message);