Thursday, May 10, 2007

ANSI Escape Sequences

Intro
ANSI Escape sequences are used to perform special operations on the terminal, such as changing the output color, making it bold, printing at a specified coordinate etc.

The sequences

Wherever you see '#', that should be replaced by the appropriate number.

ESC code sequence Function
------------------- ---------------------------
Cursor Controls:
ESC[#;#H or ESC[#;#f Moves cusor to line #, column #
ESC[#A Moves cursor up # lines
ESC[#B Moves cursor down # lines
ESC[#C Moves cursor forward # spaces
ESC[#D Moves cursor back # spaces
ESC[#;#R Reports current cursor line & column
ESC[s Saves cursor position for recall later
ESC[u Return to saved cursor position

Erase Functions:
ESC[2J Clear screen and home cursor
ESC[K Clear to end of line

Set Graphics Rendition:
ESC[#;#;....;#m Set display attributes where # is
00 for normal display (or just 0)
01 for bold on (or just 1)
02 faint (or just 2)
03 standout (or just 3)
04 underline (or just 4)
05 blink on (or just 5)
07 reverse video on (or just 7)
08 nondisplayed (invisible) (or just 8)
22 normal
23 no-standout
24 no-underline
25 no-blink
27 no-reverse
30 black foreground
31 red foreground
32 green foreground
33 yellow foreground
34 blue foreground
35 magenta foreground
36 cyan foreground
37 white foreground
39 default foreground
40 black background
41 red background
42 green background
43 yellow background
44 blue background
45 magenta background
46 cyan background
47 white background
49 default background

ESC[=#;7h or Put screen in indicated mode where # is
ESC[=h or 0 for 40 x 25 black & white
ESC[=0h or 1 for 40 x 25 color
ESC[?7h 2 for 80 x 25 b&w
3 for 80 x 25 color
4 for 320 x 200 color graphics
5 for 320 x 200 b & w graphics
6 for 640 x 200 b & w graphics
7 to wrap at end of line

ESC[=#;7l or ESC[=l or Resets mode # set with above command
ESC[=0l or ESC[?7l

Keyboard Reassignments:

ESC[#;#;...p Keyboard reassignment. The first ASCII
or ESC["string"p code defines which code is to be
or ESC[#;"string";#; changed. The remaining codes define
#;"string";#p what it is to be changed to.

E.g. Reassign the Q and q keys to the A and a keys (and vice versa).

ESC [65;81p A becomes Q
ESC [97;113p a becomes q
ESC [81;65p Q becomes A
ESC [113;97p q becomes a

E.g. Reassign the F10 key to a DIR command.

ESC [0;68;"dir";13p The 0;68 is the extended ASCII code
for the F10 key and 13 is the ASCII
code for a carriage return.

Other function key codes F1=59,F2=60,F3=61,F4=62,F5=63
F6=64,F7=65,F8=66,F9=67,F10=68



Source : The Internet

ESC
The Escape sequence on Linux is \e. Replace the ESC by \e on linux to make it work.
For example,
\e[31;1m
Prints this in RED BOLD.

Have more info? Please do comment...

Perl Script to Login to Orkut

Finally
Succeeded in making the Perl Script that logs you into Orkut! Signing into Orkut is much more than a simple POST, Google is always clever so that they make things complex for automating the login process. Anyway here is the complex script, ..

Algorithm


  1. Goto www.orkut.com

  2. Check whether the title is Home.

  3. If not find the source of login page IFRAME

  4. Fill the username and password and submit the FORM.

  5. Find the follow link from the next page.

  6. Goto the followlink and GET the redirect page URL

  7. Goto the redirect page

  8. Goto Orkut.com/home.aspx

  9. Your done.



Note:
Tested on Linux (FC6, Perl 5.8.8)
The following script is for linux, Will run on Windows too but the ANSI coloring may not work...
Windows Program is given below the program for Linux (without ANSI coloring).
For Linux (with ANSI coloring)

use WWW::Mechanize;
use HTTP::Cookies;
use HTTP::Request::Common;

$cj=HTTP::Cookies->new(file => "cookie.jar",autosave=>1,ignore_discard=>1);
$mech = WWW::Mechanize->new(cookie_jar => $cj);

@prc = ("\e[0m\n","[\e[1;31mFAILED\e[0m]","[\e[2;32m OK \e[0m]\n");

print "\n\e[31;1mG\e[0mmail iD : ";
$email = ;
print "\e[31;1mP\e[0massword : \e[97m";
$pass = ;
print $prc[0];

chomp $email;chomp $pass;

RELOGIN:
for($i=3; $i>=0;$i--)
{
printf("\e[2m%-60s","GET /Home.aspx");
$mech->get("http://www.orkut.com/Home.aspx");
last if($mech->success());

printf("%10s",$prc[1]);
print "\e[2;37m Retry (",$i,")".$prc[0] if($i);
}

return if($i<=0);
print $prc[2];


$cnt = $mech->response->as_string;
$cnt =~ s/\n|\s+/ /g;

if($mech->title =~ m/orkut.*home/i)
{
print "\n\e[34mAlready Logged In",$prc[0];

$cnt =~ m/<b>(.*)\@gmail.com<\/b>/;

print "\nLogout $1?[n] : ";
$com=;chomp $com;
if($com eq "y")
{
logout;
goto RELOGIN;
}
else
{
return;
}
}

$cnt = $mech->response->as_string;
$cnt =~ s/\n|\s+/ /g;

printf("\e[2m%-60.59s","Parsing for Login Page ");
#print $cnts;
if($cnt !~ m/id='liframe'.*?src='(.*)'/)
{
printf("%10s",$prc[1]);
print "\e[2;37m Login page URL Not Found!\nTry Again OR Update the script! ".$prc[0];

return;
}

print $prc[2];
$url = $1;
$j=3;
REDO:
for($i=3;$i>=0;$i--)
{
printf("\e[2m%-60.59s","GET $url");
$mech->get($url);
last if($mech->success());

printf("%10s",$prc[1]);
print "\e[2;37m Retry (",$i,")".$prc[0] if($i);
}

return if($i<=0);

print $prc[2];

$cnt = $mech->response->as_string;
$cnt =~ s/\n|\s+/ /g;

$mech->form_number(1);
$mech->field("Email",$email."\@gmail.com");
$mech->field("Passwd",$pass);


printf("\e[2m%-60.59s","Logging In ... ");

$mech->click("null");

$j--;
if($j && !$mech->success())
{
printf("%10s\n",$prc[1]);
goto REDO;
}
return unless($j);

$cnt = $mech->response->as_string;
$cnt =~ s/\n|\s+/ /g;

if(!$mech->find_link(text => "click here to continue"))
{
printf("%10s",$prc[1]);
print "\n\e[31;1mWrong Usename or Password!",$prc[0];
return;
}


print $prc[2];
for($i=3;$i>=0;$i--)
{
printf("\e[2m%-60.59s","Continuing ...");
$mech->follow_link(text => "click here to continue");
last if($mech->success());

printf("%10s",$prc[1]);
print "\e[2;37m Retry (",$i,")".$prc[0] if($i);
}

return if($i<=0);

print $prc[2];

printf("\e[2m%-60s","Parsing REDIRECT URL");
$cnt = $mech->response->as_string;
$cnt =~ s/\n|\s+/ /g;

if($cnt !~ m/location.replace\("(.*)"\)/)
{
printf("%10s",$prc[1]);
print "\n\e[31;1mRedirect script missing!",$prc[0];
return;
}

$url = $1;
$url =~ s/\\u003d/=/g;

print $prc[2];
for($i=3;$i>=0;$i--)
{
printf("\e[2m%-60.59s","GET $url");
$mech->get($url);
last if($mech->success());

printf("%10s",$prc[1]);
print "\e[2;37m Retry (",$i,")".$prc[0] if($i);
}
return if($i<=0);

print $prc[2];
print "\e[34mLogged In!\e[0m";
exit 0;


sub logout
{

printf("\e[0;2m%-60s","Parsing Logout");
if(!$mech->find_link(text => "Logout"))
{
printf("%10s",$prc[1]);
print "\n\e[31;1mNot Logged In?",$prc[0];
return;
}


print $prc[2];
for($i=3;$i>=0;$i--)
{
printf("\e[2m%-60.59s","Logging Out ...");
$mech->follow_link(text => "Logout");
last if($mech->success());

printf("%10s",$prc[1]);
print "\e[2;37m Retry (",$i,")".$prc[0] if($i);
}
return if($i<=0);

print $prc[2];
}


For windows:

use WWW::Mechanize;
use HTTP::Cookies;
use HTTP::Request::Common;

$cj=HTTP::Cookies->new(file => "cookie.jar",autosave=>1,ignore_discard=>1);
$mech = WWW::Mechanize->new(cookie_jar => $cj);

@prc = ("\n","[FAILED]","[ OK ]\n");

print "\nEmail iD : ";
$email = ;
print "Password : \e[97m";
$pass = ;
print $prc[0];

chomp $email;chomp $pass;

RELOGIN:
for($i=3; $i>=0;$i--)
{
printf("%-60s","GET /Home.aspx");
$mech->get("http://www.orkut.com/Home.aspx");
last if($mech->success());

printf("%10s",$prc[1]);
print " Retry (",$i,")".$prc[0] if($i);
}

return if($i<=0);
print $prc[2];


$cnt = $mech->response->as_string;
$cnt =~ s/\n|\s+/ /g;

if($mech->title =~ m/orkut.*home/i)
{
print "\nAlready Logged In",$prc[0];

$cnt =~ m/<b>(.*)\@gmail.com<\/b>/;

print "\nLogout $1?[n] : ";
$com=;chomp $com;
if($com eq "y")
{
logout;
goto RELOGIN;
}
else
{
return;
}
}

$cnt = $mech->response->as_string;
$cnt =~ s/\n|\s+/ /g;

printf("%-60.59s","Parsing for Login Page ");
#print $cnts;
if($cnt !~ m/id='liframe'.*?src='(.*)'/)
{
printf("%10s",$prc[1]);
print "Login page URL Not Found!\nTry Again OR Update the script! ".$prc[0];

return;
}

print $prc[2];
$url = $1;
$j=3;
REDO:
for($i=3;$i>=0;$i--)
{
printf("%-60.59s","GET $url");
$mech->get($url);
last if($mech->success());

printf("%10s",$prc[1]);
print " Retry (",$i,")".$prc[0] if($i);
}

return if($i<=0);

print $prc[2];

$cnt = $mech->response->as_string;
$cnt =~ s/\n|\s+/ /g;

$mech->form_number(1);
$mech->field("Email",$email."\@gmail.com");
$mech->field("Passwd",$pass);


printf("%-60.59s","Logging In ... ");

$mech->click("null");

$j--;
if($j && !$mech->success())
{
printf("%10s\n",$prc[1]);
goto REDO;
}
return unless($j);

$cnt = $mech->response->as_string;
$cnt =~ s/\n|\s+/ /g;

if(!$mech->find_link(text => "click here to continue"))
{
printf("%10s",$prc[1]);
print "\nWrong Usename or Password!",$prc[0];
return;
}


print $prc[2];
for($i=3;$i>=0;$i--)
{
printf("%-60.59s","Continuing ...");
$mech->follow_link(text => "click here to continue");
last if($mech->success());

printf("%10s",$prc[1]);
print "Retry (",$i,")".$prc[0] if($i);
}

return if($i<=0);

print $prc[2];

printf("%-60s","Parsing REDIRECT URL");
$cnt = $mech->response->as_string;
$cnt =~ s/\n|\s+/ /g;

if($cnt !~ m/location.replace\("(.*)"\)/)
{
printf("%10s",$prc[1]);
print "\nRedirect script missing!",$prc[0];
return;
}

$url = $1;
$url =~ s/\\u003d/=/g;

print $prc[2];
for($i=3;$i>=0;$i--)
{
printf("%-60.59s","GET $url");
$mech->get($url);
last if($mech->success());

printf("%10s",$prc[1]);
print " Retry (",$i,")".$prc[0] if($i);
}
return if($i<=0);

print $prc[2];
print "Logged In!";
exit 0;


sub logout
{

printf("%-60s","Parsing Logout");
if(!$mech->find_link(text => "Logout"))
{
printf("%10s",$prc[1]);
print "\nNot Logged In?",$prc[0];
return;
}


print $prc[2];
for($i=3;$i>=0;$i--)
{
printf("%-60.59s","Logging Out ...");
$mech->follow_link(text => "Logout");
last if($mech->success());

printf("%10s",$prc[1]);
print "\e[2;37m Retry (",$i,")".$prc[0] if($i);
}
return if($i<=0);

print $prc[2];
}




Libs needed
LWP;
WWW::Mechanize;

Description
Script that logs you into orkut, It has its own Fail-retry methods and all the action is presented neatly in the terminal, using color codes (ANSI Escape sequences for LINUX)
There is more of this script currently in the beta version, for mass scrapping etc. For more information about the beta codes, leave your email as a comment.

Bugs ?
Please do let me know the bugs , so that i can update the script here...
It works for me... Did it work for you? Let me know... (else I may think this is all crap!)

Happy Automating,.. :)

Friday, May 4, 2007

Windows Folder Extensions

Intro

Well you all know about file extensions and what they mean etc... But have you heard about a Folder Extension ? I suppose not. Windows has some set of pretty extraordinary extensions for folders, to mark them as something different. Read more to get what I mean...


List of extensions


  • .{21EC2020-3AEA-1069-A2DD-08002B30309D} Control Panel

  • .{9E56BE61-C50F-11CF-9A2C-00A0C90A90CE} Desktop

  • .{645FF040-5081-101B-9F08-00AA002F954E} Recycle Bin

  • .{20D04FE0-3AEA-1069-A2D8-08002B30309D} My Computer

  • .{208D2C60-3AEA-1069-A2D7-08002B30309D} Network

  • .{2227A280-3AEA-1069-A2DE-08002B30309D} Printer

  • .{25336920-03F9-11CF-8FD0-00AA00686F13} HTML Document

  • .{0003000D-0000-0000-C000-000000000046} Wave File

  • .{00022602-0000-0000-C000-000000000046} Video Clip

  • .{00021401-0000-0000-C000-000000000046} Makes it a file



What to do?

Make a New folder and name it something. Now when you rename add one of the following extension to it. (ie Append the .{XXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXX} to the end of the folder name just as adding an extension to a file) and see the changes.

** Try these on a blank folder or a folder that contains nothing important!

Tested on Windows XP, you are welcome to comment about success or failure on other Windows OS's .... This method can be used to hide/protect important folders.. ;)

Wednesday, May 2, 2007

Sitemeter selling your stats to Ad Company

Sitemeter
is or rather Was a popular site tracking service that reports about traffic stats on sites. Recently it was found by many that certain advertising company specificclick.net cookies were found in the browsers. Following it up I investigated the sitemeter code provided and here is the solid proof that sitemeter sells your stats to the company :

The source code that is being generated by the sitemeter script that contains the ad code:



I have removed all sitemeter codes from my sites and I recommend you too to do the same...

Tuesday, May 1, 2007

My First useful PERL program

#!/usr/bin/perl -w

$username = $ARGV[0];
$username = "s28arunpbk" if(!$username);
#
# Get the username from the first command line argument
# If null assign my user id.
#

use LWP;
use LWP::UserAgent;
#
# Initialize objects
#


$ua = LWP::UserAgent->new;
$ua->agent("Checkmap/1.0");
my $req=HTTP::Request->new(GET => "http://sitemeter.com/?a=stats&s=$username&r=0");

my $res = $ua->request($req);
#
# Send a request and grab the response...
#

if($res->is_success)
{
$contents = $res->content;
$contents =~ m/<title>(.*)<\/title>/i; #regexp to find the title
if($1 =~ m/Site Meter/)
{
print "Wrong User name!\nAborting\n";
exit 0;
#
# If the web page title contains the words "Site Meter",
# it means the username was invalid and got redirected to
# index.
#

}

print "$1\n","="x length $1,"\n";

#
# Print the title and draw an underline...
#


$contents =~ s/ //g; #Strip `&nbsp;`
$contents =~ s/\<.*?\>//g; #Strip all HTML tags
$contents =~ s/\s+//g; #Strip all spaces
$contents =~ m/Total(([0-9]|,)+)Average/g; #The data we need.

print "Visits : ",$1;

$contents =~ m/Total(([0-9]|,)+)Average/g;
printf "\nPageviews : $1\n";
}
else
{
print "Error : $! ",$res->as_string;
exit 1;
#Messed up somewhere.
}
exit 0;
#thats all


What does this do ?

The script gives me the total number of visitors and pageviews on my site via sitemeter.com. By passing an optional username as a command line argument you can get the same information of that account.

How to use ?

Check whether you have Perl installed on your computer.
Start a command prompt and type perl -v, if the command is not found download and install Perl.

Copy the source code given above and paste it in a new file say dstat.pl
Run the code by perl dstat.pl.