Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Problem with telnet and paging

by anton (Initiate)
on Dec 19, 2012 at 11:20 UTC ( #1009547=perlquestion: print w/ replies, xml ) Need Help??
anton has asked for the wisdom of the Perl Monks concerning the following question:

Hi! First of all, I'm a beginner at perl - so be easy on me :) I'm trying to write a program which uses Net::Telnet to check information on my router (switch (different manufacturers)), and I have a problem with some commands returning a lot of data. I'm using:
my @tmp = $telnet->cmd("sh ver");
It causes a time-out and quits the program. My theory is that the output data is page'd (the switch responds with either "--More--" or "Press c for continous, q for quit, <space> for one more row" and then halts until a char has been pressed. But "->cmd" doesn't issue this, tries to read for X seconds then quits. Is there any way I can tell the Telnet-package to read everything, or do I need to use $telnet->send and $telnet->getline? :< I know that this might be the wrong forum to ask this, but I'm running out of ideas. =) Thanks in advance. Best regards, Anton.

Comment on Problem with telnet and paging
Download Code
Replies are listed 'Best First'.
Re: Problem with telnet and paging
by salva (Abbot) on Dec 19, 2012 at 11:32 UTC
    Pagging can be disabled on most devices. Just, check the device docs and start the telnet sessions by sending the command that disables it!
Re: Problem with telnet and paging
by roboticus (Chancellor) on Dec 19, 2012 at 11:51 UTC

    anton:

    Salva's suggestion is likely the best way to go. But if you can't disable paging, check out Expect--it's designed for just this type of problem.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

Re: Problem with telnet and paging
by VinsWorldcom (Vicar) on Dec 19, 2012 at 13:09 UTC

    With your example "sh ver" command I immediately think Cisco and when I connect to Cisco routers (using Net::Telnet::Cisco by the way, which in turn uses Net::Telnet), I always issue "terminal length 0" as the first command. That disables paging and then long output like 'show ver', 'show run', etc... always work for me.

      Hi.

      Thank you all for your replies. I've looked into the solution with expect - though I don't have the package and the box I'm running the script on is very limited.

      The software is indeed Cisco, but my other switches/routes are a different kind (huawei, etc) and they do not have any support for terminal length.

      Perhaps my best option would be to use a basic i/o socket, and just read for the '--More--'-line and send a space, or whatnot?

      Thanks in advance.

        this works for me on my CISCO's... may be it's the lines...

        $telnet->prompt('/\w+# $/'); $telnet->waitfor($telnet->prompt);
        that make it work... Don't know, just figuring this out myself...
        usage: ./program.pl 10.10.10.77 z.out

        #!/usr/bin/perl use strict; use warnings; use Net::Telnet; my $uri = shift @ARGV or die "IP address needed \n Usage: ./runssh 10. +10.10.77 outputfile"; my $a = shift @ARGV or die "output file needed \n usage: ./runssh 10.1 +0.10.77 outputfile"; my $telnet = new Net::Telnet (Timeout=>15 #,Input_log => "received_data.txt" ,Input_log => $a ); $telnet->open($uri); $telnet->waitfor('/login: /i'); $telnet->print('Place_the_login_here'); $telnet->waitfor('/Password: /i'); $telnet->print('Place_the password_here'); print "Login is successful \n"; $telnet->prompt('/\w+# $/'); $telnet->waitfor($telnet->prompt); $telnet->cmd("terminal length 0"); $telnet->cmd("show interface brief"); $telnet->cmd("sh int desc"); $telnet->cmd("sh flogi data"); $telnet->cmd("sh zone act"); $telnet->waitfor($telnet->prompt); my $out = $telnet->print('exit'); 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/; }
Re: Problem with telnet and paging
by Anonymous Monk on Apr 09, 2015 at 09:24 UTC

    Even though the switch /router is waiting for a prompt the ---More--- or Press Ctrl+C etc etc, by returning a carriage return or inserting a new line char at the end of your command and the next paged content turns up. Ex: $telnet->cmd('xxxxxxxxxxx'); #If this lines throws an error stating command timed-out, its because your script is waiting for the response from the device you tried to telnet while your device is waiting for a response from the user(Script in this case) which leads to waiting on both the ends and times out. So it can be solved by adding a \n or \t or \r character in the command. Ex: $telnet->cmd('xxxxxxxxxxx\n\r\t');# In this case its similar to the user or script hitting an enter key after the entire command is typed. Now if there are 5 pages in the telnet ouput, append 5 "\n" or "\r" or "\t". Well if there are 7 pages make append count to 7

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1009547]
Approved by marto
Front-paged by MidLifeXis
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (13)
As of 2015-09-03 17:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred temperature scale is:










    Results (122 votes), past polls