CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Advanced:
Help with Telnet commands.

 



cuboidgraphix
User

Jan 21, 2009, 2:15 PM

Post #1 of 19 (5966 views)
Help with Telnet commands. Can't Post

Hi again guys,

Well I've still been trying on my own and I've recently ran into a snag with a script of mine...

The code is as follows:


Code
#!/usr/bin/perl 
# This file is the clear.pl
# This is a script that will telnet into the BSM,
# and clear all SWERR and erase all cleared alarms.

use strict;
use warnings;
use Net::Telnet;
use Text::ParseWords;
use POSIX qw/strftime/;

my $date = strftime("%Y-%m-%d", localtime(time));
my $temp = "/home/bsm/data/temp.txt";
my $user="USR";
my $pass="PWD";
my $server="HOST";
my $t = Net::Telnet->new($server);
$t->waitfor('/login:/');
$t->print("$user");
$t->waitfor('/Password:/');
$t->print("$pass");
$t->waitfor('/%/');
$t->print("cliapp");
$t->waitfor('/>/');
$t->print("alarm;");
$t->waitfor('/FaultName/');

my ($data) = $t->waitfor('/>/');

open(WRITE, ">", "$temp")
or die "Can't open 'temp.txt' $!";
print WRITE $data;
close WRITE;

my $file = $temp || die "Usage: $0 <filename>\n";
open my $FH, '<', $file or die "Can't open '$file' $!";

while (<$FH>) {
if($_ =~ /^#/){
my (undef, $index, $status, $activity, $datetime, $source, $severity, $faultname) = quotewords('\s+', 0, $_);

foreach($faultname =~ /SWERR/){
print $index, "\n";
print $faultname, "\n";
$t->waitfor('/>/'); ## REMOVED THIS LINE ##
$t->print("cd $source;");
$t->waitfor('/>/');
$t->print("action uploadlogsfromcc1;");
$t->waitfor('/>/');
}
}
}
$t->close;

#system("/home/bsm/sendupdate.pl");


Now ... when I run this code I get the following..


Code
1895793 
SWERR log threshold exceeded at CEM, please upload the log // [XCEM] SWERR Log
pattern match timed-out at ./clear.pl line 45


Why is it that my script is timing out? Can you help Fish or Kevin?

Thanks a lot.



Update..

I removed the waitfor line and it worked. But the alarms still didn't clear up.. I think I need to put a sleep(3); on each command.

Will keep you posted.


(This post was edited by cuboidgraphix on Jan 21, 2009, 2:30 PM)


KevinR
Veteran


Jan 21, 2009, 2:27 PM

Post #2 of 19 (5963 views)
Re: [cuboidgraphix] Help with Telnet commands. [In reply to] Can't Post

Sorry, I have no idea. Maybe Fish or Shawn can help.
-------------------------------------------------


FishMonger
Veteran / Moderator

Jan 21, 2009, 4:49 PM

Post #3 of 19 (5959 views)
Re: [cuboidgraphix] Help with Telnet commands. [In reply to] Can't Post

I don't have a BSM, nor do I know what device that is, so I can't test/debug your script. However, the most obvious reason for the timeout is that the regex in the $t->waitfor('/>/') is failing.

I know I've pointed this out in a previous question of yours, but

Quote
Use dump_log() to debug when this method keeps timing-out and you don't think it should.


There are couple issues that I previously pointed out that you haven't corrected.

$t->print("$user");

and other lines like that which quote vars like that, should be
$t->print($user);

see
perldoc -q quoting

Sending the $data to temp file and then immediately read it back in does not make any sense. Just work with $data directly.

If you post the complete contents of $data, I might be able see what needs to be done.


cuboidgraphix
User

Jan 22, 2009, 6:55 AM

Post #4 of 19 (5950 views)
Re: [FishMonger] Help with Telnet commands. [In reply to] Can't Post

Yeah.. the $t->waitfor('/>/') was the problem.. I removed it and it stopped timing out.

You're right about the $t->print($user) ... I guess it's just a bad force of habit that I use the colons.. sowwy. :( (Already fixed it... thanks.)

Well I did want to work with the $data directly as you had showed me before when you stripped them off the \n and worked with it in one line.. but the problem is that I don't know how to do it when dealing with "quotewords". I made several attempts but failed. I'm just not good enough yet ... I'm still learning.

this is a sample of the output.. Same as the last thread.. 'Help me parse this line please'.


Code
# 1684941 Nak    Active   "20 Jan 09 12:14:57" "O%:CBS1:Cells1:MC800BTS1012:MCBTSSubsystem1:Root1:ECC1" MAJOR "AC Power Failure" 
# 1753868 Nak Active "20 Jan 09 16:05:24" "O%:CBS1:Cells1:MC800BTS1006:MCBTSSubsystem1:Root1:RFM2:RadioSector1" MAJOR "PAM output device failure. Reduced output power // FRU:MFRM-2 3 dB reduction"
# 1753886 Nak Active "21 Jan 09 15:15:28" "O%:CBS1:Cells1:MC800BTS1006:MCBTSSubsystem1:Root1:ECC1" MAJOR "Total rectifier current high // PLANCURRHI"
# 1923679 Nak Active "21 Jan 09 20:36:03" "O%:CBS1:Cells1:MC800BTS1008:MCBTSSubsystem1:Root1:CEM1" MINOR "SWERR log threshold exceeded at CEM, please upload the log // [XCEM] SWERR Log"
# 1713589 Nak Active "21 Jan 09 22:41:42" "O%:CBS1:Cells1:MC800BTS1019:MCBTSSubsystem1:Root1:CEM1" MINOR "SWERR log threshold exceeded at CEM, please upload the log // [XCEM] SWERR Log"
# 1784177 Nak Active "22 Jan 09 00:55:13" "O%:CBS1:Cells1:MC800BTS1003:MCBTSSubsystem1:Root1:CEM1" MINOR "Trap log threshold exceeded at CEM, please upload the log // [XCEM] TRAP Log"
# 1852141 Nak Active "22 Jan 09 03:50:38" "O%:CBS1:Cells1:MC800BTS1014:MCBTSSubsystem1:Root1:ECC1" MAJOR "Shelter High Temperature"
# 1876342 Nak Active "22 Jan 09 07:11:19" "O%:CBS1:Cells1:MC800BTS1021:MCBTSSubsystem1:Root1:CEM3" MINOR "SWERR log threshold exceeded at CEM, please upload the log // [XCEM] SWERR Log"



And another question..

on this line in my code..


Code
foreach($faultname =~ /SWERR/){


Is it possible to match for two variables?

for example..

foreach($faultname =~ /SWEER && $activity =~ /Active/){ <-- This doesn't work, but is something like this possible?

Thanks.


(This post was edited by cuboidgraphix on Jan 22, 2009, 7:23 AM)


FishMonger
Veteran / Moderator

Jan 22, 2009, 7:42 AM

Post #5 of 19 (5946 views)
Re: [cuboidgraphix] Help with Telnet commands. [In reply to] Can't Post


Code
#!/usr/bin/perl  
# This file is the clear.pl
# This is a script that will telnet into the BSM,
# and clear all SWERR and erase all cleared alarms.

use strict;
use warnings;
use Net::Telnet;
use Text::ParseWords;
use POSIX qw/strftime/;

my $date = strftime("%Y-%m-%d", localtime(time));
my $temp = "/home/bsm/data/temp.txt";
my $user = "USR";
my $pass = "PWD";
my $server = "HOST";
my $t = Net::Telnet->new(Prompt => '/login:/');

$t->open($server);
$t->login($user, $pass);
$t->waitfor('/%/');
$t->print("cliapp");
$t->waitfor('/>/');
$t->print("alarm;");
$t->waitfor('/FaultName/');

my ($data) = $t->waitfor('/>/');

foreach my $line (split /\n/, $data) {
my (undef, $index, $status, $activity, $datetime, $source, $severity, $faultname) = quotewords('\s+', 0, $line);

if ($faultname =~ /^SWERR/){
print $index, "\n";
print $faultname, "\n";
$t->print("cd $source;");
$t->waitfor('/>/');
$t->print("action uploadlogsfromcc1;");
$t->waitfor('/>/');
}
}



(This post was edited by FishMonger on Jan 22, 2009, 7:43 AM)


cuboidgraphix
User

Jan 22, 2009, 10:47 AM

Post #6 of 19 (5942 views)
Re: [FishMonger] Help with Telnet commands. [In reply to] Can't Post

Thanks Fish. Again you came through for me.

Finished Script. To 1. Clear the 'Active' Alarms ... and ... 2. To Erase the 'Clear' Alarms.



Code
#!/usr/bin/perl 
# This file is the clear.pl
# This is a script that will telnet into the BSM,
# and clear all SWERR and erase all cleared alarms.

use strict;
use warnings;
use Net::Telnet;
use Text::ParseWords;
use POSIX qw/strftime/;

my $user = "USR";
my $pass = "PWD";
my $server = "HOST";
my $t = Net::Telnet->new($server);

$t->waitfor('/login:/');
$t->print($user);
$t->waitfor('/Password:/');
$t->print($pass);
$t->waitfor('/%/');
$t->print("cliapp");
$t->waitfor('/>/');
$t->print("alarm;");
$t->waitfor('/FaultName/');

my ($data1) = $t->waitfor('/>/');

foreach my $line1 (split /\n/, $data1) {
my (undef, $index1, $status1, $activity1, $datetime1, $source1, $severity1, $faultname1) = quotewords('\s+', 0, $line1);

if ($faultname1 =~ /^SWERR/){
print $index1, "\n";
print $faultname1, "\n";
$t->print("cd $source1;");
$t->waitfor('/>/');
sleep(3);
$t->print("action uploadlogsfromcc1;");
$t->waitfor('/>/');
sleep(3);
}
}

sleep(5);

$t->print("alarm;");
$t->waitfor('/FaultName/');

my ($data2) = $t->waitfor('/>/');

foreach my $line2 (split /\n/, $data2) {
my (undef, $index2, $status2, $activity2, $datetime2, $source2, $severity2, $faultname2) = quotewords('\s+', 0, $line2);

if ($activity2 =~ /^Clear/){
print $index2, "\n";
print $activity2, "\n";
print $faultname2, "\n";
$t->print("alarm -a $index2;");
$t->waitfor('/>/');
sleep(2);
}
}



$t->close;

system("/home/bsm/sendupdate.pl");



FishMonger
Veteran / Moderator

Jan 22, 2009, 11:52 AM

Post #7 of 19 (5938 views)
Re: [cuboidgraphix] Help with Telnet commands. [In reply to] Can't Post

Since you're only using 3 of the vars when you split the line, you might be able to do an array slice.


Code
my ($index1, $source1, $faultname1) = (quotewords('\s+', 0, $line1))[1,5,7];


However, that may not work with the quotewords(..) sub, you'll need to test it.

Why are you using sleep()? It seems to me that it's unnecessary and would slow down yor script.


cuboidgraphix
User

Jan 22, 2009, 12:04 PM

Post #8 of 19 (5935 views)
Re: [FishMonger] Help with Telnet commands. [In reply to] Can't Post

Cool... Thanks for the tip. I'll go ahead and try that.


cuboidgraphix
User

Jan 22, 2009, 1:59 PM

Post #9 of 19 (5931 views)
Re: [cuboidgraphix] Help with Telnet commands. [In reply to] Can't Post

Wicked!!!


Thanks Fish.. it worked. :)


cuboidgraphix
User

Jan 22, 2009, 3:01 PM

Post #10 of 19 (5930 views)
Re: [cuboidgraphix] Help with Telnet commands. [In reply to] Can't Post

Is there any way to calculate the difference between these two times? Maybe a module?


Code
$ConnectTime= "17:25:30"; 
$DisconnectTime: "17:35:13";



KevinR
Veteran


Jan 22, 2009, 3:35 PM

Post #11 of 19 (5924 views)
Re: [cuboidgraphix] Help with Telnet commands. [In reply to] Can't Post

You could use basic math to convert the HH::MM::SS into seconds, subtract the connect time from the disconect time, and convert the difference between the two back to HH::MM::SS format if necessary again using basic math.
-------------------------------------------------


cuboidgraphix
User

Jan 22, 2009, 5:16 PM

Post #12 of 19 (5919 views)
Re: [KevinR] Help with Telnet commands. [In reply to] Can't Post

Thanks Kevin. LMAO ... yeah ... I thought about that but I it was wishful thinking that there might just be a module for that. :) Anyways.. I'll go ahead and do that.

Thanks again.


FishMonger
Veteran / Moderator

Jan 22, 2009, 5:25 PM

Post #13 of 19 (5917 views)
Re: [cuboidgraphix] Help with Telnet commands. [In reply to] Can't Post

use Date::Calc
http://search.cpan.org/~stbey/Date-Calc-5.4/Calc.pod


cuboidgraphix
User

Jan 22, 2009, 7:30 PM

Post #14 of 19 (5910 views)
Re: [FishMonger] Help with Telnet commands. [In reply to] Can't Post

Hi Fish.. I tried using that link you gave me but it just confused me.

Are you referring to Delta_DHMS() ?

I've been doing some google'ing and I found this link..

http://bumppo.net/lists/macperl/1999/04/msg00166.html

I see there's the POSIX::difftime. Can that be used? I tried using it but I get this error.


Code
Argument "17:35:13" isn't numeric in subroutine entry at ./parser.pl line 23, <$FH> line 10. 
Argument "17:25:30" isn't numeric in subroutine entry at ./parser.pl line 23, <$FH> line 10.



(This post was edited by cuboidgraphix on Jan 22, 2009, 7:38 PM)


KevinR
Veteran


Jan 22, 2009, 8:12 PM

Post #15 of 19 (5902 views)
Re: [cuboidgraphix] Help with Telnet commands. [In reply to] Can't Post


In Reply To
Thanks Kevin. LMAO ... yeah ... I thought about that but I it was wishful thinking that there might just be a module for that. :) Anyways.. I'll go ahead and do that.

Thanks again.


Using Date::Calc should be easier but sometimes its good to figure out how to do these things on your own.
-------------------------------------------------


cuboidgraphix
User

Jan 22, 2009, 9:00 PM

Post #16 of 19 (5898 views)
Re: [KevinR] Help with Telnet commands. [In reply to] Can't Post


In Reply To
Using Date::Calc should be easier but sometimes its good to figure out how to do these things on your own.


You're right Kevin. It just sucks when I try to learn new things and end up not using it. :(

I went with your first advice and did the basic math thingie and it worked. I just wanted to use a module, but it's hard learning how to use the module.

Anyways... here's my finished code. It works ... might not be the best script, but I did try to do good perl practice.


Code
#!/usr/bin/perl 
# This file is the parser.pl
# This is a script that will parse data from the Syslog

use strict;
use warnings;
use Text::ParseWords;

my $file = $ARGV[0] || die "Usage: $0 <filename>\n";
open my $FH, '<', $file or die "Can't open '$file' $!";

while (<$FH>) {
if($_ =~ /^20/){
my ($Date, $Time, $Local, $CID1, $CID2, $CID3, $CID4, $PA, $CT, $DT) = (quotewords('\s+', 0, $_))[0,1,2,12,13,14,15,24,36,43];
my ($CID, $DIFF, $CALL, $secs1, $secs2);
if($Local =~ /^Local7.Notice/){
$CID = "$CID1 $CID2 $CID3 $CID4";
$CT = substr("$CT",1,8);
$DT = substr("$DT",1,8);
$secs1 = (substr("$CT",0,2) * 360)+(substr("$CT",3,2) * 60)+substr("$CT",6,2);
$secs2 = (substr("$DT",0,2) * 360)+(substr("$DT",3,2) * 60)+substr("$DT",6,2);
$DIFF = $secs2 - $secs1;

print "Date: $Date \n";
print "Time: $Time \n";
print "ConnectionID: $CID \n";
print "PeerAddress: $PA \n";
print "ConnectTime: $CT \n";
print "DisconnectTime: $DT \n";
print "TotalTime: $DIFF \n";
print " \n\n";

$CALL = "$Date,$Time,$CID$PA$CT,$DT,$DIFF\n";

my $calllog = "calllog.txt" || die "Usage: $0 <filename>\n";
open my $CL, '>>', $calllog or die "Can't open '$calllog' $!";
print $CL $CALL;
close $CL;
}
if($Local =~ /^Local7.Error/){
my $errorlog = "errorlog.txt" || die "Usage: $0 <filename>\n";
open my $EL, '>>', $errorlog or die "Can't open '$errorlog' $!";
print $EL $_;
close $EL;
}
}
}
close $FH;



KevinR
Veteran


Jan 22, 2009, 9:18 PM

Post #17 of 19 (5896 views)
Re: [cuboidgraphix] Help with Telnet commands. [In reply to] Can't Post

Cool, well done. One potential problem is an interval that occurs between the hours 23 and 00. Something like:

my $ConnectTime = "23:55:00";
my $DisconnectTime = "00:05:00";

You're going to drive Fish crazy if you keep quoting single scalars. Lines like this:

$CT = substr("$CT",1,8);

Are much better written without the double-quotes:

$CT = substr($CT,1,8);

Only use quotes to make strings.

Edit:

I think you got this wrong:

$secs1 = (substr("$CT",0,2) * 360)

should be:

$secs1 = (substr("$CT",0,2) * 3600)

To convert hours into seconds you multiply by 3600 (60 * 60).
-------------------------------------------------


(This post was edited by KevinR on Jan 22, 2009, 9:21 PM)


KevinR
Veteran


Jan 22, 2009, 10:01 PM

Post #18 of 19 (5891 views)
Re: [cuboidgraphix] Help with Telnet commands. [In reply to] Can't Post

Something along these lines might be helpful:


Code
my $ConnectTime = "23:55:42";  
my $DisconnectTime = "00:07:29";
my $diff = ConvertToSeconds($ConnectTime,$DisconnectTime);
print $diff;

sub ConvertToSeconds {
my ($start,$end) = @_;
my ($sh,$sm,$ss) = split(/:/,$start);
my ($eh,$em,$es) = split(/:/,$end);
$eh = $eh == 0 ? 24 : $eh;
$start = ($sh * 3600) + ($sm * 60) + $ss;
$end = ($eh * 3600) + ($em * 60) + $es;
my $diff = $end - $start;
my ($s,$m,$h) = (gmtime($diff))[0,1,2];
return (sprintf "%02d:%02d:%02d", $h,$m,$s);
}


Assumes you are only ever going to need to compute the difference in HH:MM:SS and never in DD:HH:MM:SS or higher.
-------------------------------------------------


FishMonger
Veteran / Moderator

Jan 23, 2009, 5:41 AM

Post #19 of 19 (5881 views)
Re: [cuboidgraphix] Help with Telnet commands. [In reply to] Can't Post

Q. When would the var assignment in this statement fail?
my $calllog = "calllog.txt" || die "Usage: $0 <filename>\n";

A. Never

Q. How should that statement be written?

A. I'll leave that to the reader to determine.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives