Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Download and insert salt string inside wordpress wp-config.php with Bash

How can I insert the content of the variable $SALT in a specific point (line or string) of a file like wp-contet.php from wordpress using Bash script?

SALT=$(curl -L https://api.wordpress.org/secret-key/1.1/salt/)
like image 965
Roger Avatar asked Jun 03 '11 22:06

Roger


3 Answers

I'm not an expert at parsing text files in bash but you should delete the lines that define the things you're downloading from the wordpress salt and then insert the variable at the end... something like:

#!/bin/sh

SALT=$(curl -L https://api.wordpress.org/secret-key/1.1/salt/)
STRING='put your unique phrase here'
printf '%s\n' "g/$STRING/d" a "$SALT" . w | ed -s wp-config.php

OK, now it's fixed... it should look for where the salt is supposed to go and it will replace it with the info retrieved from https://api.wordpress.org/secret-key/1.1/salt/

like image 98
la_f0ka Avatar answered Nov 13 '22 20:11

la_f0ka


This version defines new keys if none exist, and also replaces existing keys:

#!/bin/bash
find . -name wp-config.php -print | while read line
do 
    curl http://api.wordpress.org/secret-key/1.1/salt/ > wp_keys.txt
    sed -i.bak -e '/put your unique phrase here/d' -e \
    '/AUTH_KEY/d' -e '/SECURE_AUTH_KEY/d' -e '/LOGGED_IN_KEY/d' -e '/NONCE_KEY/d' -e \
    '/AUTH_SALT/d' -e '/SECURE_AUTH_SALT/d' -e '/LOGGED_IN_SALT/d' -e '/NONCE_SALT/d' $line
    cat wp_keys.txt >> $line
    rm wp_keys.txt
done
like image 35
lucrussell Avatar answered Nov 13 '22 20:11

lucrussell


If you have csplit available, you can split the original wp-config.php file either side of the salt definitions, download new salts, then cat back together. This keeps the PHP define() statements at the same location in wp-config.php instead of than moving them to a different location within the file:

# Download new salts
curl "https://api.wordpress.org/secret-key/1.1/salt/" -o salts

# Split wp-config.php into 3 on the first and last definition statements
csplit wp-config.php '/AUTH_KEY/' '/NONCE_SALT/+1'

# Recombine the first part, the new salts and the last part
cat xx00 salts xx02 > wp-config.php

# Tidy up
rm salts xx00 xx01 xx02
like image 3
user3417917 Avatar answered Nov 13 '22 19:11

user3417917