Hi Monks,

I have a problem with Net::Telnet module. I use it to login to a device via TCP/IP and get some information from the device. Depending on the connection the prompt at the device side is IP1x with x between 0-7.

The problem is that on sending a command the script behaves inconsistent. Sometimes I direclty receive the replied message from the device and sometimes I have to give another enter to receive the message.

I am still in test-mode, but would like to automate the script and am not sure how to deal with the above. Attached is the script and the debug output of the telnet module. The difference between bouth debug outputs is that in the second case the enter command is there > 0x00000: 0a                                                  .

What am I doing wrong that causes this inconsistent behaviour?

#!/usr/bin/perl -w # # test # ------ # Test connection to equipment via TCP/IP use vars qw( $VERSION ); use Getopt::Long; use File::Basename; use Net::Telnet; use strict; use warnings; local $|=1; $VERSION = 20110214; # Process options and input arguments Getopt::Long::Configure( "prefix_pattern=(-|\/)" ); my %Config; my $Result = GetOptions( \%Config, qw( ip|i=s port|p=s help|?|h ) ); $Config{help} = 1 if( ! $Result ); if ( $Config{help} || not exists($Config{ip}) ) { Syntax(); exit(); } my $hostname; my $hostport; $hostname=$Config{ip}; $hostport=$Config{port}; # Connect to receiver my $file1="debug.out"; my $file2="input.out"; my $Rtelnet = new Net::Telnet ( Timeout=>1, Errmode=>'die', Prompt => +'/\>/', Binmode=>1,Dump_log=>$file1,Input_log=>$file2,Telnetmode=>0); + $Rtelnet->open(host => $hostname, port => $hostport); # Stop previous output and clear buffers $Rtelnet->put("SSSSSSSSSS\n"); $Rtelnet->buffer_empty; # Send an empty line WHY??-otheriwse it does not work... $Rtelnet->cmd(''); # find out the name of the port (format IP##) my $prompt; my $tryout=0; my $Rdevice; while ( $tryout <= 5 ) { my @lines2=$Rtelnet->cmd(''); $prompt=pop(@lines2); if (!(defined($prompt))) { print "Read on IP port timed out while testing for prompt\n"; } elsif ( $prompt =~ /(\S\S\d\d)/i ) { $Rdevice=$1; last; } $tryout++; } if ( $tryout > 5 ){ die "Unable to determine the prompt"; } $prompt=$Rdevice.">"; my $Rreprompt='/'.$prompt.'$/i'; print "Prompt is [$prompt]\n" ; # Set prompt as match operators (for record seperators) $Rtelnet->prompt($Rreprompt); # Start command line mode print "Connected to $hostname in command mode; enter command, quit or +exit\n"; print $prompt; my $data; while (<STDIN>) { chomp; last if (/^quit$/i || /^exit$/i); $data=Rcmd($_); print "$data"; print $prompt; } # End of command mode sub Rcmd{ my ($cmd)=@_; my $buffer; my @lines; @lines = $Rtelnet->cmd(String=>$cmd); $buffer=join('',@lines); return $buffer; } sub Syntax{ my( $Script ) = ( $0 =~ m#([^\\/]+).pl$# ); my $Line = "-" x length( $Script ); print << "EOT"; $Script (Version: $VERSION) $Line Download, mirror and remove internal data files. Syntax: $Script -i <Hostname> [-p <Port> -h] -i <Hostname>...IP name/address. -p <Port>.......IP port to conect to. -?|h|help.......This help. Examples: $Script -i 192.168.0.1 -p 8888 EOT }
Output with direct response
> 0x00000: 53 53 53 53 53 53 53 53 53 53 0a SSSSSSS +SSS. > 0x00000: 0a . < 0x00000: 49 50 31 32 3e IP12> > 0x00000: 0a . < 0x00000: 49 50 31 32 3e IP12> > 0x00000: 67 64 69 6f 0a gdio. < 0x00000: 24 52 3a 20 67 64 69 6f 0d 0a 20 20 44 61 74 61 $R: gdi +o.. Data < 0x00010: 49 6e 4f 75 74 2c 20 44 53 4b 31 2c 20 43 4d 44 InOut. +DSK1. CMD < 0x00020: 2c 20 53 42 46 2b 4e 4d 45 41 2c 20 28 6f 6e 29 . SBF+N +MEA. (on) < 0x00030: 0d 0a 20 20 44 61 74 61 49 6e 4f 75 74 2c 20 43 .. Dat +aInOut. C < 0x00040: 4f 4d 31 2c 20 43 4d 44 2c 20 53 42 46 2b 4e 4d OM1. CM +D. SBF+NM < 0x00050: 45 41 2c 20 28 6f 6e 29 0d 0a 20 20 44 61 74 61 EA. (on +).. Data < 0x00060: 49 6e 4f 75 74 2c 20 43 4f 4d 32 2c 20 43 4d 44 InOut. +COM2. CMD < 0x00070: 2c 20 53 42 46 2b 4e 4d 45 41 2c 20 28 6f 6e 29 . SBF+N +MEA. (on) < 0x00080: 0d 0a 20 20 44 61 74 61 49 6e 4f 75 74 2c 20 43 .. Dat +aInOut. C < 0x00090: 4f 4d 33 2c 20 43 4d 44 2c 20 53 42 46 2b 4e 4d OM3. CM +D. SBF+NM < 0x000a0: 45 41 2c 20 28 6f 6e 29 0d 0a 20 20 44 61 74 61 EA. (on +).. Data < 0x000b0: 49 6e 4f 75 74 2c 20 43 4f 4d 34 2c 20 43 4d 44 InOut. +COM4. CMD < 0x000c0: 2c 20 53 42 46 2b 4e 4d 45 41 2c 20 28 6f 6e 29 . SBF+N +MEA. (on) < 0x000d0: 0d 0a 20 20 44 61 74 61 49 6e 4f 75 74 2c 20 55 .. Dat +aInOut. U < 0x000e0: 53 42 31 2c 20 43 4d 44 2c 20 53 42 46 2b 4e 4d SB1. CM +D. SBF+NM < 0x000f0: 45 41 2c 20 28 6f 66 66 29 0d 0a 20 20 44 61 74 EA. (of +f).. Dat < 0x00100: 61 49 6e 4f 75 74 2c 20 55 53 42 32 2c 20 43 4d aInOut. + USB2. CM < 0x00110: 44 2c 20 53 42 46 2b 4e 4d 45 41 2c 20 28 6f 66 D. SBF+ +NMEA. (of < 0x00120: 66 29 0d 0a 20 20 44 61 74 61 49 6e 4f 75 74 2c f).. D +ataInOut. < 0x00130: 20 49 50 31 30 2c 20 43 4d 44 2c 20 53 42 46 2b IP10. +CMD. SBF+ < 0x00140: 4e 4d 45 41 2c 20 28 6f 66 66 29 0d 0a 20 20 44 NMEA. ( +off).. D < 0x00150: 61 74 61 49 6e 4f 75 74 2c 20 49 50 31 31 2c 20 ataInOu +t. IP11. < 0x00160: 43 4d 44 2c 20 53 42 46 2b 4e 4d 45 41 2c 20 28 CMD. SB +F+NMEA. ( < 0x00170: 6f 66 66 29 0d 0a 20 20 44 61 74 61 49 6e 4f 75 off).. + DataInOu < 0x00180: 74 2c 20 49 50 31 32 2c 20 43 4d 44 2c 20 53 42 t. IP12 +. CMD. SB < 0x00190: 46 2b 4e 4d 45 41 2c 20 28 6f 6e 29 0d 0a 20 20 F+NMEA. + (on).. < 0x001a0: 44 61 74 61 49 6e 4f 75 74 2c 20 49 50 31 33 2c DataInO +ut. IP13. < 0x001b0: 20 43 4d 44 2c 20 53 42 46 2b 4e 4d 45 41 2c 20 CMD. S +BF+NMEA. < 0x001c0: 28 6f 66 66 29 0d 0a 20 20 44 61 74 61 49 6e 4f (off).. + DataInO < 0x001d0: 75 74 2c 20 49 50 31 34 2c 20 43 4d 44 2c 20 53 ut. IP1 +4. CMD. S < 0x001e0: 42 46 2b 4e 4d 45 41 2c 20 28 6f 66 66 29 0d 0a BF+NMEA +. (off).. < 0x001f0: 20 20 44 61 74 61 49 6e 4f 75 74 2c 20 49 50 31 DataI +nOut. IP1 < 0x00200: 35 2c 20 43 4d 44 2c 20 53 42 46 2b 4e 4d 45 41 5. CMD. + SBF+NMEA < 0x00210: 2c 20 28 6f 66 66 29 0d 0a 20 20 44 61 74 61 49 . (off) +.. DataI < 0x00220: 6e 4f 75 74 2c 20 49 50 31 36 2c 20 43 4d 44 2c nOut. I +P16. CMD. < 0x00230: 20 53 42 46 2b 4e 4d 45 41 2c 20 28 6f 66 66 29 SBF+NM +EA. (off) < 0x00240: 0d 0a 20 20 44 61 74 61 49 6e 4f 75 74 2c 20 49 .. Dat +aInOut. I < 0x00250: 50 31 37 2c 20 43 4d 44 2c 20 53 42 46 2b 4e 4d P17. CM +D. SBF+NM < 0x00260: 45 41 2c 20 28 6f 66 66 29 0d 0a 49 50 31 32 3e EA. (of +f)..IP12>
Debug output with response after sending an extra return
> 0x00000: 53 53 53 53 53 53 53 53 53 53 0a SSSSSSS +SSS. > 0x00000: 0a . < 0x00000: 49 50 31 37 3e IP17> > 0x00000: 0a . < 0x00000: 49 50 31 37 3e IP17> > 0x00000: 67 64 69 6f 0a gdio. < 0x00000: 49 50 31 37 3e IP17> > 0x00000: 0a . < 0x00000: 24 52 3a 20 67 64 69 6f 0d 0a 20 20 44 61 74 61 $R: gdi +o.. Data < 0x00010: 49 6e 4f 75 74 2c 20 44 53 4b 31 2c 20 43 4d 44 InOut. +DSK1. CMD < 0x00020: 2c 20 53 42 46 2b 4e 4d 45 41 2c 20 28 6f 6e 29 . SBF+N +MEA. (on) < 0x00030: 0d 0a 20 20 44 61 74 61 49 6e 4f 75 74 2c 20 43 .. Dat +aInOut. C < 0x00040: 4f 4d 31 2c 20 43 4d 44 2c 20 53 42 46 2b 4e 4d OM1. CM +D. SBF+NM < 0x00050: 45 41 2c 20 28 6f 6e 29 0d 0a 20 20 44 61 74 61 EA. (on +).. Data < 0x00060: 49 6e 4f 75 74 2c 20 43 4f 4d 32 2c 20 43 4d 44 InOut. +COM2. CMD < 0x00070: 2c 20 53 42 46 2b 4e 4d 45 41 2c 20 28 6f 6e 29 . SBF+N +MEA. (on) < 0x00080: 0d 0a 20 20 44 61 74 61 49 6e 4f 75 74 2c 20 43 .. Dat +aInOut. C < 0x00090: 4f 4d 33 2c 20 43 4d 44 2c 20 53 42 46 2b 4e 4d OM3. CM +D. SBF+NM < 0x000a0: 45 41 2c 20 28 6f 6e 29 0d 0a 20 20 44 61 74 61 EA. (on +).. Data < 0x000b0: 49 6e 4f 75 74 2c 20 43 4f 4d 34 2c 20 43 4d 44 InOut. +COM4. CMD < 0x000c0: 2c 20 53 42 46 2b 4e 4d 45 41 2c 20 28 6f 6e 29 . SBF+N +MEA. (on) < 0x000d0: 0d 0a 20 20 44 61 74 61 49 6e 4f 75 74 2c 20 55 .. Dat +aInOut. U < 0x000e0: 53 42 31 2c 20 43 4d 44 2c 20 53 42 46 2b 4e 4d SB1. CM +D. SBF+NM < 0x000f0: 45 41 2c 20 28 6f 66 66 29 0d 0a 20 20 44 61 74 EA. (of +f).. Dat < 0x00100: 61 49 6e 4f 75 74 2c 20 55 53 42 32 2c 20 43 4d aInOut. + USB2. CM < 0x00110: 44 2c 20 53 42 46 2b 4e 4d 45 41 2c 20 28 6f 66 D. SBF+ +NMEA. (of < 0x00120: 66 29 0d 0a 20 20 44 61 74 61 49 6e 4f 75 74 2c f).. D +ataInOut. < 0x00130: 20 49 50 31 30 2c 20 43 4d 44 2c 20 53 42 46 2b IP10. +CMD. SBF+ < 0x00140: 4e 4d 45 41 2c 20 28 6f 66 66 29 0d 0a 20 20 44 NMEA. ( +off).. D < 0x00150: 61 74 61 49 6e 4f 75 74 2c 20 49 50 31 31 2c 20 ataInOu +t. IP11. < 0x00160: 43 4d 44 2c 20 53 42 46 2b 4e 4d 45 41 2c 20 28 CMD. SB +F+NMEA. ( < 0x00170: 6f 66 66 29 0d 0a 20 20 44 61 74 61 49 6e 4f 75 off).. + DataInOu < 0x00180: 74 2c 20 49 50 31 32 2c 20 43 4d 44 2c 20 53 42 t. IP12 +. CMD. SB < 0x00190: 46 2b 4e 4d 45 41 2c 20 28 6f 66 66 29 0d 0a 20 F+NMEA. + (off).. < 0x001a0: 20 44 61 74 61 49 6e 4f 75 74 2c 20 49 50 31 33 DataIn +Out. IP13 < 0x001b0: 2c 20 43 4d 44 2c 20 53 42 46 2b 4e 4d 45 41 2c . CMD. +SBF+NMEA. < 0x001c0: 20 28 6f 66 66 29 0d 0a 20 20 44 61 74 61 49 6e (off). +. DataIn < 0x001d0: 4f 75 74 2c 20 49 50 31 34 2c 20 43 4d 44 2c 20 Out. IP +14. CMD. < 0x001e0: 53 42 46 2b 4e 4d 45 41 2c 20 28 6f 66 66 29 0d SBF+NME +A. (off). < 0x001f0: 0a 20 20 44 61 74 61 49 6e 4f 75 74 2c 20 49 50 . Data +InOut. IP < 0x00200: 31 35 2c 20 43 4d 44 2c 20 53 42 46 2b 4e 4d 45 15. CMD +. SBF+NME < 0x00210: 41 2c 20 28 6f 66 66 29 0d 0a 20 20 44 61 74 61 A. (off +).. Data < 0x00220: 49 6e 4f 75 74 2c 20 49 50 31 36 2c 20 43 4d 44 InOut. +IP16. CMD < 0x00230: 2c 20 53 42 46 2b 4e 4d 45 41 2c 20 28 6f 66 66 . SBF+N +MEA. (off < 0x00240: 29 0d 0a 20 20 44 61 74 61 49 6e 4f 75 74 2c 20 ).. Da +taInOut. < 0x00250: 49 50 31 37 2c 20 43 4d 44 2c 20 53 42 46 2b 4e IP17. C +MD. SBF+N < 0x00260: 4d 45 41 2c 20 28 6f 6e 29 0d 0a 49 50 31 37 3e MEA. (o +n)..IP17>

In reply to Net::Telnet inconsistent output of ->cmd? by popijopi

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.