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

Discussion in 'Perl Misc' started by Facco Eloelo, Aug 13, 2004.

  1. Facco Eloelo

    Facco Eloelo Guest

    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###############################
     
    Facco Eloelo, Aug 13, 2004
    #1
    1. Advertising

  2. Facco Eloelo

    thundergnat Guest

    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";
    }
    }
     
    thundergnat, Aug 13, 2004
    #2
    1. Advertising

  3. Facco Eloelo

    Anno Siegel Guest

    Facco Eloelo <> 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
     
    Anno Siegel, Aug 13, 2004
    #3
  4. Facco Eloelo

    Jim Keenan Guest

    Facco Eloelo <> wrote in message news:<>...
    > 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
     
    Jim Keenan, Aug 13, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jack
    Replies:
    9
    Views:
    2,739
  2. Ken Kafieh
    Replies:
    1
    Views:
    398
  3. WuyaSea Operator
    Replies:
    48
    Views:
    1,528
    Ben Phillips
    Sep 24, 2007
  4. Lew
    Replies:
    0
    Views:
    429
  5. Damjan Rems
    Replies:
    5
    Views:
    133
    Waldemar Dick
    Apr 23, 2010
Loading...

Share This Page