Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get past the login page with Wget?

Tags:

wget

I am trying to use Wget to download a page, but I cannot get past the login screen.

How do I send the username/password using post data on the login page and then download the actual page as an authenticated user?

like image 675
Señor Reginold Francis Avatar asked Aug 24 '09 19:08

Señor Reginold Francis


People also ask

How do I pass credentials with wget?

You can provide authentication credential via --user=USERNAME and --password=PASSWORD ; based on the man wget , the command can be overridden using the --http-user=USERNAME and --http-password=PASSWORD for http connection and the --ftp-use=USERNAME and --ftp-password=PASSWORD for ftp connection.

How do I use cookies in wget?

Mirroring such a site requires Wget to send the same cookies your browser sends when communicating with the site. This is achieved by ' --load-cookies '—simply point Wget to the location of the cookies. txt file, and it will send the same cookies your browser would send in the same situation.

How do I use wget terminal?

Most Linux distributions have Wget installed by default. To check, type wget in your terminal and press ENTER . If it is not installed, it will display: command not found . You can install it by running the following command: sudo apt-get install wget .

Is cURL the same as wget?

Differences Between wget and cURL Wget is a simple transfer utility, while curl offers so much more. Curl provides the libcurl library, which can be expanded into GUI applications. Wget, on the other hand, is a simple command-line utility. Wget supports fewer protocols compared to cURL.


10 Answers

Based on the manual page:

# Log in to the server.  This only needs to be done once.
wget --save-cookies cookies.txt \
     --keep-session-cookies \
     --post-data 'user=foo&password=bar' \
     --delete-after \
     http://server.com/auth.php

# Now grab the page or pages we care about.
wget --load-cookies cookies.txt \
     http://server.com/interesting/article.php

Make sure the --post-data parameter is properly percent-encoded (especially ampersands!) or the request will probably fail. Also make sure that user and password are the correct keys; you can find out the correct keys by sleuthing the HTML of the login page (look into your browser’s “inspect element” feature and find the name attribute on the username and password fields).

like image 94
jarnoan Avatar answered Sep 23 '22 12:09

jarnoan


You can log in via browser and copy the needed headers afterwards:

screenshot

Use "Copy as cURL" in the Network tab of browser developer tools and replace curl's flag -H with wget's --header (and also --data with --post-data if needed).

like image 27
user Avatar answered Sep 23 '22 12:09

user


I directly gave cookies of an existing connection to wget with --no-cookies and the Cookie HTTP request header. In my case it was a Moodle university login where logging in looks more complex (using multiple requests with a login ticket). I added --post-data because it was a POST request.

For example, get all Moodle users list:

wget --no-cookies --header "Cookie: <name>=<value>" --post-data 'tab=search&name=+&personsubmit=Rechercher&keywords=&keywordsoption=allmine' https://moodle.unistra.fr/message/index.php
like image 23
baptx Avatar answered Sep 25 '22 12:09

baptx


I had the same problem. My solution was to do the login via Chrome and save the cookies data to a text file. This is easily done with this Chrome extension: Chrome cookie.txt export extension.

When you get the cookies data, there is also an example on how to use them with wget. A simple copy-paste command line is provided to you.

like image 27
Thor-Erik Rødland Avatar answered Sep 25 '22 12:09

Thor-Erik Rødland


I wanted a one-liner that didn't download any files; here is an example of piping the cookie output into the next request. I only tested the following on Gentoo, but it should work in most *nix environments:

wget -q -O /dev/null --save-cookies /dev/stdout --post-data 'u=user&p=pass' 'http://example.com/login' | wget -q -O - --load-cookies /dev/stdin 'http://example.com/private/page'

(This is one line, though it likely wraps on your browser)

If you want the output saved to a file, change -O - to -O /some/file/name.ext

like image 34
Caleb Gray Avatar answered Sep 24 '22 12:09

Caleb Gray


You don't need cURL to do POSTed form data. --post-data 'key1=value1&key2=value2' works just fine. Note: you can also pass a file name to wget with the POST data in the file.

like image 36
J. Piel Avatar answered Sep 24 '22 12:09

J. Piel


If they're using basic authentication:

wget http://username:[email protected]/page.html

If they're using POSTed form data, you'll need to use something like cURL instead.

like image 35
ceejayoz Avatar answered Sep 24 '22 12:09

ceejayoz


A solution which uses lynx and wget.

Note: Lynx has to have been compiled with the --enable-persistent-cookies flag for this to work

When you want to use wget to download some file from a site which requires login, you just need a cookie file. In order to generate the cookie file, I choose lynx. lynx is a text web browser. First you need a configure file for lynx to save cookie. Create a file lynx.cfg. Write these configuration into the file.

SET_COOKIES:TRUE
ACCEPT_ALL_COOKIES:TRUE
PERSISTENT_COOKIES:TRUE
COOKIE_FILE:cookie.file

Then start lynx with this command:

lynx -cfg=lynx.cfg http://the.site.com/login

After you input the username and password, and select 'preserve me on this pc' or something similar. If login successfully, you will see a beautiful text web page of the site. And you logout. The in the current directory, you will find a cookie file named as cookie.file. This is what we need for wget.

Then wget can download file from the site with this command.

wget --load-cookies ./cookie.file http://the.site.com/download/we-can-make-this-world-better.tar.gz
like image 40
PokerFace Avatar answered Sep 23 '22 12:09

PokerFace


Example to download with wget on server a big file link that can be obtained in your browser.

In example using Google Chrome.

Login where you need, and press download. Go to download and copy your link.

enter image description here

Then open DevTools on a page where you where login, go to Console and get your cookies, by entering document.cookie

enter image description here

Now, go to server and download your file: wget --header "Cookie: <YOUR_COOKIE_OUTPUT_FROM_CONSOLE>" <YOUR_DOWNLOAD_LINK>

enter image description here

like image 6
Alex Ivasyuv Avatar answered Sep 26 '22 12:09

Alex Ivasyuv


I use this chrome extension. It'll give you the wget command for any download link you open.

like image 1
Vahid Avatar answered Sep 27 '22 12:09

Vahid