http://www.perlmonks.org?node_id=1013260

perl514 has asked for the wisdom of the Perl Monks concerning the following question:

Respected Monks

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.

Here is the script

#!/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{COMPUT +ERNAME}\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 Ma +il Titled - \n\"Automated Check For NAS DataMover Health.\"\n"; }

And here is the text file called "nas_array_ip_list.txt" that contains the IPs.

############################################################### # #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 Array +s. # ########################################################### NASBOX1 127.0.0.1 NASBOX2 127.0.0.2 NASBOX3 127.0.0.3 NASBOX4 127.0.0.4

I intend to tweak it to perfection so that later some time, I can put this under "Cool Uses For Perl"

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.

Perlpetually Indebted To PerlMonks

Win 7 at Work. Living through it....Linux at home. Loving it.