CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Telnet.pm error

 



Drucal
Novice

Aug 12, 2008, 2:10 PM

Post #1 of 7 (2390 views)
Telnet.pm error Can't Post

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 (differenct 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.


travs69
Novice

Aug 12, 2008, 8:54 PM

Post #2 of 7 (2386 views)
Re: [Drucal] Telnet.pm error [In reply to] Can't Post

Suggestions:

Use the login method instead of the open/waitfor. I believe you should have another watifor (using your method) before you start running the cmd's if you don't want to use the login method, but I'd just change to the login method.

What are you seeing in the dump_log/input_log? Do any of the routers have weird prompts?


Drucal
Novice

Aug 13, 2008, 7:53 AM

Post #3 of 7 (2374 views)
Re: [travs69] Telnet.pm error [In reply to] Can't Post

Dumplog:

< 0x00000: ff fb 03 ff fb 01 ff fb 00 ff fd 18 ff fd 1f 75 .....u
< 0x00010: 73 65 72 6e 61 6d 65 3a 20 sername:

> 0x00000: ff fd 03 ff fd 01 ff fe 00 ff fc 18 ff fc 1f .....

> (this line removed for security purposes.) This is just the Username line.

< 0x00000: ff fb 03 ff fb 01 ..

< (this line removed for security purposes.) This is just the Username line.

< 0x00000: 70 61 73 73 77 6f 72 64 3a 20 password:

> (this line removed for security purposes.) This is the password..

> 0x00000: 76 6c 61 6e 20 73 68 6f 77 0d 0a vlan show..

< 0x00000: 0d 0a 0d 0a 0d 0a 57 65 6c 63 6f 6d 65 20 74 6f ......Welcome to
< 0x00010: 20 42 61 67 20 45 6e 64 20 28 63 29 20 32 30 30 Bag End (c) 200
< 0x00020: 33 20 57 6f 72 6c 64 20 57 69 64 65 20 50 61 63 3 World Wide Pac
< 0x00030: 6b 65 74 73 0d 0a 0d 0a 42 69 6c 62 6f 3e kets....Bilbo>

< 0x00000: 76 6c 61 6e 20 73 68 6f 77 0d 0a vlan show..

< 0x00000: 20 4e 61 6d 65 20 20 20 20 20 20 20 20 20 20 20 Name
< 0x00010: 20 20 20 20 7c 20 56 4c 41 4e 20 49 44 20 7c 20 | VLAN ID |
< 0x00020: 20 50 6f 72 74 20 4d 61 70 20 20 7c 0d 0a 2d 2d Port Map |..--
< 0x00030: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
< 0x00040: 2d 2d 7c 2d 2d 2d 2d 2d 2d 2d 2d 2d 7c 2d 30 31 --|---------|-01
< 0x00050: 32 33 34 35 36 37 38 47 2d 7c 0d 0a 2a 44 45 46 2345678G-|..*DEF
< 0x00060: 41 55 4c 54 2d 56 4c 41 4e 2a 20 20 20 20 20 20 AULT-VLAN*
< 0x00070: 7c 20 20 20 20 20 20 20 31 20 7c 20 55 55 55 55 | 1 | UUUU
< 0x00080: 55 55 55 2d 2d 54 20 7c 0d 0a 56 4c 41 4e 2d 31 UUU--T |..VLAN-1
< 0x00090: 32 37 20 20 20 20 20 20 20 20 20 20 20 20 7c 20 27 |
< 0x000a0: 20 20 20 20 31 32 37 20 7c 20 2d 2d 2d 2d 2d 2d 127 | ------
< 0x000b0: 2d 2d 2d 54 20 7c 0d 0a 56 4c 41 4e 2d 31 32 38 ---T |..VLAN-128
< 0x000c0: 20 20 20 20 20 20 20 20 20 20 20 20 7c 20 20 20 |
< 0x000d0: 20 20 31 32 38 20 7c 20 2d 2d 2d 2d 2d 2d 2d 55 128 | -------U
< 0x000e0: 2d 2d 20 7c 20 3c 69 66 49 6e 64 65 78 20 32 3e -- | <ifIndex 2>
< 0x000f0: 0d 0a 2a 56 6f 49 50 20 56 4c 41 4e 2a 20 20 20 ..*VoIP VLAN*
< 0x00100: 20 20 20 20 20 20 7c 20 20 20 20 20 35 30 30 20 | 500
< 0x00110: 7c 20 2d 2d 2d 2d 2d 2d 2d 2d 55 54 20 7c 0d 0a | --------UT |..
< 0x00120: 56 4c 41 4e 2d 31 32 36 20 20 20 20 20 20 20 20 VLAN-126
< 0x00130: 20 20 20 20 7c 20 20 20 20 20 31 32 36 20 7c 20 | 126 |
< 0x00140: 2d 2d 2d 2d 2d 2d 2d 2d 2d 54 20 7c 20 3c 69 66 ---------T | <if
< 0x00150: 49 6e 64 65 78 20 31 3e 0d 0a 42 69 6c 62 6f 3e Index 1>..Bilbo>

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

Bilbo> is the prompt. I was reading in another post that it may be hanging on the prompt. I'm going to try defining the prompt in the telnet object. I'll post the input log and output log in the next post.


Also, the prompt is different on the other switches that it works perfectly on.


(This post was edited by Drucal on Aug 13, 2008, 7:58 AM)


Drucal
Novice

Aug 13, 2008, 7:56 AM

Post #4 of 7 (2373 views)
Re: [travs69] Telnet.pm error [In reply to] Can't Post

Input.log:

username: myusername
password:


Welcome to Bag End (c) 2003 World Wide Packets

Bilbo>vlan show
Name | VLAN ID | Port Map |
--------------------|---------|-012345678G-|
*DEFAULT-VLAN* | 1 | UUUUUUU--T |
VLAN-127 | 127 | ---------T |
VLAN-128 | 128 | -------U-- | <ifIndex 2>
*VoIP VLAN* | 500 | --------UT |
VLAN-126 | 126 | ---------T | <ifIndex 1>
Bilbo>

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

Output.log:

myusername
mypassword
vlan show


Drucal
Novice

Aug 13, 2008, 9:08 AM

Post #5 of 7 (2368 views)
Re: [Drucal] Telnet.pm error [In reply to] Can't Post

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',
);


travs69
Novice

Aug 13, 2008, 2:41 PM

Post #6 of 7 (2363 views)
Re: [Drucal] Telnet.pm error [In reply to] Can't Post

That's weird as the default prompt is
/[\$%#>] $/
and that should have caught that Bilbo>


Drucal
Novice

Aug 13, 2008, 2:47 PM

Post #7 of 7 (2361 views)
Re: [travs69] Telnet.pm error [In reply to] Can't Post

Perhaps the space after the >] is the problem. In the switch, there is no space after Bilbo>. The cursor is right next to the >.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives