Assuming $rh is connected to the appropriate host and you have successfully authenticated (to what looks like a Juniper device?), the main problem here seems to be your while(){} loop which will never end but (assuming ->cmd() ever exists) will keep executing the same command over and over, assigning the output to a variable that immediately goes out of lexical scope.

Adding use strict; use warnings; would have pointed this out to you btw.

Having worked quite a bit with Juniper devices myself, I used Net::Telnet::Cisco but had to customize the "prompt" setting in order to get ->cmd() to work as expected. If you are using that module and didn't set the appropriate "prompt" setting, then ->cmd() will hang indefinitely while waiting for a Cisco command prompt, which is a little different.

I know I'm guessing a lot here, but I hope it helps. FYI, here's how I do it with MX, EX and SRX series -- seems to work pretty well:
@lines = $session->cmd( String => 'show version | no-more', Prompt => '/\n\S+\>/');

Putting it all together, try this:

use strict; use warnings; # Open your $rh and authenticate here # ... open(my $fh, "+>>default_ve.txt") || die "Couldn't open file: $!"; my @lines = $rh->cmd(String => 'show version | no-more', Prompt => '/\ +n\S+\>/'); foreach my $line (@lines) { print $fh $line; } close $fh; # Close $rh or do more stuff here # ...

Finally, note the use of "| no-more" to safeguard against output longer than your (virtual) terminal size which would cause an interactive prompt. Use it always, just to be safe.

If you're not using a Juniper device, read the stuff about the loop and disregard everything else :o)

-- FloydATC

Time flies when you don't know what you're doing


In reply to Re: File operations by FloydATC
in thread File operations by Anonymous Monk

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.