Skip to content

Instantly share code, notes, and snippets.

@soundstorm
Last active February 25, 2023 18:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save soundstorm/923f1bbe358899a0b73d12742f561098 to your computer and use it in GitHub Desktop.
Save soundstorm/923f1bbe358899a0b73d12742f561098 to your computer and use it in GitHub Desktop.
Quickly resalt all WordPress installation via Bash Script with no need to install any Addons
#!/bin/bash
SCRIPNAME=$0
function show_help() {
echo "Resalt Wordpress configurations without any plugins."
echo "Usage:"
echo " -f Use specific file to resalt ($SCRIPTNAME -f /var/www/html/wordpress/wp-config.php)"
echo " -d Use wp-config.php in given directory ($SCRIPTNAME -d /var/www/html/wordpress)"
echo " -a Use wp-config.php in all subdirectories of given directory ($SCRIPTNAME -a /var/www/html)"
echo " -h Show this help"
echo "Return values:"
echo "1 invalid option"
echo "2 not a file/directory"
echo "3 not a valid wp-config.php"
echo "4 WP API not reachable"
}
function resalt() {
WP_CONFIG=$1
grep -q "define('AUTH_KEY'," "$WP_CONFIG" &>/dev/null
if [[ "$?" -ne 0 ]]
then
>&2 echo "File is malformed (has no keys defined)"
return 3
fi
SALT=`curl -s https://api.wordpress.org/secret-key/1.1/salt/`
if [[ "$SALT" != define* ]]
then
>&2 echo "Wordpress Salt API not returning proper result"
return 4
fi
AUTHK=$(echo "$SALT" | head -n1 | sed -e 's/&/\\\&/g')
SAUTHK=$(echo "$SALT" | head -n2 | tail -n1 | sed -e 's/&/\\\&/g')
LOGGEDK=$(echo "$SALT" | head -n3 | tail -n1 | sed -e 's/&/\\\&/g')
NONCEK=$(echo "$SALT" | head -n4 | tail -n1 | sed -e 's/&/\\\&/g')
AUTHS=$(echo "$SALT" | head -n5 | tail -n1 | sed -e 's/&/\\\&/g')
SAUTHS=$(echo "$SALT" | head -n6 | tail -n1 | sed -e 's/&/\\\&/g')
LOGGEDS=$(echo "$SALT" | head -n7 | tail -n1 | sed -e 's/&/\\\&/g')
NONCES=$(echo "$SALT" | head -n8 | tail -n1 | sed -e 's/&/\\\&/g')
# Use Tabs as delimiter as all other standard ASCII chars are possible within the keys/salts
# See https://developer.wordpress.org/reference/functions/wp_generate_password/#more-information
sed -i "s define[(]'AUTH_KEY',\s*'[^']*'[)]; $AUTHK " "$WP_CONFIG"
sed -i "s define[(]'SECURE_AUTH_KEY',\s*'[^']*'[)]; $SAUTHK " "$WP_CONFIG"
sed -i "s define[(]'LOGGED_IN_KEY',\s*'[^']*'[)]; $LOGGEDK " "$WP_CONFIG"
sed -i "s define[(]'NONCE_KEY',\s*'[^']*'[)]; $NONCEK " "$WP_CONFIG"
sed -i "s define[(]'AUTH_SALT',\s*'[^']*'[)]; $AUTHS " "$WP_CONFIG"
sed -i "s define[(]'SECURE_AUTH_SALT',\s*'[^']*'[)]; $SAUTHS " "$WP_CONFIG"
sed -i "s define[(]'LOGGED_IN_SALT',\s*'[^']*'[)]; $LOGGEDS " "$WP_CONFIG"
sed -i "s define[(]'NONCE_SALT',\s*'[^']*'[)]; $NONCES " "$WP_CONFIG"
}
while getopts "hf:a:d:" option; do
case $option in
h)
show_help
exit;;
f)
FILE=$OPTARG
if [ ! -f $FILE ]; then
echo "$FILE is not a file."
exit 2
fi
resalt "$FILE"
exit $?;;
a)
TOPDIR=$OPTARG
if [ ! -d $TOPDIR ]; then
echo "$TOPDIR is not a directory."
exit 2
fi
ls $TOPDIR/*/wp-config.php >/dev/null 2>&1
if [[ $? -ne 0 ]]; then
echo "$TOPDIR has no subdirectories containing a wp-config.php."
exit 2
fi
echo "Resalt all in directory: $TOPDIR"
echo
EX=0
for filename in $TOPDIR/*/wp-config.php; do
echo "Resalt $filename"
resalt "$filename"
RET=$?
EX=$(($RET > $EX ? $RET : $EX))
done
exit $EX;;
d)
DIR=$OPTARG
if [ ! -f "$DIR/wp-config.php" ]; then
echo "$DIR/wp-config.php does not exist."
exit 2
fi
echo "Resalt $DIR/wp-config.php"
resalt "$DIR/wp-config.php"
exit $?;;
\?)
echo "Not a valid option."
show_help
exit 1;;
esac
done
show_help
exit 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment