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

I'm trying to acquire cache flow data from a Cisco 6509 router with the following command via a telnet session:
@data = $tnet->cmd('show ip cache flow');
This works fine on the inner routers within the network, since there is a manageable amount of data. On the routers that border the network, this command times out since there is too much data to process. I'm trying to find a way to run this command for a certain time period. For example:
until (5 seconds pass) { @data = $tnet->cmd('show ip cache flow'); }
I've been unsucessful in implementing this with the sleep command, probably due to my inexperience with Perl. Any help or suggestions are appreciated.

Replies are listed 'Best First'.
Re: How to break while reading data from a Cisco router?
by bruceb3 (Pilgrim) on Sep 16, 2007 at 04:01 UTC
    Check the documentation for the alarm function in perldoc.

    Here is some example code for your situation;

    my $timeout = 5; eval { local $SIG{ALRM} = sub { die "alarm\n" }; # NB: \n required alarm $timeout; @data = $tnet->cmd('show ip cache flow'); alarm 0; }; if ($@) { die unless $@ eq "alarm\n"; # propagate unexpected errors # timed out } else { # didn't }
Re: How to break while reading data from a Cisco router?
by laceytech (Pilgrim) on Sep 16, 2007 at 04:44 UTC
    Set the timeout for telnet session to a longer period of time.

    See the documentation "timeout" and "timed_out" in Net::Telnet

      I tried using the timeout option early on. The problem is the amount of cache flow data. I could set the timeout for 10 minutes there would still be plenty more data to grab. I think the alarm function will work for me though. Thanks for the suggestions.