Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: File operations

by FloydATC (Deacon)
on Nov 20, 2015 at 14:31 UTC ( [id://1148239]=note: print w/replies, xml ) Need Help??


in reply to File operations

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1148239]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2024-04-24 05:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found