Shell Script to login into a website

scripting, shell, unix

Solution

If what you want to do is retrieve content from a website using HTTP Basic authentication then you can achieve this from the command line using wget or curl:

wget --http-user=user --http-password=password 

curl http://username:password@website.com/url

If cookie-based authentication is used you may be able to use wget for this too, depending on how it is set up. If for example you can HTTP POST to the login page

wget --save-cookies cookies.txt --post-data \
'user=myusername&password=mypassword' http://url/loginpage

wget --load-cookies cookies.txt -p http://url/thecontentIwant

The --save-cookies and --load-cookies allow for cookie-based authentication tokens to persist between requests.

If the form of authentication being used is more complex then the answer is going to be quite different - so it would help to state which website you have in mind. Furthermore, many sites have APIs that you should use instead...

In general it would help if you could clarify the question a little -- I find this bit 'at the same time I want to automatically take the user id and the password also' a bit confusing. If you want to do an interactive session in bash (i.e. getting the user to input username and password into the bash script) then you will find

read -e -p 

very useful (eg. read -e -p "My prompt: " varname; echo $varname).

Problem

I want to login into a website using a shell script and at the same time I want to automatically take the user id and the password also. Please suggest how is it done using shell script. Thanks !!!

Original source