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

Hi Monks, I am working on a script to automate health check of EMC VNX box in this i am using command nas_checkup to get the output . Problem I am facing right now this command takes 10 mins to complete health check OUTPUT .But this script is returing first 2 lines of output in just seconds.Need help!

#!/usr/bin/perl use Net::SSH2; use warnings; use strict; use diagnostics; my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(t +ime); my $datestring = sprintf("%4d_%02d_%02d_%02d",($year + 1900),($mon+1), +$mday,$hour); my $sw; use constant BUFLEN => 50_0000 ; my $user = ""; my $password = ""; my $buf; my $channel; my $command; my $read; #get ip’s #open file open(FILE, "c:/scripts/ips2.txt") or die("Unable to open file"); # read file into an array my @data = <FILE>; # print output to file open OUTPUT, '+>', "EMC-$datestring.txt" or die $!; # create / open th +e out.txt for STDOUT redirection open ERROR, '+>', "err-$datestring.txt" or die $!; # create / open t +he err.txt for errors redirection STDOUT->fdopen( \*OUTPUT, 'w' ) or die $!; # redirecting the STDOUT to + file STDERR->fdopen( \*ERROR, 'w' ) or die $!; # redirecting the errors to + file foreach $sw (@data) { my $ssh = Net::SSH2->new(); if(!$ssh->connect($sw)){ print("Connection Failed\n"); exit(1); } if(!$ssh->auth_password($user,$password)){ print("Authentication Failed"); exit(1); } print "############################################################### +######################\n"; print "Status report of EMC VNX with IP:$sw \n"; print "############################################################### +######################\n"; # Printing Hardware print "Health Check \n"; { $buf =""; my $channel = $ssh->channel(); my $command = $channel->exec("/nas/bin/nas_checkup"); my $read = $channel->read($buf, BUFLEN ); warn 'More than', BUFLEN, 'characters in listing' if $read >= BUFLEN; print "$buf \n"; } print "############################################################### +######################\n"; print "END of Report \n"; print "############################################################### +######################\n"; }

Output , I am getting right now ================================

##################################################################################### Status report of EMC VNX with IP:10.193.140.139 ##################################################################################### Health Check Check Version: 8.1.9.217 Check Command: /nas/bin/nas_checkup ##################################################################################### END of Report #####################################################################################

Replies are listed 'Best First'.
Re: EMC VNX SCRIPT
by thanos1983 (Parson) on Aug 08, 2018 at 11:59 UTC

    Hello atulperl,

    Welcome to the monastery. I have update a bit your code with some closing file calls and also some minor modifications chomp array etc. On my system this script runs under a second. Also I would suggest to use ssh key authentication instead of user name and password for many reason but mainly for security.

    Can you check how long your commands needs to be executed?

    I think the reason that this delay is observed on your call because of the huge buffer that you set.

    If for some reason this sample script does not work post your problem and I will try to assist more.

    Update: I would also strongly suggest to read this small tutorial regarding the module from the forum see here: A little demo for Net::SSH2

    Update2: Alternative solution is to use method read2 that captures stdout and stderr. Sample of code:

    Update3: Alternative solution is to use module IO::All which will reduce your code significantly in reading/writing files etc. Sample of code:

    Hope this helps, BR.

    Seeking for Perl wisdom...on the process of learning...not there...yet!

      Hi Thanos, Thanks for the reply

      Issue with my script was not the time , but my issue is nas_Checkup command takes 15 min normally .But my script is not waiting that much minute to print the output in file. I want my script to wait till command fully runs.

        Hello again atulperl,

        Unfortunately I can not replicate your problem. Can you give it a try on the sample of code Update3 I think this is the best approach of using Net::SSH2::Channel::read2 method, capable of capturing STDOUT and STDERR.

        If this fails can you provide the output what is the error code that is failing?

        P.S. why specifically this module Net::SSH2 and not any other SSH module available e.g Net::OpenSSH?

        Hope this helps, BR.

        Seeking for Perl wisdom...on the process of learning...not there...yet!