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);

Friday, October 28, 2011

Checking the RSSI of an Accesspoint with an interval of 5minutes

Recently i needed to write a script to check the RSSI of an access point for every 5mins interval which is newly deployed at the base-station so i gave a try, the signal status is required for every 5mins with time stamp.

Note:If you are doing telnet into a device you need to know the prompt then we can control everything.

code:
use Net::Cisco;
open(OUTPUT,">output.txt");
sub tel
{
$t=Net::Telnet::Cisco->new(Host=>$ip,Timeout=>7,Errmode=>'return');
$t->login('9190','PP@KUM425');
@output0=$t->cmd(string=>'show clock',prompt=>'/# /');
print OUTPUT "@output0\n";
$t->cmd(string=>'telnet 10.0.0.5',prompt=>'/login: /');
$t->cmd(string=>"admin",prompt=>'/password: /');
$t->cmd(string=>"admin",prompt=>'/> /');
$t->cmd(string=>"login",prompt=>'/# /');
@output=$t->cmd(string=>"iwconfig ath0",prompt=>'/# /');
print OUTPUT $output[5];
$t->close;
}
$i=144;
while($i>0)
{
$ip='10.0.0.2';
tel($ip);
sleep 300;
$i--;
}

so for every 5mins it login into the base-station cisco router and checks it signal, and it runs for 12 hours thats what i assigned the counter as 144 which means 144*5=720 minutes=12 hours.For time stamp i used the cisco router time that what i used "show clock".

Output:

*19:31:43.968 IST Fri Oct 21 2011

          Link Quality=66/94  Signal level=-71 dBm  Noise level=-95 dBm
*19:36:46.360 IST Fri Oct 21 2011

          Link Quality=66/94  Signal level=-71 dBm  Noise level=-95 dBm
*19:41:48.496 IST Fri Oct 21 2011

          Link Quality=66/94  Signal level=-71 dBm  Noise level=-95 dBm
*19:46:54.116 IST Fri Oct 21 2011

          Link Quality=66/94  Signal level=-71 dBm  Noise level=-95 dBm
*19:51:56.648 IST Fri Oct 21 2011

          Link Quality=66/94  Signal level=-71 dBm  Noise level=-95 dBm
*19:56:58.792 IST Fri Oct 21 2011

          Link Quality=66/94  Signal level=-71 dBm  Noise level=-95 dBm
*20:02:00.764 IST Fri Oct 21 2011

          Link Quality=66/94  Signal level=-71 dBm  Noise level=-95 dBm
*20:07:03.108 IST Fri Oct 21 2011

          Link Quality=63/94  Signal level=-72 dBm  Noise level=-95 dBm
*20:12:05.256 IST Fri Oct 21 2011

          Link Quality=66/94  Signal level=-71 dBm  Noise level=-95 dBm
*20:17:07.220 IST Fri Oct 21 2011

          Link Quality=66/94  Signal level=-71 dBm  Noise level=-95 dBm
*20:22:09.372 IST Fri Oct 21 2011

          Link Quality=66/94  Signal level=-71 dBm  Noise level=-95 dBm
*20:27:11.524 IST Fri Oct 21 2011

          Link Quality=66/94  Signal level=-71 dBm  Noise level=-95 dBm
*20:32:13.464 IST Fri Oct 21 2011

         

Wednesday, September 28, 2011

Telnet checking

My boss asked me whether if there is any possibility to check telnet accessibility for a number of routers,all the routers are CISCO so i did download cisco module and installed, as you can see at the second line of the program i used "Net:Cisco module".i kept all the ips in the "iplist.txt" and reading each ip and checking telnet accessibility and also writing output to a output.txt file which writes "telnet success" if access is possible otherwise writes "telnet failed" with respect to each ip.

#/usr/bin/perl
use Net::Cisco;                                                                         
open(IPLIST,"<iplist.txt")or die "couldn't open iplist.txt";          
open(OUTPUT,">output.txt");
$count=0;
while(<IPLIST>)
{
chomp($_);
$data=$_;
tel($data);
$count++;
}
close(IPLIST);
sub tel
{
$tl=0;
$t=Net::Telnet::Cisco->new(Host=>$data,Timeout=>7,Errmode=>'return')or $tl=1;
if($tl==0)
{
print "$data,telnet success\n";
print OUTPUT "$data,Telnet success\n";
$t->close;
}
else
{
    print "$data,Telnet Failed\n";
    print OUTPUT "$data,Device Telnet Failed\n";
}
}
close(OUTPUT);

Sunday, August 14, 2011

Telnet in a Telnet Cisco

use Net::Cisco;
open(CONFIG,">config.csv");
print CONFIG "Router IP,Device IP,Ethernet Mac Address,Description\n";
open(IPLIST,"iplist.txt")or die "couldn't open iplist.txt";
my $count=0;
while(<IPLIST>)
{
chomp($_);
my @data=split(/\^/,$_);
tel($data[0],$data[1]);
$count++;
}
close(IPLIST);
close(CONFIG);
sub tel
{
my($router,$g)=@_;
my $tl=0;
my @output;
my @output0;
my @output1;
my $t=Net::Telnet::Cisco->new(Host=>$router,Timeout=>7,Errmode=>'return')or $tl=1;
if($tl==0)
{
$t->login('9190','M@R42');
@output0=$t->cmd(string=>"$g",prompt=>'/Please enter password: /');
$g=~s/^.......(.*)$/$1/;
if(@output0)
{
@output1=$t->cmd(string=>"public",prompt=>'/]> /');
chomp($output1[0]);
print "$output1[0]\n";
    if($output1[0] eq "Incorrect Password")
    {
    print "$router,$g,Incorrect Password\n";
    print CONFIG "$router,$g,Incorrect Password\n";
    $t->close;
    }
    else
    {
    @output=$t->cmd(string=>"show system",prompt=>'/]>/');
    $t->print('exit');
    $t->close;
    #print @output;
    chomp($g);
    chomp($output[4]);
    chomp($output[11]);
    $output[4] =~ s/^..............................................(.*)$/$1/;
    $output[11] =~ s/^................................(.*)$/$1/;
    print "$router,$g,$output[11],$output[4]\n";
    print CONFIG "$router,$g,$output[11],$output[4]\n";
    }
}
else
{
    print "$router,$g,Telnet Failed\n";
    print CONFIG "$router,$g,Device Telnet Failed\n";
    $t->close;
}
}
else
{
    print "$router,Login Failed\n";
    print CONFIG "$router,$g,Router Login Failed\n";
}
}

Tuesday, August 9, 2011

Telnet Cisco


use Net::Cisco;
$t =Net::Telnet::Cisco->new(Host=>'192.168.2.1');
$t->login('919', 'password');
@output=$t->cmd("show runn");
open(CONFIG,">config.txt");
print CONFIG "@output\n";
close(CONFIG);
$t->close;


The above code can telnets into a Cisco device and and executes a command "show runn" and writes that output to file config.txt
I didn't include any success and failure control statements,please comment..if u need any further information

Thursday, July 7, 2011

Azimuth Calculation

Hi....I developed a script to find the azimuth between two coordinates..here is the simple code of this.


PROGRAM:
use Math::Trig;
use math::complex;

$lat=17.39152778; ##start point
$lon=78.47789444;
$lat0= 17.425328; ##end point
$lon0=78.466847;
$lat1 = deg2rad($lat);
$lon1 = deg2rad($lon);
$lat2 = deg2rad($lat0);
$lon2 = deg2rad($lon0);

$o = atan2(sin($lon2-$lon1)*cos($lat2) ,cos($lat1)*sin($lat2) - sin($lat1)*cos($lat2)*cos($lon2-$lon1));

print "$o\n";
$o1=($o*180)/pi;

print "$o1\n";
$o2=($o1+360)%360;##Gives the azimuth from the starting point
print "$o2\n";
$o2=($o1+180)%360; ##Gives the azimuth from the end point
print "$o2\n";


copy this code , paste it in notepad and save it with ".pl" extension and run it in command prompt because i didn't include any file handling to write the output to any other file so you can see the output on the command only..

Thursday, June 30, 2011

PING TEST

Hi all...
The following program initiates ping and write it to a file whether it is reachable or not..
It takes input from a input file "input.txt",create input.txt and paste all your ip's here and run it, an output.txt automatically created and writes the output to it whether it is reachable or not..

NOTE:I hope you know how to run this code in "windows" and "linux"

PROGRAM:

open(INPUT,"input.txt") or die "couldn't open input.txt";
open(OUTPUT,">output.txt") or die "couldn't open input.txt";
$a=0;
print OUTPUT "IP,STATUS\n";
while(<INPUT>)
{
chomp($_);
$url=($_);
$ping = `ping $url`;

print "$ping";
if ($ping =~ m/Request timed out/)
{
#print "$ping";
print OUTPUT "$url,unreachable\n";

}
else
{

#print "$ping";
print OUTPUT "$url,reachable\n";

}
$a++;
}
close INPUT;
close OUTPUT;

Friday, June 17, 2011

Copy data from one file to another file

This is my first program which Copies data from log.txt file to log1.txt.
Program 1:

open (LOGFILE, "log.txt") or die "I couldn't get at log.txt";
open (ABC,">log1.txt");
for $line(<LOGFILE>)
{

print ABC "$line";
}


output:this program copies all the data from log.txt to log1.txt