How to automatically login to a website using a shell script?

I'd like to know if there was a way to login to a website by specifying login credentials in a shell script and automatically login by running the shell script.


You can use lynx to automate the login and it provides much more features. Refer the man page of lynx here

lynx can be easily integrated with a shell script. Using lynx with -auth parameter you can pass a id and password for authentication.

-auth=ID:PASSWD set authorization ID and password for protected documents at startup. Be sure to protect any script files which use this switch.

Hope this helps.


In my case, -auth=ID:PASS didn't work.
So I used this:

lynx -cmd_log=/home/luk/lynxScript.lys -accept_all_cookies www.webside.com/auth

Then logged in, with a password.

As a result (save in the file lynxScript.lys), I got all the commands executed when logging in.

Now I could use this:

lynx -cmd_script=/home/luk/lynxScript.lys -accept_all_cookies www.webside.com/auth
  • -cmd_log - saves your actions.
  • -cmd_script - plays the recorded actions.

So with -cmd_log and -cmd_script, you can do everything you want on the webpage.


It depends on what login mechanism the website uses, because there is no standard.

Most websites today use cookies. So basically you have to automatically fill in the login form, submit it, and store the cookie you get in return. Then you have to use that cookie in subsequent access to the site.

curl is a good tool for this. You have to write a script where you first call

curl -c /tmp/cookiefile -d login_field_name=login -d password_field_name=password https://site.domain/path/to/login

Of course login_field_name, password_field_name and https://site.domain/path/to/login (and maybe other parameters needed by login form) are things you must read from the page source.

After this succeeds, authentication cookie will be stored in /tmp/cookiefile, and you have to use it for subsequent accesses to the site:

curl -b /tmp/cookiefile http://site.domain/path/to/whatever/you/need