#!/bin/bash -x set -e usage() { echo "Usage: $0 [-h ] [ -s domain ] [ -l | -v | -u userid ]" >&2 exit 0 } if [ $# -lt 3 ]; then echo "not enough arguments" usage fi while getopts ":hs:lu:v" opt; do case "$opt" in h) echo "Usage:" echo " $0 -h Display this help message." echo " $0 -s The website is a mandatory argument." echo " $0 -s -l List WordPress users." echo " $0 -s -v Display the WordPress version" echo " $0 -s -u Generate and set a random password for this WordPress userid." exit 0 ;; s) WEB=$OPTARG ;; l) LIST=1 ;; u) USERID=$OPTARG ;; v) VERSION=1 ;; \?) echo "Error: Invalid Option: -${OPTARG}" >&2 usage ;; :) echo "Error: -${OPTARG} requires an argument." usage ;; *) usage ;; esac done if [ -z ${WEB} ]; then usage fi # declaration section # functions section check_err() { STATUS=$? if [ ${STATUS} -ne 0 ]; then echo "Unexpected error: ${STATUS}" exit ${STATUS} fi } function get_sysuser() { SYSUSER=`ls -l /var/www/ | awk '$9 ~ /^'${WEB}'$/ { print $11 }' | awk -F'/' '{ print $6 }'` if [ -z ${SYSUSER} ]; then echo "SYSUSER contains nothing!" exit 1 fi echo ${SYSUSER} } function get_rootpath() { ROOTPATH="/var/www/${WEB}" echo ${ROOTPATH} } function get_php() { VERSION=`echo "select server_php.name from server_php join web_domain where server_php.server_php_id=web_domain.server_php_id and web_domain.server_php_id != 0 and web_domain.domain like '${WEB}'\G" | mysql --defaults-file=/etc/mysql/debian.cnf dbispconfig | awk '$1 ~ /:/ { print $2 }'` if [ -z "${VERSION}" ]; then echo "/usr/bin/php" else echo "/opt/php-${VERSION}/bin/php" fi } function change_user_password() { MYUSER=$(get_sysuser) MYPATH=$(get_rootpath) MYPHP=$(get_php) chsh -s /bin/bash ${MYUSER} cp /root/bin/wp-cli.phar ${MYPATH}/private NEWPASS=`openssl rand -base64 12` su -c "cd web && ${MYPHP} ../private/wp-cli.phar user update ${USERID} --user_pass=${NEWPASS}" - ${MYUSER} rm ${MYPATH}/private/wp-cli.phar chsh -s /bin/false ${MYUSER} echo ${NEWPASS} } function list_wp_users() { MYUSER=$(get_sysuser) MYPATH=$(get_rootpath) MYPHP=$(get_php) chsh -s /bin/bash ${MYUSER} cp /root/bin/wp-cli.phar ${MYPATH}/private su -c "cd web && ${MYPHP} ../private/wp-cli.phar user list" - ${MYUSER} rm ${MYPATH}/private/wp-cli.phar chsh -s /bin/false ${MYUSER} } function show_wp_version() { MYUSER=$(get_sysuser) MYPATH=$(get_rootpath) MYPHP=$(get_php) chsh -s /bin/bash ${MYUSER} cp /root/bin/wp-cli.phar ${MYPATH}/private su -c "cd web && ${MYPHP} ../private/wp-cli.phar core version --extra" - ${MYUSER} rm ${MYPATH}/private/wp-cli.phar chsh -s /bin/false ${MYUSER} } # main section if [ "${LIST}" == 1 ]; then list_wp_users exit elif [ "${VERSION}" == 1 ]; then show_wp_version exit else change_user_password fi