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

I wrote a simple script to interface with some Cisco routers on our network (3745's mainly) and have run into some issues using Net::Telnet's function "waitfor"

#!/usr/bin/perl -w use strict; use Net::Telnet; die "Required argument missing!\n" unless ($ARGV[0]); my $target = $ARGV[0]; my $password = "password"; my $telnet = new Net::Telnet (Timeout => 60, Errmode => 'die', Telnetmode => '1', Input_log => "$target.txt", Dump_log => "dump_log.txt"); $telnet->open(Host => $target, Port => '23') || die "Cannot open telnet connection to $ +target"; $telnet->print($password) if ($telnet->waitfor('/Password:/')); $telnet->print('ena') if ($telnet->waitfor('/\>/')); $telnet->print($password) if ($telnet->waitfor('/Password:/')); $telnet->print('sh run') if ($telnet->waitfor('/#/')); while (1) { if ($telnet->waitfor('/--More--/')) { $telnet->print(chr(32)); }else{ print "Configuration file for $target has been successfully save +d!\n"; exit(0) } }
Now as you can see inside the while loop I look for the prompt of "--More--" which is what all cisco gear returns when the listed query is longer than will fit on one page. I have this working properly and it loops through the entire "sh run" command but never exits because it keeps waiting for "--More--".

The nature of the "waitfor()" command will wait as long as you have set the "timeout" argument for for the desired prompt. This works fine as long as the program recieves this prompt. However after the entire configuration for the router has been listed it goes back to the router shell prompt.

This input recieved from the router does not match the target text in the waitfor() function so the script waits like it is suppose to. I need a way to do what I have tried here, to exit if the waitfor() does not recieve the desired text (meaning the entire con fig has been listed).

Any ideas?

Oh and I have looked into using the Net::Telnet::Cisco module too but it appears that this module would give me a similar problem.

Replies are listed 'Best First'.
Re: Net::Telnet issues!
by NetWallah (Canon) on Mar 20, 2004 at 00:43 UTC
    THe trick is to avoid the "--More--" by setting the terminal lines to 0, or a very high number. Sorry - No time to lookup the docs to give more specific info.

    Offense, like beauty, is in the eye of the beholder, and a fantasy.
    By guaranteeing freedom of expression, the First Amendment also guarntees offense.

      right -

      set pager lines 0

      at the start of the script

      parse/save streaming output from commands

      before exiting set pager lines 24 or whatever it was

      telnet? SSH!
Re: Net::Telnet issues!
by Anonymous Monk on Mar 20, 2004 at 02:47 UTC

    Net::Telnet::Cisco will work better in the end but what you're looking for is terminal length 0

Re: Net::Telnet issues!
by eXile (Priest) on Mar 20, 2004 at 01:14 UTC
    May be Expect.pm is what you need. See this post: Using Expect.pm to Manage an Unreliable Program.
    I've never used it myself, but in the docs I read that you can make it wait for multiple output-patterns at the same time like
    $exp->expect($timeout, [ 'It worked', sub { $spawn_ok = "OK"; exp_continue; } ], [ '-- More --', sub { do_more(); exp_continue; } ], [ timeout => sub { print "Process timed out.\n"; } ] );
Re: Net::Telnet issues!
by Anonymous Monk on Mar 20, 2004 at 23:51 UTC
    What nobody seems to have addressed is why does that script never exit if it doesn't see '--More--'? Shouldn't the timeout passed in the Net::Telnet constructor take care of things?