Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
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"; }

Comment on Net::OpenSSH stdout_fh problem
Download Code
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". ;-)
Re: Net::OpenSSH stdout_fh problem
by salva (Monsignor) 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.

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?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (9)
As of 2014-08-20 16:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (118 votes), past polls