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

Hi I have put together a a basic telnet script script . I can get it to work fine for simple commands but what I want to do is replicate a script I user over ssh to spool the <STDOUT> to a file. The problem I keep hitting is the script runs and produces a page of info with a more at the bottom and then halts and then the script times out. I know it is doing this as I trace the TCP connection with ethereal and I can see the command execute and return a screen full of data with the more at the bottom

The basic script looks like this. If I change the comand to an 'ls' it works a treat

use Net::Telnet (); use warnings; my $host ='somehost'; my $username ='user'; my $passwd = 'password'; #my $prompt = 'prompt'; my $cmd = '/ flow export fcap "src net a.b.c.d " recent'; # my $cmd ='ls'; $telnet = Net::Telnet ->new (Timeout => 20, Host=> $host, Prom +pt => "/$prompt/"); $telnet ->login(Name =>$username, Password => $passwd); @lines = $telnet ->cmd($cmd); $telnet -> close; pop @lines; print @lines;

Cheers Nigel

Edited by Chady -- code tags.

Replies are listed 'Best First'.
Re: problem with net::telnet moduleand STDOUT
by tachyon (Chancellor) on Jul 26, 2004 at 11:07 UTC

    If ls works (presuming that means several screens worth of data) but the 'flow' command does not and starts paging it is an issue of the interaction between flow and the Net::Telnet virtual tty. I presume that the flow command automatically pages if it detects a (screen/buffer) full of data has been written. I suspect that increasing the size of the Net::Telnet buffer (so it is large enough to accept all the expected data++) may fix the problem $obj->max_buffer_length(BIG_NUM)

    Also note that there is an example in the docs that shows how to copy a big file, so if the previous suggestion does not work perhaps just redirect the flow output to a file and then read from that. Other options to consider are the getlines() method.

    cheers

    tachyon

      Hi

      adjusting the max buffer did not work. By setting the value to a big number this stopped the server process spooling the output. Due to a system constraint it is not possible to create files on the remote system that is why I need to read the screen output to a file but first I need to overcome the more problem.

      Cheers Nigel

        Not knowing anything about the flow program all I can suggest it that it has a pager flag and in its interaction with Net::Telnet is delivering a virtual screenful. I don't definitively know how to fix flow or the Net::Telnet interaction. Regardless this hack should work around it. If I pipe a stream through more and then to cat:

        $ ll | more | cat

        Then more is effectively disabled as cat accepts the stream and just pipes it out so it streams several screenfuls. Certainly worth a try and I can't see why it will not work as we just abstracted the output from flow away from the vtty, but without using a file.

        Next suggestion is to try:

        $obj->option_accept( Dont => TELOPT_NAOP, Dont => TELOPT_NAWS ) # from the source we see the constants being declared..... # sub TELOPT_NAOP () {9}; # Output Page Size # sub TELOPT_NAWS () {31}; # Negotiate About Window Size

        If you dig into the souce you should find a burried use IO::Pty which does itself have a method to control the window size but no easy way to get at it short of hacking the source.

        cheers

        tachyon

Re: problem with net::telnet moduleand STDOUT
by eXile (Priest) on Jul 26, 2004 at 15:03 UTC
    Another approach is to use Expect to manage your input/output. In the tutorials-section there is a good tutorial on how to do this.
Re: problem with net::telnet moduleand STDOUT
by vladdrak (Monk) on Jul 27, 2004 at 05:56 UTC
    Looks like you may be accessing a Cisco or a Foundry box..? You might try issuing a command to kill paging first, ala:
    $telnet->cmd("skip-page"); # foundry $telnet->cmd("set length 0"); # new ciscos $telnet->cmd("term length 0"); # old ciscos
    -Vlad