Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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.


In reply to Seeking guidance for more idiomatic way of (re)writing this script. by perl514

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • 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.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (6)
As of 2024-04-19 06:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found