Find All WordPress Installations and List by Version then Send Sorted CSV to your Email


This Script will find all Wordpress installations on your server. It will then email you a sorted list in CSV format.
In the current form the script is designed to be run from the command line. It can be easily modified to run from cron.
If you need the indented/easy-to-read version please contact our support.

SCRIPT

#/bin/bash

#########################################################
# #
# #
# Find Wordpress Script #
# 8Dweb LLC #
# 04-14-2016 #
# #
#########################################################


# Declare Variables for Script
LATEST=4.5

# BASEDIR set here - can be changed via input later...
BASEDIR=/chroot/home


echo ""
echo ""
echo "Please Enter the base path for websites. " ;
echo -e "Leave blank for default path \e[36m\e[1m/chroot/home\e[0m:" ;
read BASEPATHINPUT
if [[ -d "$BASEPATHINPUT" ]] ; then
echo -e "\e[36m\e[1m$BASEPATHINPUT \e[0mseems to be a " ;
echo "legitimate directory on this server." ;
echo -e "\e[32m\e[1mProceeding...\e[0m" ;
BASEPATH=$BASEPATHINPUT ;
else
# default base path of the websites on YOUR server
# Edit If you want to set a different default base path
echo -e "Your directory entry \e[31m\e[1m$BASEPATHINPUT is empty or invalid.\e[0m" ;
echo -e "Now using default \e[36m\e[1m/chroot/home\e[0m" ;
echo -e "\e[32m\e[1mProceeding...\e[0m" ;
BASEPATH="/chroot/home/" ;
fi

# Obtain a filename for sorting and CSV output
echo ""
echo ""
echo "Please Enter the filename for output from this script." ;
echo -e "Leave Blank for default filename \e[36m\e[1m>>wordpress<<\e[0m to be used:" ;
read OUTFILEINPUT
if [[ "$OUTFILEINPUT" != "" ]] ; then
echo "File \"$OUTFILEINPUT\" will be used by this script." ;
echo "If you used "wordpress" a sorted CSV will be emailed to you." ;
echo -e "\e[32m\e[1mProceeding...\e[0m" ;
OUTFILE=$OUTFILEINPUT ;
else
echo "No output file specified." ;
echo -e "Using default \e[36m\e[1mwordpress\e[0m filename." ;
echo -e "\e[32m\e[1mProceeding...\e[0m" ;
OUTFILE="wordpress" ;
fi

# Obtain Email Address to which CSV can be sent
echo ""
echo ""

# Getmail Function - called by while loop...
getemail() {
echo "Please Enter a valid Email Address:" ;
echo "Example: joe@somedomain.com" ;
echo -e "Type \e[36m\e[1mNO\e[0m to bypass..." ;
read EMAIL
}

# While Loop checks for SIMPLE validation of email (i.e. jo@domain.com)
# Thanks to Stackoverflow.com for REGEX Ideas for simple email validation
# The regex is not perfect, but works for admins because we are supposed to be more intelligent....
mailregex="^[a-z0-9!#\$%&'*+/=?^_\`{|}~-]+(\.[a-z0-9!#$%&'*+/=?^_\`{|}~-]+)*@([a-z0-9]([a-z0-9-]*[a-z0-9])?\.)+[a-z0-9]([a-z0-9-]*[a-z0-9])?\$"

# While Loop to make sure a good email address is entered
while :; do
getemail
#echo $EMAIL
if [[ $EMAIL == "NO" ]] ; then
echo "" ;
echo "" ;
echo "You have chosen to NOT" ;
echo "enter an Email address." ;
echo -e "\e[32m\e[1mProceeding...\e[0m" ;
break ;
elif [[ $EMAIL =~ $mailregex ]] ; then
echo ""
echo ""
echo -e "Email address \e[36m\e[1m>> $EMAIL <<\e[0m seems valid. " ;
echo -e "\e[32m\e[1mProceeding....\e[0m" ;
break ;
else
echo "" ;
echo "" ;
echo -e "Email address \e[31m\e[1m>> $EMAIL << is INVALID.\e[0m" ;
echo "Please try again!" ;
fi
done

# First If statement checks to see if output filename was supplied when script was run
# Special checking for file named wordpress so extra processing can occur
# for sorting and mailing of results as CSV file
# Cleanup any old files - remember you can place this in any folder so you need to
# Change the /root/scripts/ to what meets your requirements...
if [ -f /root/scripts/wordpress ] ; then
echo "" ;
echo "" ;
echo "Cleaing up old output files..." ;
echo " -Removing File\: /root/scripts/wordpress..." ;
echo " -Removing File\: wordpress.sorted.csv..." ;

# If you run from another directory please change rm statement
# These files are created on a stock run-through so should be deleted
# automatically each time. Removal of files from previous runs
# Handled Here
rm -f /root/scripts/wordpress /root/scripts/wordpress.sorted.csv ;
echo "Finished Cleaning up old files..." ;
echo -e "\e[32m\e[1mProceeding...\e[0m" ;
fi

if [[ "$OUTFILE" != "" ]] ; then
# empty CSV file
echo -n "" > $OUTFILE ;
fi

# Add Counter Option for easy "line numbers" later...
# Counter also is checked so that first line(s) of output are correct.
# Set Counter to One (not zero)
COUNTER=1

echo ""
echo ""
echo "Searching for Wordpress Installs" ;
echo -e "\e[35m\e[1mPlease Wait...\e[0m" ;
echo ""
echo ""

# Wordpress - Unlike Joomla stores version information
# In the same file across versions (Joomal get your act together!)
# Primary For Loop to setup and Display Information

for L in `find ${BASEPATH} -type d -name 'wp-includes'` ; do

DIR=`dirname $L` ;
FILE=$DIR/wp-includes/version.php
# This version is a bit quicker but leaves the ' ' around version
#VERSION=`grep 'wp_version =' $FILE |cut -d"=" -f2` ;
# This version is slower but produces clean version (i.e 4.5)
VERSION=`grep '\$wp_version =' $FILE | sed -r "s/^.*=\s*'(.*)'.*$/\1/g"` ;
# Test echo
#echo "Directory=$DIR and File=$FILE and version=$VERSION" ;

# Extract the Site from full directory... (testing)
# Works for /chroot/home - cut -d"/" -f5 may need adjusting if
# other directory depths occur (possibly script this...)
SITE=$( echo $DIR | cut -d"/" -f5) ;
#echo $SITE ;

# Set/Reset Variables that display findings later in script
ISOK=0 ; # ISOK holds value of whether version is Up-to-date or not
SHOWNEWEST="" ; # SHOWNEWEST holds value of latest WP version
IMPORTANCE=0 ; # IMPORTANCE for quick visual reference

##MAIN IF BLOCK
# Tests for all versions under latest (4.5 as of 04-14-2016)
if [ "${VERSION}" \< "${LATEST}" ] ; then
ISOK=0 ;
SHOWNEWEST="${LATEST}" ; #redundant but leaving since it works
IMPORTANCE="WP-UPGRADE-REQUIRED" ;
# If WP install is the latest write out information to screen and OUTFILE
else
ISOK=1 ;
SHOWNEWEST="YOU GOT IT" ;
IMPORTANCE="OK" ;

# Echo out to Screen
# If This is the first time through lool then output header text to screen
if [ "${COUNTER}" == "1" ] ; then
echo "" ;
echo -e "\e[32m\e[1m*************************************" ;
echo "* WORDPRESS Installations Found *" ;
echo -e "*VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV*\e[0m" ;
echo "" ;
fi
#Echo WORDPRESS Information to Screen
echo -e "$COUNTER \e[31m\e[1m$VERSION\e[0m found in $SITE Latest=$SHOWNEWEST Status=$IMPORTANCE" ;

# Output to File if one is supplied when the script is run
if [[ "$OUTFILE" != "" ]] ; then
echo $VERSION,$SITE,$SHOWNEWEST,$IMPORTANCE >> $OUTFILE ;
fi

fi
##END MAIN IF BLOCK

##BEGIN IF BLOCK ISOK is FALSE - Writes Out-of-Date Joomla Info to Screen & OUTFILE
if [[ $ISOK -eq 0 ]] ; then

#If This is the first time through Loop then Output Header to Screen
if [ "${COUNTER}" == "1" ] ; then
echo "" ;
echo -e "\e[32m\e[1m*************************************" ;
echo "* Wordpress Installations Found *" ;
echo -e "*VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV*\e[0m" ;
echo "" ;
fi

# Echo Joomla Information Out to Screen
echo -e "$COUNTER \e[31m\e[1m$VERSION\e[0m found in $SITE Latest=$SHOWNEWEST Status=$IMPORTANCE" ;

# Output to File if one is supplied when the script is run
if [[ "$OUTFILE" != "" ]] ; then
# write CSV file
echo $VERSION,$SITE,$SHOWNEWEST,$IMPORTANCE >> $OUTFILE ;
fi

fi
##END IF BLOCK ISOK is FALSE

# Reset Variables for Next Loop
IMPORTANCE=""
# Increase Counter
((COUNTER++))

done

# Take OUTFILE name and add .sorted.csv for easy Excel / Spreadsheet manipulation
# Check if OUTFILE is available (remember it was added at beginning)
if [[ "$OUTFILE" != "" ]] ; then
# Echo Headers for CSV/EXCEL
echo Version,Site,Latest,Status >> $OUTFILE.sorted.csv ;

#Sort on 1st(WORDPRESS) and 2nd (directory listing) Column and write out to *.sorted.csv
sort -k1 -k2 $OUTFILE >> $OUTFILE.sorted.csv ;
fi

# If output file is named wordpress then email wordpress.sorted.csv file using mutt (must be installed)
if [ -f "/root/scripts/wordpress.sorted.csv" ] ; then

##BEGIN MAIL ROUTINE IF BLOCK
# Check to see if user set EMAIL to NO - if so then do not send email...
if [[ $EMAIL == "NO" ]] ; then
echo "" ;
echo "" ;
echo -e "\e[33m\e[1mNo Email will be sent...\e[0m" ;

else
echo "" ;
echo "" ;
echo -e "\e[32m\e[1mEmailing CSV File to \e[36m\e[1m>>$EMAIL<<\e[0m" ;
echo "Wordpress Version Listing from CP1." | mutt -s "Wordpress Version List" $EMAIL -a /root/scripts/wordpress.sorted.csv ;
fi
##END MAIL ROUTINE IF BLOCK
fi

echo ""
echo ""
echo -e "\e[31m\e[1m************************************************************"
echo -e "\e[31m\e[1m* //Find WordPress Installs SCRIPT FINISHED PROCESSING// *"
echo -e "\e[31m\e[1m************************************************************\e[0m"

# End of Script - Exit Properly...

exit 0 ;


Comments

Please login to comment