Wednesday, December 28, 2011

passsing Arguments to perl script

use strict;
use Time::Local;
use Net::Telnet();
open(OUTPUT,">output.txt");
print "****THIS SCRIPT IS DEVELOPED BY KUMARMANTRI****\n";
print "FORMAT TO RUN THIS SCRIPT===rssi.pl apip suip HOURS INTERVAL\n";
sub tel
{
my ($ip,$s)=@_;
my $t=Net::Telnet->new(Host=>$ip,Prompt=>'/login: /',Timeout=>7)or print OUTPUT "couldn't telnet\n";
$t->cmd(String=> "admin",Prompt=>'/password: /');
$t->cmd(String=> "admin",Prompt=>'/> /');
$t->cmd(String=>'login',Prompt=>'/# /');
my @output=$t->cmd(String=>$s,Prompt=>'/# /');
return(@output);
$t->close;
}
my $count=($ARGV[2]*60)/$ARGV[3];
my $i=$count;
while($i>0)
{
my @ap=($ARGV[0],"wlanconfig ath0 list");
my @su=($ARGV[1],'iwconfig ath0');
###################
my @apsignal=tel(@ap);
chomp($apsignal[1]);
my @rssi=split(/ /,$apsignal[1]);
###################
my @susignal=tel(@su);
chomp($susignal[5]);
my @rss=split(/=/,$susignal[5]);
###################
my $time=scalar localtime;
print "$time\t APRSSI($ap[0]) $rssi[11]  SURSSI($su[0]) $rss[2]\n";
print OUTPUT "$time\t APRSSI($ap[0]) $rssi[11]  SURSSI($su[0]) $rss[2]\n";
my $second=$ARGV[3]*60;
sleep ($second);
$i--;
}
close(OUTPUT);


Recently i needed to work on a script which monitors the signal status of a wireless link, if you gone through my earlier scripts and compares this one with those you find this one some little bit advanced than those...the reason is because i thought myself  kind of like a very beginner of perl so i decided to learn advanced and reduce the code.

here my main focus is explain the passing of arguments to a perl scripts,i designed this in such a way that while running this program itself you can give the inputs.
For example:rssi.pl 10.0.0.1 10.0.0.2 1 5
here the rssi.pl is name of the script and 10.0.0.1, 10.0.0.2,1 and 5 are the inputs..so to make use of this you have to include the $ARGV in your code depends on the inputs you need,here i make use of 4 inputs so included all those 4 inputs in my code as $ARGV[0],$ARGV[1],$ARGV[2],$ARGV[3]

I hope you understand the code and use of arguments, please leave a comment if you have any doubts and suggest any ideas.

oops i forgot to tell you the use of this script, this code captures the rssi of both AP and SU for a particular time with an interval specified by the user.
usage: rssi.pl 10.0.0.1(ap ip) 10.0.0.2(su ip) 1(timeperiod to run in hours) 5(interval in minutes)

Monday, December 12, 2011

Ping with ping module

you might have seen my other ping script which doesn't use any ping module,but here i use the Ping::External module which offers me some extra features like timeout,count and size.i used one file handle which you can dump all your ips in the textfile called ip.txt so, you can put n no of ips  in the file to check the reachability.The output is writes to the output.txt

you can download this module at http://search.cpan.org/~chorny/Net-Ping-External-0.13/External.pm and you can install this by untaring to a particular folder and open prompt and goto that folder and type these following commads.
"make install"
"perl test.pl"
you can also find the installation instructions of this module in the above link itself at the end of the page


#!/usr/bin/perl -w
use Net::Ping::External qw(ping);
open(IP,"ip.txt");
open(OUTPUT,">output.txt");
$count=0;
while(<IP>)
{
$ip=$_;
chomp($ip);
  my $alive = ping(host => "$ip",count=>2,size=>1024,timeout=>3);
  if ($alive)
{
print OUTPUT "$ip is online\n";
print "$ip is online\n" ;
}
 else
{
print OUTPUT "$ip is offline\n";
 print "$ip is offline\n";
}
}$count++;
close(IP);
close(OUTPUT);