Sunday, July 6, 2014

Life, the universe and everything

Your program is to use the brute-force approach in order to find the Answer to Life, the Universe, and Everything. More precisely... rewrite small numbers from input to output. Stop processing input after reading in the number 42. All numbers at input are integers of one or two digits.

Example

Input:
1
2
88
42
99

Output:
1
2
88 
 
My Code: 
$c=1;
chomp($input=<>);
while ($input ne "")
{
if($input>99)
{
exit;
}
@array[$c-1]=$input;
$c++;
chomp($input=<>);
}


$i=0;
while(@array[$i]!=42)
{
if($i!=$c)
{
print "$array[$i]\n";
}
$i++;
}


INPUT:
1
2
45
65
78
42
34
56
68
42
56
78
98

OUTPUT:
1
2
45
65
78

Saturday, July 5, 2014

Prime generator

Generate all prime numbers between two given numbers!(problem from spoj.com)

Input

The input begins with the number t of test cases in a single line (t<=10). In each of the next t lines there are two numbers m and n (1 <= m <= n <= 1000000000, n-m<=100000) separated by a space.

Output

For every test case print all prime numbers p such that m <= p <= n, one number per line, test cases separated by an empty line.

Example

Input:
2
1 10
3 5

Output:
2
3
5
7

3
5
 
MY CODE: 
 
#!/usr/bin/perl
use strict;
use warnings; 
print "Enter the number of times:";
$a=<STDIN>;
chomp($a);
for($b=0;$b<$a;$b++)
{
print "Enter the starting number:";
my $x=<STDIN>;
chomp($x);
print "Enter the end range:";
my $y=<STDIN>;
chomp($y);
print "$x ","$y\n";
for(my $j=$x;$j<=$y;$j++)
{
my $c=0;

for(my $i=1;$i<=$j;$i++)
{
if($j % $i ==0)
{
$c++;
#print $c;
}
}
if($c<=2&&$j!=1)
{
print "$j\n";
}
}
}
 
 
 
stdin 
1
1
10 
 
stdout 
2
3
5
7
 

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";
}
}