Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
Hi Corion,

Thanks for your reply. Main code:
#!/usr/bin/perl use threads; use strict; use warnings; use Net::SSH::Expect; require 'total.pl'; my @servers = ('192.168.0.18','192.168.0.19','192.168.0.20'); my @process = ('testing','b','c'); my @threads; my $output; my $res; foreach my $host ( @servers ) { my $png = `ping -c1 $host`; if ( $png =~ /64 bytes/ ) { my $user= 'netcool'; my $ssh = &SSH_Reference($host,$user); $ssh->run_ssh(); eval {$res=$ssh->read_all(); }; if ( !$@ && $res !~ /\(yes\/no\)/ && $res !~ m/password/ ) { print "\nLogged in to $host\n"; foreach my $proc ( @process ) { push (@threads, threads->new(\&Process_Output, $ss +h, $proc )); while ( my $thread = shift @threads ) { $output=$thread->join(); #print @output; if ( $output =~ m/$proc/ ) { print "\t$proc is runn +ing\n"; } else { print "\t$proc is not +running\n"; } } } $ssh->close(); } else { print "Unable to SSH to $host\n"; } } else { print "\nUnable to ping to Host: $host\n"; } }
total.pl:
use Config::General; use Tie::IxHash; use Net::SNMP; sub SSH_Reference() { ($host,$user)=@_; #print "$host---$user--\n"; #($host,$user)= @_; $ssh = Net::SSH::Expect->new ( host => $host, user => $user, timeout=>10, raw_pty => 1 ); return $ssh; } sub Process_Output() { $ssh = shift; $process = shift; #( $ssh, $process ) = @_; $output; $ssh->eat($ssh->peek(0)); $ssh->send("ps -ef | grep $process | grep -v 'grep'"); while ( defined ($line = $ssh->read_line(2)) ) { next if ( $line =~ m/grep/ ); $output .= "$line"; } $ssh->eat($ssh->peek(0)); return $output; } 1;
And the output is:
perl some.pl Logged in to 192.168.0.18 Use of uninitialized value in pattern match (m//) at some.pl line 33. testing is not running Thread 2 terminated abnormally: SSHConnectionAborted at total.pl line +25 Use of uninitialized value in pattern match (m//) at some.pl line 33. b is not running Thread 3 terminated abnormally: SSHConnectionAborted at total.pl line +25 Use of uninitialized value in pattern match (m//) at some.pl line 33. c is not running Logged in to 192.168.0.19 Segmentation fault
Please suggest

Thanks,
Ashok

In reply to Re^2: Need help with Perl multi threading by ashok.g
in thread Need help with Perl multi threading by ashok.g

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!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others examining the Monastery: (3)
    As of 2014-09-15 02:13 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My favorite cookbook is:










      Results (145 votes), past polls