Hi there friends,

So I'm writing my first perl program using threads, and it's actually doing what I want it to do, but I don't understand why :-)

Basically it's a testing program that wget's a file off a web server while tailing various logs:

#!/usr/bin/perl use strict; use warnings; use threads; use Net::OpenSSH; use Data::Dumper; my $sensor_ssh = Net::OpenSSH->new( 'rasto:password@10.3.12.175', 'master_opts' => [ '-o' => 'StrictHostKeyChecking=no' ] ); $sensor_ssh->error and die "Couldn't establish SSH connection: " . $sensor_ssh->error; my $sudo = $sensor_ssh->capture( { stderr_to_stdout => 1 }, 'echo \'password\' | sudo -S bash' ); print "initialize sudo: " . $sudo; my $wget_thread = threads->create( \&wget_sub ); my $sensor_syslog_thread = threads->create( \&tail_sensor_syslog, $sen +sor_ssh ); $wget_thread->join(); my $tail_syslog = $sensor_syslog_thread->join(); print "tail syslog: $tail_syslog\n"; my $test = $sensor_ssh->capture('ls') or warn "Failed: $sensor_ssh->er +ror \n"; print Dumper $sensor_ssh; sub wget_sub { my $wget = `wget http://10.3.13.4/xaa`; print "wget:\n$wget\n"; return 1; } sub tail_sensor_syslog { my $ssh = shift; print("In the thread\n"); my $cap = $ssh->capture( { stderr_to_stdout => 1 }, 'sudo tail -f /var/log/syslog' ); return $cap; }
So basically what happens is when the wget is finished, the tail -f going on stops. This is convenient for me at the moment, but the more I think about it I don't understand why.

One thing that is interesting is that if I attempt to use the $sensor_ssh connection again, it will warn in this code:

Failed: Net::OpenSSH=HASH(0xa21b220)->error
In the dump of the Net::OpenSSH object I see this bit:
'_error' => 'master ssh connection broken',
So I guess my question is, how is the master ssh connection being broken? And is there a better way to do this? It's convenient for me right now (as the join() does return the data) but I'm not to comfortable not understanding what's going on here, as I'm sure in the future it will come back and bite me :-)

Any input would be greatly appreciated!ls


In reply to threads and Net::OpenSSH by rastoboy

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.