<?xml version="1.0" encoding="windows-1252"?>
<node id="1013260" title="Seeking guidance for more idiomatic way of (re)writing this script." created="2013-01-14 14:03:29" updated="2013-01-14 14:03:29">
<type id="115">
perlquestion</type>
<author id="941553">
perl514</author>
<data>
<field name="doctext">
&lt;p&gt;Respected Monks &lt;/p&gt; 
&lt;p&gt; 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. &lt;/p&gt;
&lt;p&gt; Here is the script&lt;/p&gt;


&lt;code&gt; 
#!/usr/bin/perl
use warnings;
use strict;
use Net::SSH2;
use MIME::Lite;
MIME::Lite-&gt;send ("smtp", "mail.server.com");

if (!open my $fh , "&lt;" , "C:/path/to/nas_array_ip_list.txt")
{
    my $msg = MIME::Lite-&gt;new
	      (
	      From    =&gt; 'name@email.com',
	      To      =&gt; 'name@email.com',
	      Data    =&gt; "Error:nas_array_ip_list.txt:$!.$^E\n",
	      Subject =&gt; "IP List File - nas_array_ip_list.txt - Not Found For $0 Script on $ENV{COMPUTERNAME}\n",
	      );

$msg-&gt;send ();
    
}
else {
	print "Please wait. $0 script is being executed...\n";
	open my $mailfh, "&gt;", "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 (&lt;$fh&gt;)
	{
	    next if (/^#/);
	    
	    my ($ipname, $ipaddr) = split /\t+|\s+/;
	    
	    my $username = 'username';
	    my $password = 'password';
	    
	    my $ssh2 = Net::SSH2-&gt;new();
	    
	    
	    print $mailfh "\n----------------------------";
	    
	    print $mailfh "\nData Mover Check For $ipname ($ipaddr)\n";
	    
	    print $mailfh "----------------------------\n";
	    $ssh2-&gt;connect("$ipaddr") || die "PROBELM -$!";
	    
	    $ssh2-&gt;auth_password("$username","$password") || die "Username/Password not right";
	    
	    my $chan = $ssh2-&gt;channel();
	    
	    
	    $chan-&gt;blocking(0);	
	    $chan-&gt;exec('/nas/sbin/getreason');
	    sleep 3;
	    
	    while (&lt;$chan&gt;)
	    {
		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-&gt;close();
	     
	}
	
	close $mailfh;
	my $nasmailmsg = MIME::Lite-&gt;new(
		From		=&gt;'name@email.com',
		To 		=&gt; 'name@email.com',
		Subject 	=&gt; "Automated Check For NAS DataMover Health.",
		Type	=&gt; 'Multipart/mixed',
					    
					);
	    
	    $nasmailmsg-&gt;attach (
		Type		=&gt; 'TEXT',
		Path		=&gt; "dmcheck.txt",
		Filename	=&gt; "dmcheck.txt",
		Disposition	=&gt; 'inline',
				);
	    $nasmailmsg-&gt;send;
	    system "del dmcheck.txt";
	   					
    	print "$0 execution completed.Please check your mailbox for Mail Titled - \n\"Automated Check For NAS DataMover Health.\"\n";
	
}
&lt;/code&gt;
&lt;p&gt; And here is the text file called "nas_array_ip_list.txt" that contains the IPs.&lt;/P&gt;
&lt;code&gt;
###############################################################
#
#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
&lt;/code&gt;
&lt;p&gt;
I intend to tweak it to perfection so that later some time, I can put this under "Cool Uses For Perl"&lt;/P&gt;
&lt;p&gt; 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. &lt;/p&gt;

&lt;div class="pmsig"&gt;&lt;div class="pmsig-941553"&gt;
&lt;P&gt;Perlpetually Indebted To PerlMonks&lt;/P&gt;
&lt;P&gt;Win 7 at Work. Living through it....Linux  at home. Loving it.&lt;/P&gt;


&lt;/div&gt;&lt;/div&gt;</field>
</data>
</node>
