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
-
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.