Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > newbie question-----where is wrong.(only 19 lines code)

Reply
Thread Tools

newbie question-----where is wrong.(only 19 lines code)

 
 
Facco Eloelo
Guest
Posts: n/a
 
      08-13-2004
I want to get the "inputrate" and the "outputrate" from a router's log(using
show command).and the outputfile looks like:
1,1000,0
2,3000,2000
....

It should be very easy.But the code doesn't work.can anybody help me?thanks in
advance.


#########code begin##################
#!/usr/bin/perl
$infile='d:\routertest.log';
$outfile='d:\output';
open(IN, "< $infile") or die "Couldn't open $infile for reading: $!";
open(OUT, "> $outfile") or die "Couldn't open $outfile for reading: $!";
@line=<IN>;
$num=@line;
$count=1;
for($i=0;$i<$num;$i++)
{

if(chomp($line[$i]) eq "r1-b-sdnn>show int fa1/1/0")
{
@inputrate=split(/''/,$line[$i+14]);
@outrate=split(/''/,$line[$i+1]);
print OUT "$count,$inputrate[4],$outputrate[4]";
$count++;
}
}
##############code end##########################



################d:\routertest.log##############
r1-b-sdnn>show int fa1/1/0
FastEthernet1/1/0 is up, line protocol is up
Hardware is cyBus FastEthernet Interface, address is 22211.d126.2898 (bia
0072.7176.292
Description: to-580-p
Internet address is 61.179.255.202/30
MTU 1500 bytes, BW 100000 Kbit, DLY 100 usec, rely 255/255, load 1/255
Encapsulation ARPA, loopback not set
Keepalive set (10 sec)
Full-duplex, 100Mb/s, 100BaseTX/FX
ARP type: ARPA, ARP Timeout 04:00:00
Last input 00:00:00, output 00:00:00, output hang never
Last clearing of "show interface" counters never
Queueing strategy: fifo
Output queue 0/40, 0 drops; input queue 1/75, 0 drops, 54 flushes
5 minute input rate 1000 bits/sec, 1 packets/sec
5 minute output rate 0 bits/sec, 0 packets/sec
31321654 packets input, 3593026896 bytes, 0 no buffer
Received 13476682 broadcasts, 1 runts, 0 giants, 0 throttles
31 input errors, 0 CRC, 0 frame, 0 overrun, 31 ignored
0 watchdog, 0 multicast
0 input packets with dribble condition detected
25753387 packets output, 3750041951 bytes, 0 underruns
0 output errors, 0 collisions, 6 interface resets
0 babbles, 0 late collision, 0 deferred
10 lost carrier, 0 no carrier
0 output buffer failures, 0 output buffers swapped out
r1-b-sdnn>show int fa1/1/0
FastEthernet1/1/0 is up, line protocol is up
Hardware is cyBus FastEthernet Interface, address is 0902.7d16.8828 (bia
0102.7616.222
Description: to-580-p
Internet address is 35.179.255.202/30
MTU 1500 bytes, BW 100000 Kbit, DLY 100 usec, rely 255/255, load 1/255
Encapsulation ARPA, loopback not set
Keepalive set (10 sec)
Full-duplex, 100Mb/s, 100BaseTX/FX
ARP type: ARPA, ARP Timeout 04:00:00
Last input 00:00:00, output 00:00:00, output hang never
Last clearing of "show interface" counters never
Queueing strategy: fifo
Output queue 0/40, 0 drops; input queue 2/75, 0 drops, 54 flushes
5 minute input rate 3000 bits/sec, 3 packets/sec
5 minute output rate 2000 bits/sec, 3 packets/sec
31321698 packets input, 3593036917 bytes, 0 no buffer
Received 13476696 broadcasts, 1 runts, 0 giants, 0 throttles
31 input errors, 0 CRC, 0 frame, 0 overrun, 31 ignored
0 watchdog, 0 multicast
0 input packets with dribble condition detected
25753446 packets output, 3750058041 bytes, 0 underruns
0 output errors, 0 collisions, 6 interface resets
0 babbles, 0 late collision, 0 deferred
10 lost carrier, 0 no carrier
0 output buffer failures, 0 output buffers swapped out
r1-b-sdnn>show int fa1/1/0
FastEthernet1/1/0 is up, line protocol is up
Hardware is cyBus FastEthernet Interface, address is 0002.7016.2028 (bia
1002.0916.202
Description: to-580-p
Internet address is 78.179.255.202/30
MTU 1500 bytes, BW 100000 Kbit, DLY 100 usec, rely 255/255, load 1/255
Encapsulation ARPA, loopback not set
Keepalive set (10 sec)
Full-duplex, 100Mb/s, 100BaseTX/FX
ARP type: ARPA, ARP Timeout 04:00:00
Last input 00:00:00, output 00:00:00, output hang never
Last clearing of "show interface" counters never
Queueing strategy: fifo
Output queue 0/40, 0 drops; input queue 3/75, 0 drops, 54 flushes
5 minute input rate 3000 bits/sec, 5 packets/sec
5 minute output rate 3000 bits/sec, 5 packets/sec
31321765 packets input, 3593045770 bytes, 0 no buffer
Received 13476706 broadcasts, 1 runts, 0 giants, 0 throttles
31 input errors, 0 CRC, 0 frame, 0 overrun, 31 ignored
0 watchdog, 0 multicast
0 input packets with dribble condition detected
25753528 packets output, 3750073394 bytes, 0 underruns
0 output errors, 0 collisions, 6 interface resets
0 babbles, 0 late collision, 0 deferred
10 lost carrier, 0 no carrier
0 output buffer failures, 0 output buffers swapped out
#####################code end###############################
 
Reply With Quote
 
 
 
 
thundergnat
Guest
Posts: n/a
 
      08-13-2004
Facco Eloelo wrote:
> I want to get the "inputrate" and the "outputrate" from a router's log(using
> show command).and the outputfile looks like:
> 1,1000,0
> 2,3000,2000
> ...
>
> It should be very easy.But the code doesn't work.can anybody help me?thanks in
> advance.
>



How bout somthing like:


#!/usr/bin/perl

use Warnings;
use Strict;

my $infile='d:\routertest.log';
my $outfile='d:\output';
my $count;

open(my $IN, "< $infile") or die "Couldn't open $infile for reading: $!";
open(my $OUT, "> $outfile") or die "Couldn't open $outfile for reading: $!";
while (<$IN>) {
if ( m/input rate (\d+)/ ){
$count++;
print $OUT $count,',',$1,',';
}
if ( m/output rate (\d+)/ ){
print $OUT "$1\n";
}
}
 
Reply With Quote
 
 
 
 
Anno Siegel
Guest
Posts: n/a
 
      08-13-2004
Facco Eloelo <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> I want to get the "inputrate" and the "outputrate" from a router's log(using
> show command).and the outputfile looks like:
> 1,1000,0
> 2,3000,2000
> ...
>
> It should be very easy.But the code doesn't work.can anybody help me?thanks in
> advance.


"Doesn't work", eh? So what does it do? Be specific.

> #########code begin##################
> #!/usr/bin/perl


No warnings, no strict.

> $infile='d:\routertest.log';
> $outfile='d:\output';
> open(IN, "< $infile") or die "Couldn't open $infile for reading: $!";
> open(OUT, "> $outfile") or die "Couldn't open $outfile for reading: $!";

^^^^^^^
You are trying to overwrite the file, not to read it. Don't thoughtlessly
copy error messages from one place to another, that's misleading.

> @line=<IN>;
> $num=@line;
> $count=1;
> for($i=0;$i<$num;$i++)
> {
>
> if(chomp($line[$i]) eq "r1-b-sdnn>show int fa1/1/0")


The return value of chomp() is not the chomped string. See "perldoc
-f chomp". The condition will never match.

> {
> @inputrate=split(/''/,$line[$i+14]);


Oh, come on. The line with the input rate is 15 lines below the "show"
line, not 14. Are you asking us to do your line-counting for you?

> @outrate=split(/''/,$line[$i+1]);


....and the line with the output rate is 16 lines down. Why are you looking
for it in the next line?

Also, you are splitting on a pattern of two consecutive single quotes.
As far as I can see, that pattern doesn't appear at all in your data.
You want to split on white space or blanks.

> print OUT "$count,$inputrate[4],$outputrate[4]";


Another counting error. The numbers appear at offset 5 (not 4), because
the lines start with blanks.

> $count++;
> }
> }
> ##############code end##########################


[data snipped]

Your code has so many trivial errors, it can hardly count as a serious
attempt to solve the problem.

my $count = 0;
my ( $inrate, $outrate);
while ( <IN> ) {
chomp;
$count ++ if $_ eq 'r1-b-sdnn>show int fa1/1/0';
$inrate = $1 if /input rate (\d+)/;
if ( /output rate (\d+)/ ) {
$outrate = $1;
print OUT "$count, $inrate, $outrate\n";
}
}

Anno
 
Reply With Quote
 
Jim Keenan
Guest
Posts: n/a
 
      08-13-2004
Facco Eloelo <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> I want to get the "inputrate" and the "outputrate" from a router's log(using
> show command).and the outputfile looks like:
> 1,1000,0
> 2,3000,2000
> ...
>
> It should be very easy.But the code doesn't work.can anybody help me?thanks in
> advance.
>
>
> #########code begin##################
> #!/usr/bin/perl
> $infile='d:\routertest.log';
> $outfile='d:\output';
> open(IN, "< $infile") or die "Couldn't open $infile for reading: $!";
> open(OUT, "> $outfile") or die "Couldn't open $outfile for reading: $!";
> @line=<IN>;
> $num=@line;
> $count=1;
> for($i=0;$i<$num;$i++)
> {
>
> if(chomp($line[$i]) eq "r1-b-sdnn>show int fa1/1/0")
> {
> @inputrate=split(/''/,$line[$i+14]);
> @outrate=split(/''/,$line[$i+1]);
> print OUT "$count,$inputrate[4],$outputrate[4]";
> $count++;
> }
> }


There's a lot wrong with it. First, as another poster implied, you've
got misspellings in your variables that you would have caught had you
started out your program with:

use strict;
use warnings;

Second, AFAICT in the line beginning

if(chomp ...

the condition never returns true; hence the body of the loop is never
executed.

Third, you're splitting on an empty string; my hunch is that you
really wanted to split on a wordspace.

Fourth, for readable output you have to conclude each 'print'
statement with a newline.

Fifth, the line that comes after $i+14 should be $i+15, not $i+1.

You can take it from there.

jimk
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to know two lines are a pare parallel lines lovecreatesbeauty C Programming 11 04-28-2006 03:32 AM
How to display a string in many lines, each lines have a specified length thuyptt@dsp.com.vn C++ 1 12-06-2005 07:26 AM
Asp.Net Calender, how to display 5 lines if there are only 5 lines in one month? Jack ASP .Net 9 10-12-2005 03:44 AM
Modems, Analog Lines and ... Electrical Lines? Sens Fan Happy In Ohio Computer Support 5 09-02-2004 04:15 AM
Re: how to read 10 lines from a 200 lines file and write to a new file?? Joe Wright C Programming 0 07-27-2003 08:50 PM



Advertisments