Tuesday, September 25, 2012

Use Curl to log on a page

This article is to give an example of using linux curl  command to log on a page.

Assuming we want to login a page like this:

So, the first thing is to look HTML code to determine which kind of input field to submit to the server side. We can just use browser's developer tool to the source code and we found something like the following:
<form action="http://annunziato.org/academia/login/index.php" method="post" id="login">

          <div class="loginform">          

              <input type="text" name="username" id="username" size="15" value="">

              <input type="password" name="password" id="password" size="15" value="">

              <input type="submit" id="loginbtn" value="Login"> 

             <input type="checkbox" name="rememberusername" id="rememberusername" value="1"> 

          </div>

</form>

Under the form tag, We have three input tag has names: username, password and rememberusername. So, we use curl option --data to submit the data.
#! /bin/bash

timemark=`date +%s`;
cookieFileName+="cookie${timemark}"
curl --cookie-jar $cookieFileName \
     --location \
     --output login.html \
     http://www.somewebsite.com/academia/login/index.php 

curl --cookie-jar $cookieFileName \
     --cookie $cookieFileName \
     --data username=myname \
     --data @pwdfile \
     --data rememberusername=1 \
     --location \
     http://www.somewebsite.com/academia/login/index.php \
     --output welcome.html

The line13 is using a password file called "pwdfile" which using a pattern like "key=value".
 The option --data is to pass form data like user name, password, etc. to specified url using Post.

we save this script into curlLogin.sh and change the previlege using chmod 644 curlLogin.sh.
Then, execute it.

We should open the welcome.html to see some successful login information if everything goes well.

No comments:

Post a Comment