04 Feb, 2010

Perl Script to Fetch PNR Status reservation on train from Indian Railways

The code below is a perl script that fetches the PNR status from the indianrail.gov.in website and displays it on the command line.

Warning: No one is authorised to make any type of commercial usage like putting web advertisements or SMS service and Reproducing/Transmitting/Storing in its database, any content of www.indianrail.gov.in website, without prior written permission from Indian Railways. Violators will be severely prosecuted.

Ok, That is the warning you see on Indian Railways site to check the PNR Status. The following code written in perl fetches content from Indian Railways site, however it does not store reproduce or does anything with the data. It justs fetches the PNR Status and puts it out on the command line prompt.

This code is for educational purpose only anyone using the code for any purpose should contact Indian Railways before doing so. I am not responsible for any outcomes by using the code below.

Requires: WWW::Mechanize module and PERL.
Usage:

./pnr.pl <PNR NUMBER>

EDIT: The Railways have changed their interface, so the script also have changed.
New Script

#!/usr/bin/perl

use WWW::Mechanize;

my $mech = WWW::Mechanize->new();


$pnr = $ARGV[0];

$mech->get("http://www.indianrail.gov.in/pnr_Enq.html");

$part1 = substr($pnr,0,3);
$part2 = substr($pnr,3);

print " Fetching for PNR $part1-$part2 \n\n";

$mech->submit_form(
        form_number => 1,
        fields      => {
            lccp_pnrno1    =>  $part1,
            lccp_pnrno2    =>  $part2,
        }
    );

	
$content = $mech->response->content;
$content =~ s|<body>(.*?)</body>|$1|si;
$content =~ s|<(.*?)>||sig;

if($content =~ m|(Passenger [1-6].*?)Chart|sig) {
print $1;
}
Outdated script
#!/usr/bin/perl

use WWW::Mechanize;

my $mech = WWW::Mechanize->new();


$pnr = $ARGV[0];

$mech->get("http://www.indianrail.gov.in");

$part1 = substr($pnr,0,3);
$part2 = substr($pnr,3);

print " Fetching for PNR $part1-$part2 \n\n";

$mech->submit_form(
        form_number => 1,
        fields      => {
            lccp_pnrno1    =>  $part1,
            lccp_pnrno2    =>  $part2,
        }
    );

	
$content = $mech->response->content;
$content =~ s|<body>(.*?)</body>|$1|si;
$content =~ s|<(.*?)>||sig;

if($content =~ m|(Passenger [1-6].*?)Chart|sig) {
print $1;
}

Comments (0) Post a comment
You may also like



Email Newsletter
Email:
Popular Posts
Recent Posts
Tags
Random photo
Barefoot Resort Reception Barefoot Resort Reception in Havelock Island Andaman
On Facebook
Recent Comments


digitalpbk