Friday, November 14, 2008

Perl FTP Test program - Net::FTP - useful for monitoring/ testing

The following is a Perl script that can be used to connect to and list an FTP directory.
This can be extended to accept the paremeters as required - host ip,user password, directory etc.
This also can be run in a loop or from cron to monitor connectivity or to monitor file movements(that is a not so good use!!)

use Net::FTP;
$host = '1x2.1xx.xx.xxx';
$user = 'User';
$pass = 'xxxxxxxxxxx';
$home = '/home/release;
print "Connecting...";
#connect
$ftp = Net::FTP->new($host);
#login
$ftp->login($user,$pass);
print "LoggedIn...";
#slurp
@result = $ftp->ls($home);
print "\nResult...";
for(@result)
{ print "\n";
print $_; }
$ftp->quit;
print "\n...Closed";

Finding lines of text following a text match - awk

Suppose you need to display x lines following a search condition.
For example, display the first 5 lines of trace following an Exception in Tomcat log.

If you are using Solaris/ Linux/ *nix, one way of doing it is to "awk" it.
Get the first five lines after a Keyword named "Exception". Feel free to change the keyword as required.
BEGIN {flag = 0}
/Exception/
{print;
flag = 5;
next}
flag >= 1 {print;
flag--;
next}

Use awk or nawk(if you have a long line - awk has a limitation on line length)
Run it as follows:
nawk -f checkex.awk *
Redirect to a file as follows
nawk -f checkex.awk * > output.txt