http://www.perlmonks.org?node_id=1005533

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

Hello Monks, Should be a simple answer for anyone with more than the week's worth of knowledge I possess. So I'm trying to create a small script to have users telnet into their selected devices. The problem I'm running into is getting the device entered to work in Net::Telnet.
#!/usr/bin/perl use strict; use warnings; use Net::Telnet; print "Enter your device: "; my $device = <STDIN>; my $telnet = new Net::Telnet (Timeout=>15 ,Input_log => "received_data.txt" ); $telnet->open($device); $telnet->waitfor('/username: /i'); $telnet->print('xxxx'); $telnet->waitfor('/password: /i'); $telnet->print('xxxx'); $telnet->waitfor('/$device"."# /i'); ##wait for prompt $telnet->print ('show run'); $telnet->print('exit'); my $out = $telnet->waitfor('/$device"."# /i'); print $out; $telnet->close(); my $fname = "received_data.txt"; open (my $fh, "<", $fname) or die "Can't open"; while (<$fh>) { print if /ip prefix-list/; }
This is the error I'm getting: bad match operator: Global symbol "$device" requires explicit package name at telnet3.pl line 23 . I'm just having trouble figuring out why $device is coming up not defined and if their is a way to get this to work.
Uncaught exception from user code: bad match operator: Global symbol "$device" requires explicit package name <STDIN> line 7. at scopedzT1 line 279 at /sw/lib/perl5/site_perl/5.8.8/Net/Telnet.pm line 2036 Net::Telnet::_croak('Net::Telnet=GLOB(0x372ae8)', 'bad match operator: Global symbol "$device" requires explicit...') called at /sw/lib/perl5/site_perl/5.8.8/Net/Telnet.pm line 539 Net::Telnet::error('Net::Telnet=GLOB(0x372ae8)', 'bad match operator: Global symbol "$device" requires explicit...') called at /sw/lib/perl5/site_perl/5.8.8/Net/Telnet.pm line 2478 Net::Telnet::_match_check('Net::Telnet=GLOB(0x372ae8)', '/$device "."# /i') called at /sw/lib/perl5/site_perl/5.8.8/Net/Telnet.pm line 2002 Net::Telnet::waitfor('Net::Telnet=GLOB(0x372ae8)', '/$device "."# /i') called at scopedzT1 line 279

Replies are listed 'Best First'.
Re: Problem with Net::Telnet
by frozenwithjoy (Priest) on Nov 25, 2012 at 23:03 UTC
    There may be other problems, but the use of single quotes prevents $device from being interpolated in:
    $telnet->waitfor('/$device"."# /i'); ##wait for prompt
    Does using the following instead help get rid of errors?
    $telnet->waitfor( qq{/$device"."# /i} ); ##wait for prompt
      That definitely helped. At least I know it was a syntax issue. Now my only problem is I'm getting a 'pattern match timed-out' error.
      Uncaught exception from user code: pattern match timed-out at scopedzT1 line 279 at /sw/lib/perl5/site_perl/5.8.8/Net/Telnet.pm line 2036 Net::Telnet::_croak('Net::Telnet=GLOB(0x3a684c)', 'pattern mat +ch timed-out') called at /sw/lib/perl5/site_perl/5.8.8/Net/Telnet.pm +line 539 Net::Telnet::error('Net::Telnet=GLOB(0x3a684c)', 'pattern matc +h timed-out') called at /sw/lib/perl5/site_perl/5.8.8/Net/Telnet.pm l +ine 1995 Net::Telnet::waitfor('Net::Telnet=GLOB(0x3a684c)', '/yyyy# /i' +) called at scopedzT1 line 279

      I'm guessing it's just a pattern match issue with the prompt. I'm actually getting into the device. At least that is what the output file is showing me. Is there any way around the pattern match error? The output file is actually showing the correct prompt. Unsure why it's failing. Thanks for your help.
        Can you determine at which pattern match it is timing out based on the line # or printing debug statements to STDOUT between telnet calls?
Re: Problem with Net::Telnet
by jrogers (Initiate) on Nov 26, 2012 at 14:29 UTC

    You can use the flexibility of a regexp to match the expected prompt without specifying the exact string. That way you can avoid the $device interpolation. It also would be better if you could anchor the prompt as the last thing read using '$'. You should turn off paging so you don't have to deal with a "more" paging prompt.

    $telnet->prompt('/\w+# $/'); $telnet->waitfor($telnet->prompt); $telnet->cmd("terminal length 0"); @lines = $telnet->cmd("show run"); while (@lines) { print if /ip prefix-list/; }
      That worked. Thanks to all for your help. Very much appreciated.