Skip to content

Instantly share code, notes, and snippets.

@mttjohnson
Last active February 25, 2020 10:03
Show Gist options
  • Save mttjohnson/0a0fbd34e3b91c6355a8a0dbb0ba2cb6 to your computer and use it in GitHub Desktop.
Save mttjohnson/0a0fbd34e3b91c6355a8a0dbb0ba2cb6 to your computer and use it in GitHub Desktop.
Magento Version Site Checker
#!/usr/bin/env bash
set -eu
########################################
## Introduction
########################################
HELP_INFO=$(cat <<'CONTENTS_HEREDOC'
sitecheck v0.1
Copyright (c) 2019, Matt Johnson ([email protected]). All
rights reserved.
Magento Version Site Checker
https://gist.github.com/mttjohnson/0a0fbd34e3b91c6355a8a0dbb0ba2cb6
Program for checking the Magento version of a site or list of sites
provided the base_url of the Magento site has been provided.
Usage: sitecheck [options]
-u=magento_base_url, --url=magento_base_url Magento Base URL.
-f=file_name, --file=file_name File with URLs on each line
Examples:
# Check a single site
echo "https://www.example.com/" | sitecheck
sitecheck -u=https://www.example.com/
sitecheck --url=https://www.example.com/
# Check multiple sites
echo "
https://www.example.com/
https://www.example2.com/
https://www.example3.com/
" | sitecheck
cat ./file_name | sitecheck
sitecheck -f=./file_name
sitecheck --file=./file_name
CONTENTS_HEREDOC
)
########################################
## Command Line Options
########################################
declare MAGENTO_BASE_URL=""
declare FLAG_MAGENTO_BASE_URL=false
declare FILE=""
declare FLAG_FILE=false
declare FLAG_INPUT=false
for switch in $@; do
case $switch in
-u=*|--url=*)
MAGENTO_BASE_URL="${switch#*=}"
if [[ ! "${MAGENTO_BASE_URL}" =~ ^.+$ ]]; then
>&2 echo "Error: Invalid value given -u|--url=${MAGENTO_BASE_URL}"
exit -1
fi
FLAG_MAGENTO_BASE_URL=true
;;
-f=*|--file=*)
FILE="${switch#*=}"
if [[ ! "${FILE}" =~ ^.+$ ]]; then
>&2 echo "Error: Invalid value given -f|--file=${FILE}"
exit -1
fi
if ! [ -f "${FILE}" ]; then
>&2 echo "Error: No file found at location specified"
exit -1
fi
FLAG_FILE=true
;;
*)
echo "${HELP_INFO}"
exit;
;;
esac
done
########################################
## Test for piped input
########################################
if [ -t 0 ]; then # check if STDIN is a terminal
if [[ ${FLAG_FILE} == true ]]; then
if [ ! -z "${FILE}" ]; then # is not empty
INPUT=$(< ${FILE});
FLAG_INPUT=true
else
>&2 echo "Error: No configuration input given!"
exit -1
fi
fi
else # expecting STDIN to be piped data
INPUT=$(< /dev/stdin);
if [ ! -z "${INPUT}" ]; then # is not empty
FLAG_INPUT=true
fi
fi
########################################
## Build list of sites to check
########################################
declare -a SITES_TO_CHECK=()
if [[ ${FLAG_MAGENTO_BASE_URL} == true ]]; then
SITES_TO_CHECK+=("${MAGENTO_BASE_URL}")
fi
if [[ ${FLAG_INPUT} == true ]]; then
# Read each line from input and add to site array
while IFS='' read -r LINE_FROM_INPUT || [[ -n "${LINE_FROM_INPUT}" ]]; do
[ -z "${LINE_FROM_INPUT}" ] && continue
SITES_TO_CHECK+=("${LINE_FROM_INPUT}")
done <<< "${INPUT}"
fi
########################################
## Check list of sites
########################################
for EACH_SITE in "${SITES_TO_CHECK[@]}"
do
FORMATTED_BASE_URL=$(echo "${EACH_SITE}" | tr -d '[:space:]') #remove any whitespace from variable
FORMATTED_BASE_URL=$(echo "${EACH_SITE}" | perl -nle 's/(.+[^\/])$/$1\//;print') #ensure url ends with a slash
MAGENTO_VERSION=$(curl -s ${FORMATTED_BASE_URL}magento_version)
echo "${MAGENTO_VERSION} - ${FORMATTED_BASE_URL}"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment