By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,359 Members | 1,481 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,359 IT Pros & Developers. It's quick & easy.

getlines from Net::Telnet works different (works/does not work). while loop opt?

P: 2
Hello.

I'm trying to work with Cisco hardware.

Simple script read the config:
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5. use Net::Telnet;
  6.  
  7. my $t;
  8. my $host = "megahw";
  9. my $username = "megauser";
  10. my $passwd = "megapassword";
  11. my @lines = ();
  12. my $fh;
  13. my $fn = "/var/tmp/t.log";
  14. my $lline;
  15. my @llines = ();
  16. my $ownline;
  17. my $screenlines;
  18.  
  19. $t = new Net::Telnet(Timeout => 10, Prompt => '/Username: $/');
  20. $t->open($host);
  21. $fh = $t->input_log($fn);
  22. $t->login(Name => $username, Password => $passwd, Prompt => '/#$/');
  23. $t->print("show running-config");
  24. @lines = ();
  25. $ownline = 1;
  26. $screenlines = 25;
  27.  
  28. while (1){
  29.     @llines = $t->getlines(All => 0);
  30.     $lline = scalar @llines;
  31.  
  32.     if ($lline == $screenlines){
  33.  
  34.         while (scalar @llines){
  35.             $lline = shift @llines;
  36.             push @lines, $lline;
  37.         }
  38.  
  39.         if ($ownline){
  40.             $ownline = 0;
  41.             $screenlines -= 2;
  42.         }
  43.  
  44.         $t->put(' ');
  45.         next;
  46.     }else{
  47.  
  48.         while (scalar @llines){
  49.             $lline = shift @llines;
  50.             push @lines, $lline;
  51.         }
  52.  
  53.         last;
  54.     }
  55.  
  56. }
  57.  
  58. $t->print("exit");
  59. $t->break;
  60. $t->close;
  61.  
  62. while (scalar @lines){
  63.     $lline = shift @lines;
  64.     print $lline;
  65. }
  66.  
If I run it flat (script.pl) I get my own command only. t.log has:
Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. User Access Verification
  4.  
  5. Username: megauser
  6. Password: 
  7.  
  8. megahw#show running-config
  9.  
If I run it via perl -d script.pl and type c(ontinue) immediately the t.log is the same.

If I run it via perl -d script.pl and go inside while(1) loop via n(ext) commands and then c(continue), the t.log has:
Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. User Access Verification
  4.  
  5. Username: megauser
  6. Password: 
  7.  
  8. megahw#show running-config
  9. Building configuration...
  10.  
  11. Current configuration : 2201 bytes
  12. !
  13. ! No configuration change since last restart
  14. !
  15. version 15.2
  16. no service pad
  17. service timestamps debug datetime msec
  18. service timestamps log datetime msec
  19. !
  20. hostname megahw
  21. !
  22. boot-start-marker
  23. !
  24. There are many lines of config
  25. !
  26. end
  27.  
That behaviour is very strange for me.

Any clues?
May 26 '16 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.