perlquestion
perl514
<p>Respected Monks </p>
<p> Given below is the script reads EMC Celerra NAS Array IPs from a text file and then logs into those NAS Arrays, runs a command and then gathers the output and sends out an e-mail.
Please let me know if there is a better and more idiomatic way to write it. I tried running multiple commands using the "shell" option of Net::SSH2, but something isnt working. That portion is not given here as I want to try all the possibilities and error checks before I ask it here, but this is what I have as of now. </p>
<p> Here is the script</p>
<code>
#!/usr/bin/perl
use warnings;
use strict;
use Net::SSH2;
use MIME::Lite;
MIME::Lite->send ("smtp", "mail.server.com");
if (!open my $fh , "<" , "C:/path/to/nas_array_ip_list.txt")
{
my $msg = MIME::Lite->new
(
From => 'name@email.com',
To => 'name@email.com',
Data => "Error:nas_array_ip_list.txt:$!.$^E\n",
Subject => "IP List File - nas_array_ip_list.txt - Not Found For $0 Script on $ENV{COMPUTERNAME}\n",
);
$msg->send ();
}
else {
print "Please wait. $0 script is being executed...\n";
open my $mailfh, ">", "C:/path/to/dmcheck.txt";
print $mailfh "\n############################################\n";
print $mailfh "\nUser: $ENV{USERNAME} running $0 from $ENV{COMPUTERNAME}\n\n";
print $mailfh "\n###########################################\n";
while (<$fh>)
{
next if (/^#/);
my ($ipname, $ipaddr) = split /\t+|\s+/;
my $username = 'username';
my $password = 'password';
my $ssh2 = Net::SSH2->new();
print $mailfh "\n----------------------------";
print $mailfh "\nData Mover Check For $ipname ($ipaddr)\n";
print $mailfh "----------------------------\n";
$ssh2->connect("$ipaddr") || die "PROBELM -$!";
$ssh2->auth_password("$username","$password") || die "Username/Password not right";
my $chan = $ssh2->channel();
$chan->blocking(0);
$chan->exec('/nas/sbin/getreason');
sleep 3;
while (<$chan>)
{
chomp;
next if (/10 - slot_0 primary control station/);
if ($_ =~ /contacted$/)
{
print $mailfh "DM is OK: $_\n";
}
else
{
print $mailfh "POSSIBLE DM FAILURE:Please check $ipname ($ipaddr): $_ POSSIBLE DM FAILURE:\n";
}
};
$chan->close();
}
close $mailfh;
my $nasmailmsg = MIME::Lite->new(
From =>'name@email.com',
To => 'name@email.com',
Subject => "Automated Check For NAS DataMover Health.",
Type => 'Multipart/mixed',
);
$nasmailmsg->attach (
Type => 'TEXT',
Path => "dmcheck.txt",
Filename => "dmcheck.txt",
Disposition => 'inline',
);
$nasmailmsg->send;
system "del dmcheck.txt";
print "$0 execution completed.Please check your mailbox for Mail Titled - \n\"Automated Check For NAS DataMover Health.\"\n";
}
</code>
<p> And here is the text file called "nas_array_ip_list.txt" that contains the IPs.</P>
<code>
###############################################################
#
#Lines beginning with "#" will be ignored by the script.
#
#This file contains the NAS IPs.
#
#Please use tab or space to seperate the name and IP of #the NAS Arrays.
#
###########################################################
NASBOX1 127.0.0.1
NASBOX2 127.0.0.2
NASBOX3 127.0.0.3
NASBOX4 127.0.0.4
</code>
<p>
I intend to tweak it to perfection so that later some time, I can put this under "Cool Uses For Perl"</P>
<p> On another note, wanted to thank the PerlMonks and the authors of Learning Perl. This site and the book have been instrumental in getting me to a level where I could write a script like this. This script is already running in our production environment (which is why I had to fill the text file with localhost IPs as I couldnt share the actual IPs), and that feeling is really awesome. Just want to take the script to the next level. </p>
<div class="pmsig"><div class="pmsig-941553">
<P>Perlpetually Indebted To PerlMonks</P>
<P>Win 7 at Work. Living through it....Linux at home. Loving it.</P>
</div></div>