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"; }

In reply to Net::OpenSSH stdout_fh problem by jbernest

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.