Wednesday, December 21, 2011

Perl scripts to notify for your train reservation status


Two perl scripts to notify you when your train reservation status changes

To try out these scripts yourself, you will have to install the perl modules XML::XPathEngine,HTML::TreeBuilder and HTML::TreeBuilder::XPath.

pnrenq.pl

#!/usr/bin/perl

use strict;
use warnings 'all';

use HTTP::Request::Common;
use HTML::TreeBuilder::XPath;
use LWP::UserAgent;
$\ = "\n";
foreach my $pnr (@ARGV) {
    my $length = length ($pnr);
    my $lccp_pnrno1= substr ($pnr, 0, 3) ;    my $lccp_pnrno2= substr ($pnr, 3, 7) ;    if ( $length eq 10 ){
    
    my $ua = LWP::UserAgent->new;
    
    $ua->default_header('Accept'=>'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8');
    
    $ua->default_header('Accept-Charset'=>'ISO-8859-1,utf-8;q=0.7,*;q=0.3');
    
    $ua->default_header('Accept-Language'=>'en-GB,en-US;q=0.8,en;q=0.6');
    
    $ua->default_header('Cache-Control'=>'max-age=0');        $ua->default_header('Content-Type'=>'application/x-www-form-urlencoded');
    
    $ua->default_header('Host'=>'www.indianrail.gov.in');        
$ua->default_header('Origin'=>'http://www.indianrail.gov.in');
        $ua->default_header('Referer'=>'http://www.indianrail.gov.in/pnr_Enq.html');
        $ua->default_header('User-Agent'=>'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/535.7 (KHTML, like Gecko) Chrome/16.0.912.63
Safari/535.7');
    
    my $response = $ua->request(POST 'http://www.indianrail.gov.in/cgi_bin/inet_pnrstat_cgi.cgi',
    
    [lccp_pnrno1 => $lccp_pnrno1,         lccp_pnrno2 => $lccp_pnrno2,         submitpnr => "Get+status"
   
     ]);
    
    if ($response->is_success) {
        
    my $tree= HTML::TreeBuilder::XPath->new;            
            $tree->parse_content($response->content);
            print $tree->findvalue('/html/body//table[@id="center_table"]//tr[count(td)=3]/td[3]');
        
    $tree->delete;
    
    }        else {
    
        die $response->status_line;
    
    }    }    else {
    
    print "\n InValid PNR\n\n";    }
}


op_change_mailer.pl

#!/usr/bin/perl

$cmd = $ARGV[0];
$op_file = $ARGV[1];
$prev_op = `cat $op_file`;
`$cmd > $op_file`;
$current_op = `cat $op_file`;

if ($prev_op ne $current_op) {
    system("cat $op_file | mail -s 'Status changed' [your mail id]");
    print "Sent mail\n";
}



Set up a cron with:
00 * * * 1-5 op_change_mailer.pl "pnrenq.pl [pnr numbers separated by spaces]" /var/tmp/pnr

Friday, March 18, 2011

How to make MySQL and PHP to Communicate in Mac?

I already explained How to make PHP and Apache to Communicate  in Mac.
Now, we will do the same with MySQL and PHP.
The default location of mysql.sock is in the /tmp/mysql.sock directory. We’ll need to change this to /var/mysql/mysql.sock as this is where PHP will look for it.
So first off, create a my.conf (or in this case my.cnf) file in your text editor and save it as my.cnf in the /etc folder with the following code:

[client]
socket = /var/mysql/mysql.sock
 
[mysqld]
socket = /var/mysql/mysql.sock

Next move mysql.sock to it’s new directory by typing the following code in a Terminal window:

$ sudo mkdir /var/mysql
$ sudo ln -s /tmp/mysql.sock /var/mysql/mysql.sock

That is pretty much.

Thursday, March 17, 2011

How to enable PHP with Apache in Mac?

1. First you have to find the location of the apache configuration file httpd.conf. It is normally found at /private/etc/apache2/httpd.conf. Find the following or similar line:

#LoadModule php5_module        libexec/apache2/libphp5.so

Uncomment the above line by removing "#", save and quit the file.

2. Restart the apache using your root access or proper privileges
$ apachectl restart

3. Create a test php file called x.php in the public folder of the server, which can be accesses through URL in the browser

$ vi /Library/WebServer/Documents/x.php
and write

4. access the file using your favorite browser.

http://localhost/x.php

It show all the configuration. Congratulations!!!

Tip: You can create soft link directly from your home folder to your web directory like:

$ ln -s /Library/WebServer/Documents htdocs

Tuesday, July 20, 2010

Setup yum repository for update/install from ISO image

After the RHEL5 installation without the registration key, it is not possible to get repository updates/setup as system is not registered with RHN (Red Hat Network). However, yum repository can be setup using the local RHEL ISO image in following steps:


1. Create directory for mounting
  $ mkdir -p /cdrom/iso

2. Mount the iso at the loopback device
  $ mount -o loop </path/to/iso-file> /cdrom/iso

3. Create a repository using the the mounted directory
  $ cd /cdrom
  $ createrepo .
  $ yum clean all

4. Create a yum repo file
  $ cat /etc/yum.repos.d/file.repo
     [RHEL]
     baseurl=file:///cdrom/
     enabled=1
     gpgcheck=0

Troubleshoot:
  * If createrepo package is not installed, find it into the mounted directory & and install first
   $ find /cdrom/iso/ -name "*createrepo*"
   $ rpm -ivh /cdrom/iso/Server/createrepo*.rpm 


Mounting permanently:
To mount /cdrom/iso permanently, a entry need to made in /etc/fstab
/iso-name /cdrom/iso iso9660 ro,loop 0 0

Thursday, July 1, 2010

PHP Class for Array to XML Conversion


/**
* Array To XML conversion in PHP
*/
class Array2XML extends DomDocument{
    public $nodeName;
    private $xpath;
    private $root;
    private $node_name;
  
    /**
    * Constructor
    *
    * Set up the DOM environment
    *
    * @param    string    $root        The name of the root node
    * @param    string    $nod_name    The name numeric keys are called
    *
    */
    public function __construct($root = 'tests', $node_name = 'test') {
        parent::__construct();
      
        $this->encoding = "ISO-8859-1"; // set the encoding
        $this->formatOutput = true;     // format the output
        $this->node_name = $node_name;  // set the node names
        $this->root = $this->appendChild($this->createElement( $root )); // create the root element
        $this->xpath = new DomXPath($this);
        //$this->strictErrorChecking = false;
    }
  
    /*
    * creates the XML representation of the array
    *
    * @param    array    $arr    The array to convert
    * @aparam    string    $node    The name given to child nodes when recursing
    *
    */
    public function createNode($arr, $node = null) {
        if (is_null($node)) {
            $node = $this->root;
        }

        foreach($arr as $element => $value)  {
            $element = is_numeric($element) ? $this->node_name : $element;
          
            //$value = preg_replace('/[^a-z]/i', '', $value); // $value = htmlentities($value);
            $child = $this->createElement($element, (is_array($value) ? null : $value));
            $node->appendChild($child);

            if (is_array($value)) {
                self::createNode($value, $child);
            }
        }
    }
  
    /*
    * Return the generated XML as a string
    *
    * @return    string
    */
    public function __toString() {
        return $this->saveXML();
    }

    /*
    * Array2XML::query() - perform an XPath query on the XML representation of the array
    * @param str $query - query to perform
    * @return mixed
    */
    public function query($query) {
        return $this->xpath->evaluate($query);
    }
} // Class ends

// Example Use

try  {
      $xml = new Array2XML('list');
      $xml->createNode( $dataArr );
      $dataStr = $xml;
}
catch (Exception $e) {
      echo $e->getMessage();
}


Monday, June 16, 2008

Linux: Some configuration

* Name your workstation : For setting the name virus, edit /etc/hostname and put virus only in a line.

* Mount NTFS in Ubantu :
mounting ntfs in ubantu :
sudo su-
cd /media/;
mkdir win;
vi /etc/fstab;
type (assuming sda1 is win partition) : /dev/sda1 /media/win ntfs ro,dmask=0222,fmask=0333 0 0
mount /dev/sda1;
ls win;

Done !!!

* Error (Ubantu): Checking for C compiler default output file name... configure: error: C compiler cannot create executables
sudo apt-get install build-essential

* Setting host time in the shell prompt (virus 15:40:27 /etc $):
if [ "$PS1" != "" ]
then
PS1="\h \t \w \$ "
setenv () { export $1="$2"; }
unsetenv () { unset $*; }
fi