Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Hello, the experts:
According to someone's code, I try to reproduce the following: open a filehandle to run a command in remote linux machine, set it to non-blocking filehand, then use a sysread loop to read the results from the filehandle. It does't work. Could you help me check what is wrong in my code :

my $cmd = '/opt/rssh/bin/ssh'; my $machine = "remote_machine"; open(my $fh, "-|", "$cmd $machine \"hostname;uptime\" 2>&1") or die "c +annot open fh"; my $flags = fcntl($fh, F_GETFL, 0) or die "\ncan't get flags for the p +ipe: $!\n"; fcntl($fh, F_SETFL, $flags | O_NONBLOCK) or die "\ncan't set flags: $! +\n"; my $out = $fh; my $bytes_read = -1; my $blocksize = 1024; my $result = ""; while ($bytes_read) { my $buf; $bytes_read = sysread($out, $buf, $blocksize); if (defined($bytes_read)) { if ($bytes_read == 0) { close($out); last; } else { print "\nbuf content\n"; $result.= $buf; } }else { if ($! == EAGAIN()) { print "\nEAGAIN is set\n"; last; } else { last; } } } print "\nresult is: $result \n";
The result is:
EAGAIN is set result is:
But if I replace the "while ($bytes_read) " with the while (<$fl>) as the following, I can get the result.
my $cmd = '/opt/rssh/bin/ssh'; my $machine = "remote_machine"; open(my $fh, "-|", "$cmd $machine \"hostname;uptime\" 2>&1") or die "c +annot open fh"; #my $flags = fcntl($fh, F_GETFL, 0) or die "\ncan't get flags for the +pipe: $!\n"; #fcntl($fh, F_SETFL, $flags | O_NONBLOCK) or die "\ncan't set flags: $ +!\n"; my @result ; while (<$fh>) { # chomp; push @result,$_; } print "\nresult is: @result \n";

The result is
result is: ccwsge0553 13:40:10 up 4 days, 2 min, 0 users, load average: 0.08, 0.03, 0.01
What is wrong in my code with non-blocking/sysread? Thanks in advance!

In reply to use sysread to read non-blocking filehandle by x12345

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
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: (4)
As of 2024-03-29 00:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found