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

I'm extremely new to Perl, and not much of a programmer overall. I'm trying to write a Perl script that logs into a switch enters a command and prints back the results. The code below will work fine on some switches, but on others (different OS versions) it hangs. Here is the code I have:
#################################################
#!/c:\perl use warnings; use strict; use Net::Telnet; my $telnet = new Net::Telnet ( Timeout=>10, Errmode=>'die', Dump_Log=>'dump.txt', Input_log=>'input.txt', Output_log=>'output.txt', ); $telnet->open('172.16.233.214'); $telnet->waitfor('/username: $/i'); $telnet->print('myusername'); $telnet->waitfor('/password: $/i'); $telnet->print('mypassword'); my @lines; @lines = $telnet->cmd("vlan show"); #<----This is where it will hang. print @lines; @lines = $telnet->cmd("vlan"); print @lines;
#################################################
I've tried the debugger, and this is where it appears to hang.
DB<2> s command timed-out at newtelnet.pl line 25 at C:/Perl/lib/Net/Telnet.pm line 2036 Net::Telnet::_croak('Net::Telnet=GLOB(0x1f0e89c)', 'command timed-out' +) called at C:/Perl/lib/Net/Telnet.pm line 539 Net::Telnet::error('Net::Telnet=GLOB(0x1f0e89c)', 'command timed-out') + c alled at C:/Perl/lib/Net/Telnet.pm line 361 Net::Telnet::cmd('Net::Telnet=GLOB(0x1f0e89c)', 'vlan show') called at + n ewtelnet.pl line 25 Net::Telnet::DESTROY(C:/Perl/lib/Net/Telnet.pm:201): 201: sub DESTROY { DB<2> s IO::Handle::DESTROY(C:/Perl/lib/IO/Handle.pm:330): 330: sub DESTROY {} DB<2> s IO::Handle::DESTROY(C:/Perl/lib/IO/Handle.pm:330): 330: sub DESTROY {} DB<2> s IO::Handle::DESTROY(C:/Perl/lib/IO/Handle.pm:330): 330: sub DESTROY {} DB<2> s Debugged program terminated. Use q to quit or R to restart, use o inhibit_exit to avoid stopping after program termination, h q, h R or h o to get additional info.
############################################
This script works just fine for most of my switches, but on others, it hangs.

Replies are listed 'Best First'.
Re: Telnet.pm error
by spivey49 (Monk) on Aug 12, 2008 at 22:22 UTC

    It looks like you might be running against Cisco switches. Try Net::Telnet::Cisco if that's the case. If it hangs at the sh vlan command it could be either a problem with paging or the prompt isn't set correctly

Re: Telnet.pm error
by Bloodnok (Vicar) on Aug 13, 2008 at 10:50 UTC
    Just a guess but, I recently encountered a situation whereby I had a script login to a number of switches using Net::Telnet - most of which worked, the rest didn't so much hang, as wait for an awfully long timeout before the script 'came back' - the problem was that the prompt was subtly different on some of the switches.

    HTH ,

    A user level that continues to overstate my experience :-))
      Ok, this one is solved. Thanks for the help. It definitely was hanging on the prompt.

      The following is from the perldoc telnet document.

      * The methods "login()" and "cmd()" use the *prompt* setting in the object to determine when a login or remote command is complete. Those methods will fail with a time-out if you don't set the prompt correctly.

      ########################################################

      Here's the change I made to my code. I added the Prompt=>'/Bilbo>$/i', line.

      my $telnet = new Net::Telnet ( Timeout=>10, Prompt=>'/Bilbo>$/i', Errmode=>'die', Dump_Log=>'dump.txt', Input_log=>'input.txt', Output_log=>'output.txt', );