DesolateCoder has asked for the wisdom of the Perl Monks concerning the following question:

#!/usr/bin/perl -w use Net::Telnet; use strict; my ($connect,@lines,$lineid,$identifier); $connect = new Net::Telnet (Timeout => 10, Input_log => 'telnet.log',H +ost => 'myhost', Prompt => '/[#>].*$/'); $connect->waitfor('/password[: ]*$/i'); $connect->print("mypass"); $connect->cmd("term len 0"); $connect->getline; $connect->cmd(String => 'show ip bgp summary',Timeout => '10'); $connect->getline; $identifier = $connect->getline; $lineid = 0; while ($lineid != 11) { $connect->getline; $lineid++; } @lines = $connect->getlines(Timeout => '30'); my ($key,$neighbor,$version,$as,$msgrcvd,$msgsent,$tblver,$inq,$outq,$ +uptime,$state,%neighbors,$hold); for $key (@lines) { ($neighbor,$version,$as,$msgrcvd,$msgsent,$tblver,$inq,$outq,$ +uptime,$state) = split(/\s+/,$key); $neighbors{$neighbor} = [$as,$msgrcvd,$msgsent,$tblver,$inq,$o +utq,$uptime,$state]; } print "$identifier\n"; for $hold (keys %neighbors) { print "$hold: @{$neighbors{$hold}}\n"; } sleep('5'); $connect->close; # # # Snippet $lineid = 0; while ($lineid != '20') { $linedata = $connect->getline; push(@data,$linedata); next unless eof; $lineid++; }
Hmm....on the @lines = $connect->getlines part....i'm only getting the first 4-5 lines....the second snippet of code fixes it...but i don't know how to break the loop looking for eof or timeout...doc makes mention...but hey what can i say...my signature says it all :>)
my $experience = "nonexistent"; my $thoughts = "worthless"; unless ($experience) { print "You're $thoughts\n"; }

Replies are listed 'Best First'.
Re: Net::Telnet disappearing data :)
by PrakashK (Pilgrim) on Jul 24, 2001 at 00:34 UTC
    When you check for end of file, you are not using the object method ($connect->eof). Instead, the perl builtin eof is being used. Although, it might be working in your case ("eof" without an argument uses the last file read), it would be more correct and safer to use the object method.

    ...the second snippet of code fixes it...but i don't know how to break the loop looking for eof or timeout...

    You can use the timed_out method to test for timeout.

    my $lineid = 0; while ($lineid < 20) { my $linedata = $connect->getline; # check for eof or time-out _before_ using the data last if $connect->eof or $connect->timed_out; # now save the line push(@data,$linedata); $lineid++; }
      Thank-you much....this works :) Also a problem with original method is my Prompt=> /regex/ was hosed....so maybe i can use the array up front...have to play.
      my $experience = "nonexistent"; my $thoughts = "worthless"; unless ($experience) { print "You're $thoughts\n"; }
Re: Net::Telnet disappearing data :) (Net::Telnet::Cisco worth checking into)
by ybiC (Prior) on Jul 24, 2001 at 03:06 UTC
    It looks like PrakashK's advice got you going.   8^)

    It also looks like you're working with Cisco routers.   If that's the case, I'd encourage you to look into Net::Telnet::Cisco.   It streamlines prompts and other Cisoey stuff, and might save you some grief.

    Examples of how I've had good luck with that module can be found near the tail of my homenode.   Good luck!
        cheers,
        Don
        striving toward Perl Adept
        (it's pronounced "why-bick")