Authenticate on WordPress with wget

I would like to login via Wget onto a WordPress website.

I have found on StackOverflow something related to basic authentication.

Read More
wget --save-cookies cookies.txt 
--post-data 'user=foo&password=bar' 
http://server.com/auth.php

and tried this with a WordPress site but did not work.

Related posts

Leave a Reply

2 comments

  1. Try this. This worked for me for one of the Drupal projects I worked on sometime ago. Its answered by asciikewl on the Drupal thread https://drupal.org/node/118759


    I’ve managed to get it working this way, using wget’s own cookie handling:
    I’m also referencing the /user page, not a login block (not enabled on my site)

    #!/bin/sh
    site=http://your site url with a slash on the end/
    name=ScriptUser
    pass=somethingsecure
    cookies=/tmp/cron-cookies.txt
    wget -O /dev/null --save-cookies /tmp/ba-cookies.txt --keep-session-cookies --load-cookies $cookies "${site}user"
    wget --keep-session-cookies --save-cookies $cookies --load-cookies $cookies -O /dev/null 
        --post-data="name=$name&pass=$pass&op=Log%20in&form_id=user_login" 
        "${site}user?destination=login_redirect"
    wget --keep-session-cookies --save-cookies $cookies --load-cookies $cookies "${site}login_redirect"
    

    Maybe with the wget cookie handling you can recurse the site.

  2. For WordPress, you’re only missing a tiny bit, which is the testcookie=1. Also you probably need a User-Agent otherwise you may receive a “403 Forbidden” response.

    So all together the script should be:

    #!/usr/bin/env bash
    
    site="http://server.com"
    login_address="$site/wp-login.php"
    log="username"
    pwd="password"
    cookies="/tmp/cookies.txt"
    agent="Mozilla/5.0"
    
    # authenticate and save cookies
    wget 
        --user-agent="$agent" 
        --save-cookies $cookies 
        --keep-session-cookies 
        --delete-after 
        --post-data="log=$log&pwd=$pwd&testcookie=1" 
        "$login_address"
    
    # access home page with authenticated cookies
    wget 
        --user-agent="$agent" 
        --load-cookies $cookies 
        "$site"
    

    Pro tip:
    using Chrome Developers tools, in the “Network” tab, you can “Copy as cURL”, which is not too far from wget format.

    Copy as cURL