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:
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.#!/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; }
One thing that is interesting is that if I attempt to use the $sensor_ssh connection again, it will warn in this code:
In the dump of the Net::OpenSSH object I see this bit:Failed: Net::OpenSSH=HASH(0xa21b220)->error
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 :-)'_error' => 'master ssh connection broken',
Any input would be greatly appreciated!ls
In reply to threads and Net::OpenSSH by rastoboy
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |