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

Net::OpenSSH stdout_fh problem

by jbernest (Novice)
on Aug 21, 2013 at 04:46 UTC ( #1050278=perlquestion: print w/replies, xml ) Need Help??
jbernest has asked for the wisdom of the Perl Monks concerning the following question:

I'm using the stdout_fh option in the "spawn" function in Net::OpenSSH to write standard output to a file. The problem is, for some reason I'm having trouble later reading the contents of the file. When I try to open the file for reading, I get an error message, "mux_client_request_session: read from master failed: Broken pipe". Any ideas what this message means?

The output file contains the information I want, I'm just not able to read it in the same Perl script. I can make a new Perl script and read it just fine, however. If I print to the same file in the normal way first, I can read what I print, but not the contents of standard output below that line. If I first write standard output contents to the file, and then print to the file normally, I can't read what I print.

You can try to reproduce my problem by filling in 2 remote hosts you have access to and user name and password here. This script prints the standard output from the "hostname" command to "filename.txt". You can un-comment the "print testing" line to print to "filename.txt" and see if you can read it.

Thanks to anyone who can help me figure out what's wrong.

#!/usr/bin/perl use strict; use warnings; use Net::OpenSSH; my @hosts = ("host.name.1.edu", "host.name.2.edu"); my %conn = map { $_ => Net::OpenSSH->new(host => $_, user => 'user', port => 22, pa +ssword => 'password'); } @hosts; open (my $fh, ">filename.txt"); # print $fh "testing\n"; foreach(@hosts) { $conn{$_}->spawn({stdout_fh => $fh},'hostname'); } open(FILE1, "<filename.txt"); while(my $line1 = <FILE1>) { chomp $line1; print "$line1\n"; }

Replies are listed 'Best First'.
Re: Net::OpenSSH stdout_fh problem
by salva (Abbot) on Aug 26, 2013 at 08:56 UTC
    You are running the remote commands in the background and not waiting for then to finish before reading the output file.
Re: Net::OpenSSH stdout_fh problem
by jbernest (Novice) on Aug 21, 2013 at 05:01 UTC
    10 minutes later, and I just tried "sleep 2" and it works. I don't know why it works and I don't care.
      10 minutes later, and I just tried "sleep 2" and it works. I don't know why it works and I don't care.

      We just witnessed the birth of a new maintainance nightmare.

      Alexander

      --
      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1050278]
Approved by Corion
help
Chatterbox?
[Corion]: choroba: Hmm - no, I keep the snippets inline, but as my framework also has support for capturing output etc., maybe I should do the same...
[Corion]: haukex: Yes, that approach is sane, and it heals the fragility of Pod parsers in a nice way while still syntax-checking stuff
[choroba]: Unfortunately, none of it is online
[haukex]: I figured that POD tests make sense, but only as author tests
[choroba]: I mean, the slides are, but not the makefile with scripts to create them
[Corion]: haukex: I've only now arrived at that revelation ;)
[Corion]: choroba: I use spod5, which also has that support, and also implements its own kinda-make stuff
[haukex]: But that module I just linked to assumes that most verbatim blocks are runnable code, I have other modules where that's not the case, so there I just copy-and-paste the synopsis into the author tests...
[haukex]: not the most efficient, but then again, I don't have that many modules on CPAN :-)
[Corion]: haukex: Yes, but if it's only supposed to run on my machine, I can be far more liberal with how I extract the code etc.

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (11)
As of 2017-02-27 12:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?






    Results (385 votes). Check out past polls.