<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Technical Diary &#187; perl</title>
	<atom:link href="http://andriigrytsenko.net/tag/perl/feed/" rel="self" type="application/rss+xml" />
	<link>http://andriigrytsenko.net</link>
	<description>With Andrii Grytsenko</description>
	<lastBuildDate>Wed, 28 Jul 2010 09:02:46 +0000</lastBuildDate>
	
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Perl and cookies auth</title>
		<link>http://andriigrytsenko.net/2010/06/perl-and-cookies-auth/</link>
		<comments>http://andriigrytsenko.net/2010/06/perl-and-cookies-auth/#comments</comments>
		<pubDate>Wed, 09 Jun 2010 12:05:25 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Scripting]]></category>
		<category><![CDATA[cookies]]></category>
		<category><![CDATA[perl]]></category>

		<guid isPermaLink="false">http://andriigrytsenko.net/?p=794</guid>
		<description><![CDATA[I made little overview how to make http authorization through the PERL.

I&#8217;m not gonna describe how to make HTML form and  how to parse input data from html. I leave these as you your homework ;). As storage for HTTP sessions I chose database(specifically MYSQL).
Here is my table with sessions :
desc mc_sessions;
+-----------------+--------------+------+-----+---------+-------+
&#124; Field  [...]]]></description>
			<content:encoded><![CDATA[<p>I made little overview how to make http authorization through the PERL.</p>
<p><span id="more-794"></span></p>
<p>I&#8217;m not gonna describe <a href="http://www.google.com.ua/search?hl=en&amp;source=hp&amp;q=html+form&amp;aq=f&amp;aqi=g10&amp;aql=&amp;oq=&amp;gs_rfai=">how to make HTML form</a> and <a href="http://www.google.com.ua/search?hl=en&amp;q=how+to+parse+input+data+from+html+in+perl&amp;aq=f&amp;aqi=&amp;aql=&amp;oq=&amp;gs_rfai="> how to parse input data from html</a>. I leave these as you your homework ;). As storage for HTTP sessions I chose database(specifically MYSQL).</p>
<p>Here is my table with sessions :</p>
<pre>desc mc_sessions;
+-----------------+--------------+------+-----+---------+-------+
| Field           | Type         | Null | Key | Default | Extra |
+-----------------+--------------+------+-----+---------+-------+
| user_name       | varchar(50)  | YES  |     | NULL    |       |
| session_id      | varchar(255) | NO   | PRI | NULL    |       |
| expiration_time | int(11)      | YES  |     | NULL    |       |
+-----------------+--------------+------+-----+---------+-------+</pre>
<pre>
use CGI qw/:standard/;
use CGI::Cookie;

sub set_cookies {.
    my ($db,$user_name,$user_passwd) = @_;

    my $exp_time = time() + 259200; #(3 days)
    my $session_id = md5_hex($user_id.''.$user_name.''.$user_passwd.''.$exp_time);

    my $sth = $dbh-&gt;prepare("INSERT INTO $table (user_name,session_id,expiration_time) VALUES (?,?,?)");
    $sth-&gt;execute($user_name,$session_id,$exp_time);

    my $cookie1 = new CGI::Cookie(-name=&gt;'SessionID',-value=&gt;"$session_id");
    my $cookie2 = new CGI::Cookie(-name=&gt;'ExpirationTime',-value=&gt;"$exp_time");

    print header(-cookie=&gt;[$cookie1,$cookie2]);

    return 1;
}

sub check_auth {
    my ($dbh) = @_;

    my %cookies = fetch CGI::Cookie;
    my %cook_hash;

    foreach my $key (sort keys %cookies) {
        $cookies{$key} =~ /=(.*);/;
        $cook_hash{$key} = $1;
    }

    return -1 if (!defined($cook_hash{SessionID}));

    my $user_name = is_session_valid($dbh,$cook_hash{SessionID});

    return -1 if ($user_name == -1);

    return $user_name;
}

sub is_session_valid {
    my ($dbh,$session_id) = @_;

    my ($exp_time);

    my $sth = $dbh-&gt;prepare("SELECT * FROM sessions WHERE session_id=? and expiration_time &gt; ?");
    $sth-&gt;execute($session_id,time());

    while ( my @array = $sth-&gt;fetchrow_array ) {
            ($user_name,$session_id,$exp_time) = @array;
    }

    return $user_name if (defined($user_name) and $exp_time &gt; time());

    return -1;
}
</pre>
<p>As result we have 3 functions.</p>
<ol>
<li><span style="color: #000000;"><strong>set_cookies</strong></span><span style="color: #000000;"> &#8211; this function generate, put in database and send cookies to user&#8217;s browser.</span></li>
<li><strong>check_auth</strong> &#8211; check if user has already have some cookies from server if yes call <em>is_session_valid</em> for future checking.</li>
<li><strong>is_session_valid</strong> &#8211; look about user&#8217;s  session id  and check whether this session was expired or not.</li>
</ol>
<p><span style="color: #ff0000;"><strong><span style="text-decoration: underline;">Pay attention:</span></strong></span></p>
<p><span style="color: #ff0000;"><strong></strong></span><span style="color: #ff6600;"><span style="color: #ff0000;">Each function receives variable </span><strong><span style="color: #ff0000;">$dbh</span></strong><span style="color: #ff0000;"> it&#8217;s </span><a href="http://search.cpan.org/~timb/DBI-1.611/DBI.pm#Outline_Usage"><span style="color: #ff0000;">database connection identificator</span></a><span style="color: #ff0000;"> and should be created before function run.</span></span></p>
<h2 style="text-align: center;">How does it use?</h2>
<p>Edit you CGI code. And take care that all pages checks sessions id.<br />
Put this function call after authorization by password to send cookies to user:</p>
<pre>
set_cookies($dbh,$username,$password);
</pre>
<p>And these in all remains site&#8217;s pages to prevent site from unauthorized actions:</p>
<pre>
my $user_name = check_auth($dbh);

if ( $user_id == -1 ) {
        some action[s]
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://andriigrytsenko.net/2010/06/perl-and-cookies-auth/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>IPCalc on Perl</title>
		<link>http://andriigrytsenko.net/2010/03/ipcalc-on-perl/</link>
		<comments>http://andriigrytsenko.net/2010/03/ipcalc-on-perl/#comments</comments>
		<pubDate>Tue, 30 Mar 2010 18:52:41 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Scripting]]></category>
		<category><![CDATA[network]]></category>
		<category><![CDATA[perl]]></category>

		<guid isPermaLink="false">http://andriigrytsenko.net/?p=720</guid>
		<description><![CDATA[Today I decided to make my own IPCalc program. I had two reason for that. First to learn perl&#8217;s binary operations and second one to make some IP calculation points more clear for me.

There is some listing of my script I hope this example help you to understand some fundamental points in IP math.

#!/usr/local/bin/perl
# Written [...]]]></description>
			<content:encoded><![CDATA[<p>Today I decided to make my own IPCalc program. I had two reason for that. First to learn perl&#8217;s binary operations and second one to make some IP calculation points more clear for me.<br />
<span id="more-720"></span></p>
<p>There is some listing of my script I hope this example help you to understand some fundamental points in IP math.</p>
<pre>
#!/usr/local/bin/perl
# Written by Andrii Grytsenko 2010
# PP: http://andriigrytsenko.net    

use POSIX;

sub hex2bin {
   my $hex = shift;
   my $pack = unpack("B32",pack("N",$hex));
   my $pack = substr($pack,-8);
   return $pack;
}                                          

sub bin2hex {
   my $bin = shift;
   return unpack("N", pack("B32", substr("0" x 32 . $bin, -32)))
}                                                               

sub bin2ip {
    my $bin = shift;
    my $type = shift;
    my $ip;
    $ip .= bin2hex(substr($bin,0,8)) ."\.";
    $ip .= bin2hex(substr($bin,8,8)) ."\.";
    $ip .= bin2hex(substr($bin,16,8)) ."\.";
    if ( $type eq 'first') {
        $ip .= bin2hex(substr($bin,24,8)) + 1 ;
    } elsif ($type eq 'broad') {
        $ip .= bin2hex(substr($bin,24,8)) - 1 ;
    } else {
        $ip .= bin2hex(substr($bin,24,8));
    }
    return $ip;
}

sub normalization {
    my $line = shift;
    my ($ip,$mask) = split(/\//,$line);
    return($ip,$mask);
}

my ($ip,$net_mask) = normalization(@ARGV[0]);

my $host_mask = 32-$net_mask;
my $max_host = POSIX::pow(2,$host_mask)-2;
my $bin_mask = "1" x $net_mask . "0" x $host_mask;

my $bin_ip ;
$bin_ip .= hex2bin($_) foreach (split(/\./,$ip));

my $min = $bin_ip &#038; $bin_mask;
my $max_mask = "0" x $net_mask . "1" x $host_mask;
my $max = $bin_ip  | $max_mask;

my $min_ip = bin2ip($min);
my $min_host_ip = bin2ip($min,'first');
my $max_ip = bin2ip($max);
my $max_host_ip = bin2ip($max,'broad');

print \<\< EOF
IP ADDRESS:\t $ip\t $bin_ip
NETWORK MASK:\t \/$net_mask\t\t $bin_mask
WILDCARD:\t \/$host_mask\t\t $max_mask
NETWORK IP:\t $min_ip\/$net_mask\t $min
BROADCAST ADDR:\t $max_ip\t $max
FIRST ADDR:\t $min_host_ip
LAST ADDR:\t $max_host_ip
MAX HOSTS\t $max_host
EOF
</pre>
<p>Run scripts with one argument ip/netmask... Unfortunately, you can use only short network mask type :</p>
<pre>./ipcalc 192.157.9.3/21</pre>
<p>the output looks like this:</p>
<pre>
IP ADDRESS:      192.157.9.3     11000000100111010000100100000011
NETWORK MASK:    /21             11111111111111111111100000000000
WILDCARD:        /11             00000000000000000000011111111111
NETWORK IP:      192.157.8.0/21  11000000100111010000100000000000
BROADCAST ADDR:  192.157.15.255  11000000100111010000111111111111
FIRST ADDR:      192.157.8.1
LAST ADDR:       192.157.15.254
MAX HOSTS        2046
</pre>
]]></content:encoded>
			<wfw:commentRss>http://andriigrytsenko.net/2010/03/ipcalc-on-perl/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Perl performance optimization</title>
		<link>http://andriigrytsenko.net/2010/03/perl-performance-optimization/</link>
		<comments>http://andriigrytsenko.net/2010/03/perl-performance-optimization/#comments</comments>
		<pubDate>Mon, 01 Mar 2010 18:05:55 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Scripting]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[scripts]]></category>

		<guid isPermaLink="false">http://andriigrytsenko.net/?p=671</guid>
		<description><![CDATA[I&#8217;d like to describe my own experience with perl optimization. There several common way to make you code works more faster.

I used perl module Benchmark to make measurements. Rules:
1. If you can use single quotes instead of double quote, do it. In most cases it will be executed more faster because of string in single [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;d like to describe my own experience with perl optimization. There several common way to make you code works more faster.<br />
<span id="more-671"></span></p>
<p>I used perl module Benchmark to make measurements. Rules:</p>
<p>1. If you can use single quotes instead of double quote, do it. In most cases it will be executed more faster because of string in single quotes is not going to concatenate. </p>
<p>2. Use compiled regexps in case you have loops. It can brings huge performance: </p>
<pre>
use Benchmark;

sub qwe {
    my $re = shift;
    my $line = "wer";
    foreach my $reg (@$re){
        if ( $line =~ /$reg/) {
            my $l=0;
        }
    }
}

my $line = "sdfsdfsdfsdf";
my $ref = ".*";
my @arr = qw(qwe sdf wer); #plaine regexps
my @c_re;
foreach my $p_re (@arr) { # compile it and put in to the @c_re
    my $re = qr/\b$p_re\b/i ;
    push(@c_re,$re);
}

Benchmark::cmpthese(100000, {
    'Compiled' => sub { qwe(\@c_re) },
    'Not_compiled' => sub { qwe(\@arr) },
});</pre>
<p>As result:</p>
<pre>
                 Rate Not_compiled     Compiled
Not_compiled  36232/s           --         -79%
Compiled     169492/s         368%           --</pre>
<p>3. Extract values from string with <em>substr</em> if you can. There are three well knows ways to do such kind of operation: regular expressions, split and substr. Let&#8217;s compare it:</p>
<pre>sub mksplit {
    my ($line) = @_;
    my ($fst,$snd) = split(/-/,$line);
    return($fst,$snd);
}

sub mkregexp {
    my ($line,$re) = @_;
    $line =~ m/$re/;
    return($1,$2);
}

sub mkstr {
    my ($line) = @_;
    my $pos = index($line,'-');
    my $fst = substr($line,0,$pos);
    my $snd = substr($line,$pos+1);
    return($fst,$snd);
}

my $line = "12-34";
my $n_comp_re = '(\d+)-(\d+)';
my $comp_re = qr/\b$n_comp_re\b/i ;

Benchmark::cmpthese(1000000, {
    'Compiled' => sub { mkregexp($line,$comp_re) },
    'Not_compiled' => sub { mkregexp($line,$n_comp_re) },
    'STR' => sub { mkstr($line) },
    'Split' => sub { mksplit($line) },
});

Benchmark::timethese(1000000, {
    'Compiled' => sub { mkregexp($line,$comp_re) },
    'Not_compiled' => sub { mkregexp($line,$n_comp_re) },
    'STR' => sub { mkstr($line) },
    'Split' => sub { mksplit($line) },
});
</pre>
<p>Where <em>compiled</em> and <em>not_compiles</em>  we use when talk about regular expression.</p>
<pre>
                 Rate        Split     Compiled Not_compiled          STR
Split        297619/s           --          -9%         -12%         -25%
Compiled     327869/s          10%           --          -4%         -17%
Not_compiled 340136/s          14%           4%           --         -14%
STR          396825/s          33%          21%          17%           --
Benchmark: timing 1000000 iterations of Compiled, Not_compiled, STR, Split...
  Compiled:  4 wallclock secs ( 3.14 usr +  0.00 sys =  3.14 CPU) @ 318471.34/s (n=1000000)
Not_compiled:  3 wallclock secs ( 2.91 usr +  0.00 sys =  2.91 CPU) @ 343642.61/s (n=1000000)
       STR:  2 wallclock secs ( 2.53 usr +  0.00 sys =  2.53 CPU) @ 395256.92/s (n=1000000)
     Split:  3 wallclock secs ( 3.19 usr +  0.00 sys =  3.19 CPU) @ 313479.62/s (n=1000000)
</pre>
<p>As you can see from output the more faster way to exract values from string is <em>substr</em>, next is split and regular expressions is the most slower. I don&#8217;t know why compiled regexps is more slow than regular one in this case. I guess its because of 1 iteration. </p>
<p>4. Don&#8217;t use sort in hash loops if there is no necessity. The most faster way to through over the hash is <em>keys</em> statement: </p>
<pre>
sub mkeach {
    my $hash = shift;
    my %hash2;
    while ( my($key,$value) = each(%$hash)){
        $hash2{$key} = $value;
    }
    return(\%hash2);
}

sub mkkeys {
    my $hash = shift;
    my %hash2;
    for my $key (keys(%$hash)){
        $hash2{$key} = $hash->{$key};
    }
    return(\%hash2);
}

sub mksort {
    my $hash = shift;
    my %hash2;
    foreach my $key (sort keys %$hash) {
        $hash2{$key} = $hash->{$key};
    }
    return(\%hash2);
}

my %hash = (name => 'name1', surname => 'surname1', address => 'address', var => 'var1', var2 => 'var2');

Benchmark::cmpthese(1000000, {
    'Keys' => sub { mkkeys(\%hash) },
    'Each' => sub { mkeach(\%hash) },
    'Sort' => sub { mksort(\%hash) },
});

Benchmark::timethese(1000000, {
    'Keys' => sub { mkkeys(\%hash) },
    'Each' => sub { mkeach(\%hash) },
    'Sort' => sub { mksort(\%hash) },
});
</pre>
<pre>
        Rate Each Sort Keys
Each 66800/s   --  -3% -14%
Sort 68681/s   3%   -- -11%
Keys 77459/s  16%  13%   --
Benchmark: timing 1000000 iterations of Each, Keys, Sort...
      Each: 16 wallclock secs (14.92 usr +  0.00 sys = 14.92 CPU) @ 67024.13/s (n=1000000)
      Keys: 12 wallclock secs (12.91 usr +  0.00 sys = 12.91 CPU) @ 77459.33/s (n=1000000)
      Sort: 15 wallclock secs (14.62 usr +  0.00 sys = 14.62 CPU) @ 68399.45/s (n=1000000)
</pre>
<p>That&#8217;s all for now, but I&#8217;ll continue to optimize my code so I think there will be an updates soon.</p>
]]></content:encoded>
			<wfw:commentRss>http://andriigrytsenko.net/2010/03/perl-performance-optimization/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Check if the perl module is installed</title>
		<link>http://andriigrytsenko.net/2010/02/check-if-the-perl-module-is-installed/</link>
		<comments>http://andriigrytsenko.net/2010/02/check-if-the-perl-module-is-installed/#comments</comments>
		<pubDate>Tue, 23 Feb 2010 15:40:25 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Scripting]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[unix]]></category>

		<guid isPermaLink="false">http://andriigrytsenko.net/?p=665</guid>
		<description><![CDATA[Under cover you can find some trick and script to check perl module installation.

To figure out whether perl module is installed at the system. Run:
perl -M&#60;module_name&#62; -e 1
if command return an error or NOT 0(zero) then module is not installed at the moment.
Here is also perl script which help you check a lot of modules:
#!/usr/local/bin/perl

my [...]]]></description>
			<content:encoded><![CDATA[<p>Under cover you can find some trick and script to check perl module installation.<br />
<span id="more-665"></span></p>
<p>To figure out whether perl module is installed at the system. Run:</p>
<pre>perl -M&lt;module_name&gt; -e 1</pre>
<p>if command return an error or NOT 0(zero) then module is not installed at the moment.</p>
<p>Here is also perl script which help you check a lot of modules:</p>
<pre>#!/usr/local/bin/perl

my @modules = qw (put your modules here separated by space without any quotes);

foreach my $module (@modules){
print $module,"\t";
my $result=system("perl -M$module -e 1");
print "Installed" if (!$result);
print "\n";
}</pre>
<p>EOF</p>
]]></content:encoded>
			<wfw:commentRss>http://andriigrytsenko.net/2010/02/check-if-the-perl-module-is-installed/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Network bandwidth script</title>
		<link>http://andriigrytsenko.net/2010/02/network-bandwidth-script/</link>
		<comments>http://andriigrytsenko.net/2010/02/network-bandwidth-script/#comments</comments>
		<pubDate>Mon, 01 Feb 2010 17:35:44 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Scripting]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[unix]]></category>

		<guid isPermaLink="false">http://andriigrytsenko.net/?p=634</guid>
		<description><![CDATA[I spent several hours to find suitable script which help me figure out my network speed between two servers. But it was fruitlessly. So, I decided to make my own script with blackjack and hoockers. (c)

My Perl script require several things :

Perl interpreter of course
IO::Socket perl module
Getopt::Long perl module

I will tell you nothing about first [...]]]></description>
			<content:encoded><![CDATA[<p>I spent several hours to find suitable script which help me figure out my network speed between two servers. But it was fruitlessly. So, I decided to make my own script with blackjack and hoockers. (c)</p>
<p><span id="more-634"></span></p>
<p>My Perl script require several things :</p>
<ol>
<li>Perl interpreter of course</li>
<li>IO::Socket perl module</li>
<li>Getopt::Long perl module</li>
</ol>
<p>I will tell you nothing about first one. To install perl modules run <em>cpan</em> as root:</p>
<pre>cpan -i Getopt::Long</pre>
<p>and</p>
<pre>cpan -i IO::Socket</pre>
<p>When installation will be finished. Download <a href="http://andriigrytsenko.net/files/bandtest_pl.txt">bandtest.pl</a> and save it at 1-st server.</p>
<p>Now go to the second one and run <em>netcat</em>(please make sure you have this package installed):</p>
<pre>server02# nc -4tl 7777 &gt; /dev/null</pre>
<p>You can use any free port instead port 7777.</p>
<p>Back to first server and run <em>bandtest.pl</em>, but before make sure there is no any firewalls rules which hamper in connection between to hosts on port 7777.</p>
<p>Use <em>telnet</em> to check connection:</p>
<pre>server01# telnet server02 7777
Connected to kivras01.
Escape character is '^]'.</pre>
<p>This output means connection is successfully opened, so you can continue.</p>
<p>Make file with size for example 100Mb(I advice you to don&#8217;t use very big files, 100Mb is fit size which doesn&#8217;t fill your memory out and enough for proper results.  I intentionally put file into memory before sending the data&#8217;s. This allow me delete any I/O operation during the network connection and as result prevent any result distortion.):</p>
<pre>server01# dd if=/dev/zero of=/tmp/test bs=1024 count=1000000
100000+0 records in
100000+0 records out
102400000 bytes (102 MB) copied, 1.20234 s, 85.2 MB/s</pre>
<p>Run <em>bandtest.pl</em> to get a results:</p>
<pre>server01$ ./bandtest.pl -h server02 -p 7777 -f /tmp/test
Bytes were transfered 102.40 MB
Seconds taken: 8
Average speed 12.80 MB
</pre>
<p>EOF.</p>
]]></content:encoded>
			<wfw:commentRss>http://andriigrytsenko.net/2010/02/network-bandwidth-script/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Google maps API to show users location</title>
		<link>http://andriigrytsenko.net/2010/01/google-maps-api/</link>
		<comments>http://andriigrytsenko.net/2010/01/google-maps-api/#comments</comments>
		<pubDate>Sun, 17 Jan 2010 20:18:39 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Scripting]]></category>
		<category><![CDATA[perl]]></category>

		<guid isPermaLink="false">http://andriigrytsenko.net/?p=605</guid>
		<description><![CDATA[I will show you how to use google maps to print location of your site visitors.

First of all you need to get Google KEY, to do this you should have google account. If you have it visit google key page to generate the map key.
After you&#8217;ve done it -&#62; you will be ready for next [...]]]></description>
			<content:encoded><![CDATA[<p>I will show you how to use google maps to print location of your site visitors.<br />
<span id="more-605"></span></p>
<p>First of all you need to get Google KEY, to do this you should have google account. If you have it visit <a href="http://code.google.com/apis/maps/signup.html">google key page</a> to generate the map key.</p>
<p>After you&#8217;ve done it -&gt; you will be ready for next steps.</p>
<p>Below the script should be downloaded in your html directory as location.cgi for example:</p>
<pre>#!/usr/bin/perl                                              

use strict;
use LWP;   

sub GetCode {
 my ($map_key,$lat,$long,$zoom)=@_;

 my $head = &lt;&lt;HEAD;
 &lt;title&gt;Your current location&lt;/title&gt;
 &lt;script src="http://maps.google.com/maps?file=api&amp;amp;v=2&amp;amp;sensor=false&amp;amp;key=$map_key"
 type="text/javascript"&gt;&lt;/script&gt;
 &lt;script type="text/javascript"&gt;
 function initialize() {
 if (GBrowserIsCompatible()) {
 var map = new GMap2(document.getElementById("map_canvas"));
 map.setCenter(new GLatLng($lat, $long), $zoom);
 map.setUIToDefault();
 }
 }
 &lt;/script&gt;
HEAD
 my $body= &lt;&lt;BODY;
 &lt;body onload="initialize()" onunload="GUnload()"&gt;
 Your current location is $ENV{REMOTE_ADDR}:&lt;br&gt;
 &lt;div id="map_canvas" style="width: 500px; height: 300px"&gt;&lt;/div&gt;
 &lt;/body&gt;
BODY
return($head,$body);
}                                                                                                

sub GetHTMLCode {
 my ($head,$body)=@_;
 my $html = &lt;&lt;HTML;
 &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;
 &lt;html xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml"&gt;
 &lt;head&gt;
 &lt;meta http-equiv="content-type" content="text/html; charset=utf-8"/&gt;
 $head
 &lt;/head&gt;
 $body
HTML
 return $html;
}

sub GetCoordinates {
 my $ip = shift;
 my ($lat,$long);
 my $ua = new LWP::UserAgent;
 $ua-&gt;agent("Opera/9.0 " . $ua-&gt;agent);
 $ua-&gt;timeout("5");

 my $req = new HTTP::Request( GET =&gt; "http://www.geobytes.com/IpLocator.htm?GetLocation&amp;template=php3.txt&amp;IpAddress=$ip");
 my $res = $ua-&gt;request($req);
 if (! $res-&gt;is_success) {
 die "can't get acccess to server \n";
 }
 my $content=$res-&gt;content;
 foreach my $str (split(/\n/,$content)){
 $lat = $1 if ($str =~ /meta name=.latitude. content=.(\d+\.\d+)./);
 $long = $1 if ($str =~ /meta name=.longitude. content=.(\d+\.\d+)./);
 }
 return($long,$lat);
}
my $map_key="";
my $zoom="10";
my $ip_addr=$ENV{REMOTE_ADDR};
my ($long,$lat)=GetCoordinates($ip_addr);
my ($head,$body)=GetCode($map_key,$lat,$long,$zoom);
my $html = GetHTMLCode($head,$body);
print "Content-type: text/html\n\n";
print $html;</pre>
<p>This script also available by this <a href="http://andriigrytsenko.net/files/location.txt">link</a>.<br />
Your google key should be defined as variable <strong>map_key</strong> in the code.</p>
<p>Function <strong>GetCode</strong> &#8211; return two values:</p>
<ol>
<li><em>$head</em> &#8211; this block should be put into the &lt;head&gt; section of your html code.</li>
<li><em>$body</em> &#8211; into the &lt;body&gt; section.</li>
</ol>
<p>Function <strong>GetCoordinates</strong> return <em>longitude</em> and <em>latitude </em>of got ip.</p>
<p>Function  <strong>GetHTMLCode </strong>was used<strong> </strong>to generate example html code.</p>
<p>You also can implement these functions into your page as module. Download <a href="http://andriigrytsenko.net/files/google_map.pm">google_map.pm</a> and save it in your perl modules directory or do it as describe in this  <a href="http://andriigrytsenko.net/2009/11/use-perl-module-without-root-access/">post</a>. After, you can easily call this functions from your code as show below:</p>
<pre>#!/usr/bin/perl

use lib "path/to/directory/where/you/save/google_map.pm";
require google_map;

my $map_key="your_map_key_was_described_below";
my $zoom="10";
my $ip_addr=$ENV{REMOTE_ADDR};
my ($long,$lat)=google_map::GetCoordinates($ip_addr);
my ($head,$body)=google_map::GetCode($map_key,$lat,$long,$zoom);
my $html = google_map::GetHTMLCode($head,$body);
print "Content-type: text/html\n\n";
print $html;</pre>
<p>To see how it work check this <a href="http://andriigrytsenko.net/google_map.cgi">link</a> out.</p>
]]></content:encoded>
			<wfw:commentRss>http://andriigrytsenko.net/2010/01/google-maps-api/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Perl debug</title>
		<link>http://andriigrytsenko.net/2009/12/perl-debug/</link>
		<comments>http://andriigrytsenko.net/2009/12/perl-debug/#comments</comments>
		<pubDate>Wed, 23 Dec 2009 12:13:58 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Scripting]]></category>
		<category><![CDATA[perl]]></category>

		<guid isPermaLink="false">http://andriigrytsenko.net/?p=535</guid>
		<description><![CDATA[One of the perl debug way.

Perl module  PadWalker is require. Install it over cpan util:
cpan -i PadWalker 
Let&#8217;s create simple script:
$cat ./hello.pl
#!/usr/bin/perl
sub say_hello {
my $what=shift;
print "Hello $what\n";
}

my $var1="var1";
my $var2="var2";
say_hello("you");
my $var3="var3";

And try to debug it:
$ perl -dF hello.pl
Loading DB routines from perl5db.pl version 1.31
Editor support available.Enter h or `h h' for help, or `man perldebug' [...]]]></description>
			<content:encoded><![CDATA[<p>One of the perl debug way.<br />
<span id="more-535"></span></p>
<h4>Perl module  <em>PadWalker</em> is require. Install it over <em>cpan</em> util:</h4>
<pre><code>cpan -i PadWalker </code></pre>
<h4>Let&#8217;s create simple script:</h4>
<pre><code>$cat ./hello.pl</code></pre>
<pre>#!/usr/bin/perl
sub say_hello {
my $what=shift;
print "Hello $what\n";
}

my $var1="var1";
my $var2="var2";
say_hello("you");
my $var3="var3";
</pre>
<h4>And try to debug it:</h4>
<pre><code>$ perl -dF hello.pl</code></pre>
<pre>Loading DB routines from perl5db.pl version 1.31
Editor support available.Enter h or `h h' for help, or `man perldebug' for more help.main::(hello.pl:8):     say_hello("you");
DB&lt;1&gt;
</pre>
<h4>type <strong>&#8220;h&#8221;</strong> to get help.  To list  a function use <strong>&#8220;l function_name&#8221;</strong>:<br />
</h4>
<pre> DB&lt;1&gt; l say_hello
3       sub say_hello {
4:              my $what=shift;
5:              print "Hello $what\n";
6       }</pre>
<h4>To make step without entry to function use <strong>&#8220;n&#8221;</strong>. To  go through function <strong>&#8220;s&#8221;</strong></h4>
<pre> DB&lt;2&gt; s
main::say_hello(hello.pl:4):            my $what=shift;
</pre>
<h4>To return from subroutine use <strong>&#8220;r&#8221;</strong>.</h4>
<h4> use <strong>&#8220;y&#8221;</strong> to print all variables and their values or use <strong>&#8220;y variable&#8221;</strong> to print specific.</h4>
<pre> DB&lt;1&gt; y
$var1 = 'var1'
$var2 = 'var2'</pre>
<h4>and</h4>
<pre>DB&lt;1&gt; y var1
$var1 = 'var1'
</pre>
<h4> Options <strong>&#8220;M&#8221;</strong> print all used modules and path to them:</h4>
<pre> DB&lt;2&gt; M
'AutoLoader.pm' =&gt; '5.67 from /usr/local/lib/perl5/5.8.9/AutoLoader.pm'
'Carp.pm' =&gt; '1.10 from /usr/local/lib/perl5/5.8.9/Carp.pm'
...</pre>
<h4> <strong>&#8220;R&#8221;</strong> &#8211; restart the scripts.<br />
</h4>
<h4> <strong>&#8220;V&#8221;</strong> &#8211; list variables<br />
</h4>
<h4> <strong>&#8220;S&#8221;</strong> &#8211; list subroutine<br />
</h4>
<h4> <strong>&#8220;q&#8221;</strong> &#8211; for exit </h4>
]]></content:encoded>
			<wfw:commentRss>http://andriigrytsenko.net/2009/12/perl-debug/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Use perl module without root access</title>
		<link>http://andriigrytsenko.net/2009/11/use-perl-module-without-root-access/</link>
		<comments>http://andriigrytsenko.net/2009/11/use-perl-module-without-root-access/#comments</comments>
		<pubDate>Fri, 27 Nov 2009 14:46:25 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Scripting]]></category>
		<category><![CDATA[perl]]></category>

		<guid isPermaLink="false">http://andriigrytsenko.net/?p=512</guid>
		<description><![CDATA[Take this simple instruction if you need to include module (which is not installed at your system) to your perl script.


Get module and install it to directory where you have write permission (For example: /home/user1/libs).
Go to your script and add new library path to your INC array:
use lib '/home/user1/libs';
to check that it was successfully put [...]]]></description>
			<content:encoded><![CDATA[<p>Take this simple instruction if you need to include module (which is not installed at your system) to your perl script.<br />
<span id="more-512"></span></p>
<ol>
<li>Get module and install it to directory where you have write permission (For example: <em>/home/user1/libs</em>).</li>
<li>Go to your script and add new library path to your INC array:</li>
<pre>use lib '/home/user1/libs';</pre>
<p>to check that it was successfully put in INC array, print it out</p>
<pre>print "\@INC = @INC\n";</pre>
<li>Include it to your script with &#8220;<em>require</em>&#8221; or &#8220;<em>use</em>&#8221; directives.</li>
<pre>require "test.pm";</pre>
<p>or</p>
<pre>use "test.pm";</pre>
</ol>
<p style="text-align: center;"><strong>SAMPLE</strong></p>
<p>Here is some little sample how it&#8217;s work. <strong></strong></p>
<p><strong>Our module:</strong></p>
<pre>user1@agrytsenko:~/tmp2$ cat lib/test.pm
package test;

use strict;

sub say {
my $word=shift;
my $str="Hello my $word world\n";
return($str);
}
1;
__END__</pre>
<p><strong>Our script:</strong></p>
<pre>user1@agrytsenko:~/tmp2$ cat bin/test.pl
#!/usr/bin/perl

use lib '/home/user1/tmp2/lib';
require 'test.pm';

my $string=test::say("good");
print $string, "\n";</pre>
<p><strong>Our running result:</strong></p>
<pre>sheva@agrytsenko:~/tmp2/bin$ ./test.pl
Hello my good world
</pre>
]]></content:encoded>
			<wfw:commentRss>http://andriigrytsenko.net/2009/11/use-perl-module-without-root-access/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Anonymous proxy checker</title>
		<link>http://andriigrytsenko.net/2009/07/anonymous-proxy-checker/</link>
		<comments>http://andriigrytsenko.net/2009/07/anonymous-proxy-checker/#comments</comments>
		<pubDate>Sat, 04 Jul 2009 11:29:53 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Scripting]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[proxy]]></category>

		<guid isPermaLink="false">http://andriigrytsenko.net/?p=201</guid>
		<description><![CDATA[There is anonymous proxy checker written by me.

To run it on your box you should have perl interpreter and some perl modules. Such as:
LWP::UserAgent
Getopt::Long 
You can get it directly from site or over the cpan util as well.  The source of script here You have to put it on your computer. Also  you [...]]]></description>
			<content:encoded><![CDATA[<p>There is anonymous proxy checker written by me.<br />
<span id="more-201"></span></p>
<p>To run it on your box you should have perl interpreter and some perl modules. Such as:</p>
<p><strong>LWP::UserAgent<br />
Getopt::Long </strong></p>
<p>You can get it directly from <a href="http://cpan.org">site</a> or over the cpan util as well.  The source of script <a href="http://andriigrytsenko.net/files/proxy_checker.txt">here</a> You have to put it on your computer. Also  you need get the list of proxy(It can be easily done by google, just type &#8220;anonymous proxy list&#8221; and you will get a lot of links). Take list and put into the file. Like this : </p>
<pre>
agrytsenko:~/tmp$ cat proxy.2
64.66.192.61:80 0.8680  Transp. US      Y       65%     2008-12-04 09:51:23
89.114.36.221:8000      1.0750  Elite   RO      ?       30%     2008-12-04 09:39:18
74.65.65.195:8000       1.1030  Elite   US      Y       47%     2008-12-04 09:40:49
89.97.128.86:6588       1.1630  Elite   IT      N       33%     2008-12-04 09:37:52
200.179.72.132:80       1.1830  Transp. BR      Y       91%     2008-12-04 09:51:23
207.182.129.90:3128     1.2570  Transp. US      Y       54%     2008-12-04 09:51:23
207.182.129.91:3128     1.2850  Transp. US      Y       57%     2008-12-04 09:51:23
66.98.152.157:6654      1.4750  Elite   US      Y       11%     2008-12-04 09:37:52
92.48.119.220:6654      1.6100  Elite   GB      Y       29%     2008-12-04 09:36:26</pre>
<p>or like this: </p>
<pre>
agrytsenko:~/tmp$ cat proxy
64.66.192.61:80
150.101.196.52:3128
114.30.47.10:80
114.127.246.36</pre>
<p>The main rule of file format: <strong>the first field has to be ip:port of proxy.</strong> </p>
<p>The script has several keys. Here they are:</p>
<pre>
agrytsenko:~/tmp$ ./proxy_checker.pl -h
-f file         Specify file
-v              Turn on verbose mode
-h              Print this help</pre>
<p>Okay, now you are ready to run it: </p>
<pre>
agrytsenko:~/tmp$ ./proxy_checker.pl -f ./proxy -v
Try to open proxy list ./proxy
Connect to site over 64.66.192.61:80...
Connection was not established. The reason is :
500 Can't connect to 64.66.192.61:80 (connect: timeout)</pre>
<pre>
Connect to site over 150.101.196.52:3128...
Success connected with IP=150.101.196.52
Proxy 150.101.196.52:3128 is avaible for connections
The 150.101.196.52:3128 is not fully anonymous. You ip address is 79.124.129.220</pre>
<pre>
Connect to site over 114.30.47.10:80...
Connection was not established. The reason is :
503 Service Unavailable</pre>
<pre>
Connect to site over 114.127.246.36:8080...
Success connected with IP=114.127.246.36
Proxy 114.127.246.36:8080 is avaible for connections</pre>
<p>As you can see we have only one fully anonymous proxy for now. It&#8217;s 114.127.246.36. </p>
<p>Done.</p>
]]></content:encoded>
			<wfw:commentRss>http://andriigrytsenko.net/2009/07/anonymous-proxy-checker/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Simple HA cluster for linux</title>
		<link>http://andriigrytsenko.net/2009/06/103/</link>
		<comments>http://andriigrytsenko.net/2009/06/103/#comments</comments>
		<pubDate>Thu, 25 Jun 2009 10:03:39 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Scripting]]></category>
		<category><![CDATA[web]]></category>
		<category><![CDATA[clusters]]></category>
		<category><![CDATA[ha cluster]]></category>
		<category><![CDATA[perl]]></category>

		<guid isPermaLink="false">http://andriigrytsenko.net/?p=103</guid>
		<description><![CDATA[There is the description of concept of simple HA for Linux consist of 2 nodes .

Week ago I start to read book about Linux clustering. When I learned about high available cluster I have decided to made some script which will be able to implement it. 
THEORY:
We have 2 Linux machine, local network, Internet connection [...]]]></description>
			<content:encoded><![CDATA[<p>There is the description of concept of simple HA for Linux consist of 2 nodes .<br />
<span id="more-103"></span></p>
<p>Week ago I start to read book about Linux clustering. When I learned about high available cluster I have decided to made some script which will be able to implement it. </p>
<p>THEORY:<br />
We have 2 Linux machine, local network, Internet connection and border gateway.<br />
<img src="http://andriigrytsenko.net/files/Diagram1.jpeg" alt="Diagram1" /><br />
As you can see at Figure.1.<br />
Node1 and Node2 have internal IPs 10.0.30.1 and 10.0.30.2 respectively. Default route has ip 10.0.30.254. And it provide PAT(Port Address Translation) support . In other words translates all request from internet web clients to web server into localnet virtual ip 10.0.30.3 which always assigned to active node. I will skip how to install PAT support but share my knowledge&#8217;s in rest part of project. </p>
<p>PRACTICE:<br />
First of all we need machine with preinstalled Linux OS, web server, rsync and ssh. In this example I used CentOS and apache but it does not metter. </p>
<p>Try to build network connections.<br />
Network configuration for  Node #1 : </p>
<pre>#cat /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
HWADDR=08:00:27:7B:7E:03
ONBOOT=yes
IPADDR=10.0.30.2
GATEWAY=10.0.30.254
</pre>
<p>For node #2: </p>
<pre>#cat /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
HWADDR=08:00:27:B9:50:94
ONBOOT=yes
IPADDR=10.0.30.1</pre>
<p>GATEWAY=10.0.30.254 </p>
<p>In next step you need to enable rsync in xinted config: </p>
<pre>#cat /etc/xinetd.d/rsync
service rsync
{
        <strong>disable = no</strong>
        socket_type     = stream
        wait            = no
        user            = root
        server          = /usr/bin/rsync
        server_args     = --daemon
        log_on_failure  += USERID
}
</pre>
<p>And restart it:</p>
<pre>/etc/init.d/xinted restart</pre>
<p>Also you need to enable some SELinux bool: </p>
<pre>#setsebool -P rsync_export_all_ro=on</pre>
<p>Add user rsync in group root for both nods and generate ssh keys for they. Below described step-by-step actions: </p>
<p>For Node #1 :<br />
Add user: </p>
<pre>#useradd -g root rsycn </pre>
<p>Login as rsync </p>
<pre>#su - rsync </pre>
<p>Generate new rsa key:</p>
<pre>#ssh-keygen -t rsa -b 2048 -f rsync.key</pre>
<p>When ssh-keygen asks you to provide passprase. Hit Enter twice to get non-password access in the future.</p>
<p>Copy public key to the remote host:</p>
<pre>#scp rsync.key.pub root@10.0.30.2:/root/.ssh</pre>
<p>For node #2 :</p>
<pre>#useradd -g root rsycn
#mv ~/.ssh/rsync.key.pub  ~rsync/.ssh/authorized_keys
#chown rsync ~rsync/.ssh/authorized_keys
#chmod 600 ~rsync/.ssh/authorized_keys
#su - rsync
#ssh-keygen -t rsa -b 2048 -f rsync.key
#scp rsync.key.pub root@10.0.30.1:/root
</pre>
<p>Back to Node #1 and perform next actions: </p>
<pre>#mv ~/rsync.key.pub  ~rsync/.ssh/authorized_keys
#chown rsync ~rsync/.ssh/authorized_keys
#chmod 600 ~rsync/.ssh/authorized_keys
</pre>
<p>To check if the keys and rsync works properly. Run this on Node #2:</p>
<pre>#rsycn -e ssh rsync@10.0.30.1:/var/www/html /var/www/</pre>
<p>After that if you have done everything right. The web files from Node #1 should be transfered to your local web directory. </p>
<p>Let&#8217;s check our config file: </p>
<pre>#cat node.conf
another_node_addr=10.0.30.1
# this is comment
virtual_addr=10.0.30.3
max_sleep_time=10
max_ping_cnt=5
log_facility=local6
debug=1
</pre>
<p>All lines begging from &#8220;#&#8221; interpreted as a comments. Therefore it&#8217;s ignored by script. </p>
<p><strong>another_node_addr</strong>  &#8211; defined internal ip of other node<br />
<strong>virtual_addr</strong> &#8211; defined common virtual ip for both node<br />
<strong>max_sleep_time</strong>  &#8211;  max value for sleep(in seconds) between pings<br />
<strong>max_ping_cnt</strong>  &#8211;  max amount of ping attempts<br />
<strong>log_facility</strong> &#8211; the syslog facility for the logging<br />
<strong>sycn_time</strong> &#8211; time for syncronization web documents from master to slave web-server. &#8220;d&#8221; &#8211; for day, &#8220;h&#8221; &#8211; hours, &#8220;min&#8221; &#8211; minutes, &#8220;mon&#8221; &#8211; months. For example:<br />
sync_time=50min &#8211; means that files have to be syncronized every 50 minutes.<br />
<strong>debug</strong> &#8211; the value &#8220;1&#8243; is turn on debug </p>
<p>Be careful the maximum migration time you can calculate in next way:<br />
<strong>max_sleep_time*(max_ping_cnt+1)+crontab </strong>,<br />
by default it&#8217;s 10*(5+1)= 60 + 60 (crontab) =120 seconds.</p>
<p>Download <a href="http://andriigrytsenko.net/files/check_node.txt">main script</a> and <a href="http://andriigrytsenko.net/files/node.conf">config file</a> and put these into the your nodes.  </p>
<p>You can see a little bit of explanation about how to script works in block scheme below.<br />
<img src="http://andriigrytsenko.net/files/diagram2_small.jpeg" alt="block scheme" /></p>
<p>After these steps you are ready to tune your syslog and crontab configuration. <strong>Actions described below should be done on both node&#8217;s</strong>.<br />
Put into the /etc/syslog.conf:</p>
<pre>log_facility.*                                                -/var/log/node.log</pre>
<p>Where log_facility is log facility defined at node.conf.</p>
<p>Put this into crontab to make script run by crontab every minute: </p>
<pre>#crontab -e
*/1 * * * *     /PATH/TO/check_node.pl /PATH/TO/node.conf</pre>
<p>I suppost it will be worked for you as well as for me.<br />
The End.</p>
]]></content:encoded>
			<wfw:commentRss>http://andriigrytsenko.net/2009/06/103/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Simple web statistic script</title>
		<link>http://andriigrytsenko.net/2009/06/simple-web-statistic-script/</link>
		<comments>http://andriigrytsenko.net/2009/06/simple-web-statistic-script/#comments</comments>
		<pubDate>Sun, 14 Jun 2009 19:24:08 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Scripting]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[web]]></category>

		<guid isPermaLink="false">http://andriigrytsenko.net/?p=53</guid>
		<description><![CDATA[There is a little script which provide simple statistic about web visitation. 

Set &#8220;rotation_period&#8221; by days at variable section to define log rotation period. Set to &#8220;0&#8243; if you want to turn off rotation.
NOTICE: This script works only if perl interpreter is available on web server. You can check it in next way:
$ perl -v
This [...]]]></description>
			<content:encoded><![CDATA[<p>There is a little script which provide simple statistic about web visitation. </p>
<p><span id="more-53"></span></p>
<p>Set &#8220;rotation_period&#8221; by days at variable section to define log rotation period. Set to &#8220;0&#8243; if you want to turn off rotation.</p>
<p>NOTICE: This script works only if perl interpreter is available on web server. You can check it in next way:<br />
<code>$ perl -v</p>
<p>This is perl, v5.8.8 built for x86_64-Linux</p>
<p>Copyright 1987-2006, Larry Wall<br />
</code></p>
<p>You can view and download it from <a href="http://andriigrytsenko.net/files/stat.txt">here</a>.</p>
<p>This script should be put into the web server. My web-hoster doesn&#8217;t allow me send mail through shell. That&#8217;s why all lines concerning mail issue are commented out in my script. In case your hoster provides you this feature you can use it after drop comments and define variables related to mail. </p>
<p>Don&#8217;t forget to put script in the crontab. It&#8217;s should be executed at the end of the day:<br />
<code>$ crontab -l<br />
# DO NOT EDIT THIS FILE - edit the master and reinstall.<br />
# (/tmp/crontab.XXXXQpChAn installed on Sun Jun 14 21:56:42 2009)<br />
# (Cron version V5.0 -- $Id: crontab.c,v 1.12 2004/01/23 18:56:42 vixie Exp $)<br />
HOME=/home/virtwww/w_andriigrytse-net_9c210a4f<br />
SHELL=/bin/bash<br />
MAILTO=w_andriigrytse-net_9c210a4f</p>
<p>59 23 * * *     $HOME/stat.pl<br />
</code></p>
<p>Good luck:)</p>
]]></content:encoded>
			<wfw:commentRss>http://andriigrytsenko.net/2009/06/simple-web-statistic-script/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Some perl functions for monitoring</title>
		<link>http://andriigrytsenko.net/2009/06/some-perl-functions-for-monitoring/</link>
		<comments>http://andriigrytsenko.net/2009/06/some-perl-functions-for-monitoring/#comments</comments>
		<pubDate>Fri, 12 Jun 2009 15:30:50 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Scripting]]></category>
		<category><![CDATA[monitoring]]></category>
		<category><![CDATA[perl]]></category>

		<guid isPermaLink="false">http://localhost/?p=16</guid>
		<description><![CDATA[I would like to provide you library which was very useful for me during my working under monitoring system.

To make it works you have to install next perl libraries :
Net::SSH::Expect;
Net::SCP::Expect;
Net::SFTP::Foreign;
Net::FTP;
It&#8217;s easy to do through cpan. For example:
# cpan i Net::SSH::Expect
or download directly from cpan
function  exec_cmd__ :
This function execute command at remote host. Example:
exec_cmd__(hostname, username, [...]]]></description>
			<content:encoded><![CDATA[<p>I would like to provide you library which was very useful for me during my working under monitoring system.<br />
<span id="more-16"></span></p>
<p>To make it works you have to install next perl libraries :</p>
<p>Net::SSH::Expect;<br />
Net::SCP::Expect;<br />
Net::SFTP::Foreign;<br />
Net::FTP;</p>
<p>It&#8217;s easy to do through cpan. For example:</p>
<p><code># cpan i Net::SSH::Expect</code></p>
<p>or download directly from <a href="http://cpan.org">cpan</a></p>
<p><strong>function  exec_cmd__ </strong>:</p>
<p>This function execute command at remote host. Example:</p>
<p>exec_cmd__(hostname, username, password, port, command)</p>
<p><strong>function sub transfer_file__ </strong>:</p>
<p>This function  can get or put file to remote host via ssh. Example :</p>
<p>transfer_file__(hostname, username, password, port, source file|directory, destination file|directory, method)</p>
<p>where option &#8220;method&#8221;  can be &#8220;fh&#8221;(means get file from somewhere and put to localhost) or &#8220;th&#8221;(send file to host)</p>
<p><strong>function sub sftp_func__</strong> :</p>
<p>This function can get and send file to remote computer via sftp protocol. Example<br />
sftp_func__(hostname, username, password, port, source file|directory, destination file|directory, method)</p>
<p>Where option &#8220;method&#8221; can be &#8216;get&#8217;(get file from remote host) and &#8216;put&#8217;(put file to remote host) and &#8216;del&#8217;(delete file from remote host )<br />
<em>NOTICE: if you use method &#8220;del&#8221; please specify the target as &#8220;source_file&#8221; and skip &#8220;destination_file&#8221; option</em></p>
<p><strong>function  ftp_func__</strong> :</p>
<p>This function get and delete remote file via ftp protocol. Example:</p>
<p>ftp_func__(hostname, username, password, port, source file|directory, destination file|directory, method)<br />
where option &#8220;method&#8221; can be &#8216;get&#8217; or &#8216;put&#8217;</p>
<p><strong>function get_file_via_ftp__</strong>:</p>
<p>This function is the same to previous one EXCEPT its doesn&#8217;t put a file at remote computer and when its get a file doesn&#8217;t remove it at remote system. Example:</p>
<p>get_file_via_ftp(hostname, username, password, port, source file|directory,  destination file|directory)</p>
<p><strong>function get_yesterday__</strong>:</p>
<p>This function return date of yesterday in format which was got by function as first argument. Example:</p>
<p>get_yesterday__(&#8221;YYYYDDMM&#8221;), get_yesterday__(&#8221;DDMMYYYY&#8221;) and so on.</p>
<p>How it&#8217;s use? Good question :) </p>
<p>Download <a href="http://andriigrytsenko.net/files/monitoring.pm">this</a> library and put into the /usr/lib/perl5/Net/. Ex.:  </p>
<p><code># cp monitoring.pm /usr/lib/perl5/Net/</code></p>
<p>Create new test script :<br />
<code>$ cat > test.pl<br />
#!/usr/bin/perl<br />
use monitoring;<br />
$yesterday=monitoring::get_yesterday__("DDMMYYYY");<br />
print "$yesterday\n";<br />
</code></p>
<p>Set executable rights:<br />
<code>$ chmod +x test.pl</code></p>
<p>And run it:<br />
<code>$ ./test.pl</code><br />
11062009</p>
<p>That&#8217;s all :)</p>
]]></content:encoded>
			<wfw:commentRss>http://andriigrytsenko.net/2009/06/some-perl-functions-for-monitoring/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
