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

F

Facco Eloelo

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.2928)
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.2228)
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.2028)
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###############################
 
T

thundergnat

Facco said:
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";
}
}
 
A

Anno Siegel

Facco Eloelo said:
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
 
J

Jim Keenan

Facco Eloelo said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top