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

前端 未结 10 2283
有刺的猬
有刺的猬 2021-02-13 06:27

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?



        
相关标签:
10条回答
  • 2021-02-13 06:55

    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/

    0 讨论(0)
  • 2021-02-13 06:55

    I tried the accepted solution:

    #!/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
    

    However it does not work perfectly as for some reason it induces the SALTS to "move down" 1 line in the wp-config.php file each time it is used... it is not ideal if you are going to change SALTS automatically like every week, months with cron for example...

    A better solution for me was to create a little function that I call in my script. This function creates a file with the SALTS (deletes it at the end), deletes every lines containing one of the SALTS then just inserts the SALTS contained in the file in place of the initial SALTS. This works perfectly.

    fct_update_salts() {
        # Requires website name as target
    
        curl http://api.wordpress.org/secret-key/1.1/salt/ > ~/SALTS.txt
    
        var_initial_path1=`pwd`
        cd ~ #going to home directory
    
        # This scripts eliminates successively all SALT entries, replaces the last one by XXX as a marker, places SALTS.txt, below XXX and deletes XXX
        sudo sed -i "/SECURE_AUTH_KEY/d" $1/wp-config.php
        sudo sed -i "/LOGGED_IN_KEY/d" $1/wp-config.php
        sudo sed -i "/NONCE_KEY/d" $1/wp-config.php
        sudo sed -i "/AUTH_SALT/d" $1/wp-config.php
        sudo sed -i "/SECURE_AUTH_SALT/d" $1/wp-config.php
        sudo sed -i "/LOGGED_IN_SALT/d" $1/wp-config.php
        sudo sed -i "/NONCE_SALT/d" $1/wp-config.php
        sudo sed -i "/AUTH_KEY/cXXX" $1/wp-config.php
        sudo sed -i '/XXX/r SALTS.txt' $1/wp-config.php
        sudo sed -i "/XXX/d" $1/wp-config.php
        echo "SALTS REPLACED BY:"
        echo "====================="
        cat ~/SALTS.txt
        sudo rm -rf ~/SALTS.txt
        cd $var_initial_path1
    }
    

    The function is to be called in the script like this:

    # Reset SALTS
    fct_update_salts $SITE_PATH
    

    Where $SITE_PATH="/var/www/html/YOUR_WEBSITE" or whatever path works for you.

    0 讨论(0)
  • 2021-02-13 06:56

    How about using sed?

    cat wp-config.php | sed 's/old_string/new_string/g' > wp-config.php
    
    0 讨论(0)
  • 2021-02-13 06:56

    This is the bash script that I came up with that works on my Ubuntu server. I modified the examples from above.

    Its a bit of brute force in that it will only replace the 8 keys that currently are required and expects the server to return exactly the same length key every time. The script works well for my use case so I thought I would share it.

    CONFIG_FILE=wp-config.php
    SALT=$(curl -L https://api.wordpress.org/secret-key/1.1/salt/)
    SRC="define('AUTH_KEY'"; DST=$(echo $SALT|cat|grep -o define\(\'AUTH_KEY\'.\\{70\\}); sed -i "/$SRC/c$DST" $CONFIG_FILE
    SRC="define('SECURE_AUTH_KEY'"; DST=$(echo $SALT|cat|grep -o define\(\'SECURE_AUTH_KEY\'.\\{70\\}); sed -i "/$SRC/c$DST" $CONFIG_FILE
    SRC="define('LOGGED_IN_KEY'"; DST=$(echo $SALT|cat|grep -o define\(\'LOGGED_IN_KEY\'.\\{70\\}); sed -i "/$SRC/c$DST" $CONFIG_FILE
    SRC="define('NONCE_KEY'"; DST=$(echo $SALT|cat|grep -o define\(\'NONCE_KEY\'.\\{70\\}); sed -i "/$SRC/c$DST" $CONFIG_FILE
    SRC="define('AUTH_SALT'"; DST=$(echo $SALT|cat|grep -o define\(\'AUTH_SALT\'.\\{70\\}); sed -i "/$SRC/c$DST" $CONFIG_FILE
    SRC="define('SECURE_AUTH_SALT'"; DST=$(echo $SALT|cat|grep -o define\(\'SECURE_AUTH_SALT\'.\\{70\\}); sed -i "/$SRC/c$DST" $CONFIG_FILE
    SRC="define('LOGGED_IN_SALT'"; DST=$(echo $SALT|cat|grep -o define\(\'LOGGED_IN_SALT\'.\\{70\\}); sed -i "/$SRC/c$DST" $CONFIG_FILE
    SRC="define('NONCE_SALT'"; DST=$(echo $SALT|cat|grep -o define\(\'NONCE_SALT\'.\\{70\\}); sed -i "/$SRC/c$DST" $CONFIG_FILE
    
    0 讨论(0)
提交回复
热议问题