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.

#!/usr/bin/perl use strict; use warnings; use Net::SSH2; use Date::Manip; my $datestr = ParseDate("now"); my $dateHumanReadable = UnixDate($datestr, "Date: %T on %b %e, %Y."); #get ip’s #open file my $ipFile = "ips2.txt"; open(my $FH, '<', $ipFile) or die ("Unable to open file '".$ipFile."': $!"); # read file into an array chomp(my @data = <$FH>); close $FH or warn "Unable to close file '".$ipFile."': $!"; my $ssh2 = Net::SSH2->new(); foreach my $sw (@data) { $ssh2->connect($sw, 22) or $ssh2->die_with_error; # port default 2 +2 update if you use other port $ssh2->auth( publickey => "/home/user/.ssh/id_rsa"); # ssh key aut +hentication, update for user and password my $chan = $ssh2->channel() or $ssh2->die_with_error; $chan->blocking(1); $chan->exec("ls -la /home/user/Monks/Foo") or $ssh2->die_with_error; my $stdoutFileName = "stdout-$sw.txt"; open(my $fhStdOut, '>', $stdoutFileName) or die "Could not open file '".$stdoutFileName."' $!"; print $fhStdOut $dateHumanReadable; while (<$chan>){ print $fhStdOut $_; } close $fhStdOut or warn "Unable to close file '".$stdoutFileName."': $!"; my $stderrFileName = "stderr-$sw.txt"; open(my $fhStdErr, '>', $stderrFileName) or die "Could not open file '".$stderrFileName."' $!"; print $fhStdErr "exit status: " . $chan->exit_status . "\n"; close $fhStdErr or warn "Unable to close file '".$stderrFileName."': $!"; $ssh2->disconnect(); } __END__ $ time perl test.pl real 0m0.368s user 0m0.127s sys 0m0.008s $ cat stdout-127.0.0.1.txt Date: 13:45:44 on Aug 8, 2018.total 16 drwxrwxr-x 2 user user 4096 Feb 28 21:10 . drwxrwxr-x 14 user user 4096 Aug 8 13:45 .. -rw-rw-r-- 1 user user 263 Feb 28 21:10 Bar.pm -rw-rw-r-- 1 user user 184 Jan 30 2018 Bar.pm~ $ cat stderr-127.0.0.1.txt exit status: 0

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:

#!/usr/bin/perl use strict; use warnings; use Net::SSH2; use Date::Manip; my $datestr = ParseDate("now"); my $dateHumanReadable = UnixDate($datestr, "Date: %T on %b %e, %Y."); #get ip’s #open file my $ipFile = "ips2.txt"; open(my $FH, '<', $ipFile) or die ("Unable to open file '".$ipFile."': $!"); # read file into an array chomp(my @data = <$FH>); close $FH or warn "Unable to close file '".$ipFile."': $!"; my $ssh2 = Net::SSH2->new(); foreach my $sw (@data) { $ssh2->connect($sw, 22) or $ssh2->die_with_error; $ssh2->auth( publickey => "/home/user/.ssh/id_rsa"); my $chan = $ssh2->channel() or $ssh2->die_with_error; $chan->blocking(1); $chan->exec("ls -la /home/user/Monks/Foo") or $ssh2->die_with_error; my ($out, $err) = ('', ''); while (!$chan->eof) { if (my ($o, $e) = $chan->read2) { $out .= $o; $err .= $e; } else { $ssh2->die_with_error; } } my $stdoutFileName = "stdout-$sw.txt"; open(my $fhStdOut, '>', $stdoutFileName) or die "Could not open file '".$stdoutFileName."' $!"; print $fhStdOut "$dateHumanReadable\n\nSTDOUT:\n\n$out"; close $fhStdOut or warn "Unable to close file '".$stdoutFileName."': $!"; my $stderrFileName = "stderr-$sw.txt"; open(my $fhStdErr, '>', $stderrFileName) or die "Could not open file '".$stderrFileName."' $!"; print $fhStdErr "STDERR:\n\n$err" . "exit status: " . $chan->exit_ +status . "\n"; close $fhStdErr or warn "Unable to close file '".$stderrFileName."': $!"; $ssh2->disconnect(); } __END__ $ time perl test.pl real 0m0.389s user 0m0.127s sys 0m0.028s $ cat stdout-127.0.0.1.txt Date: 14:14:30 on Aug 8, 2018. STDOUT: total 16 drwxrwxr-x 2 user user 4096 Feb 28 21:10 . drwxrwxr-x 14 user user 4096 Aug 8 14:14 .. -rw-rw-r-- 1 user user 263 Feb 28 21:10 Bar.pm -rw-rw-r-- 1 user user 184 Jan 30 2018 Bar.pm~ $ cat stderr-127.0.0.1.txt STDERR: exit status: 0

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

#!/usr/bin/perl use strict; use IO::All; use warnings; use Net::SSH2; use Date::Manip; my $datestr = ParseDate("now"); my $dateHumanReadable = UnixDate($datestr, "Date: %T on %b %e, %Y."); #get ip’s #open file my @data = io("ips2.txt")->chomp->slurp; my $ssh2 = Net::SSH2->new(); foreach my $sw (@data) { $ssh2->connect($sw, 22) or $ssh2->die_with_error; $ssh2->auth( publickey => "/home/user/.ssh/id_rsa"); my $chan = $ssh2->channel() or $ssh2->die_with_error; $chan->blocking(1); $chan->exec("ls -la /home/user/Monks/Foo") or $ssh2->die_with_error; my ($out, $err) = ('', ''); while (!$chan->eof) { if (my ($o, $e) = $chan->read2) { $out .= $o; $err .= $e; } else { $ssh2->die_with_error; } } io("stdout-$sw.txt")->print("$dateHumanReadable\n\nSTDOUT:\n$out") +; io("stderr-$sw.txt")->print("STDERR:\n$err" . "exit status: " . $chan->exit_status . "\n"); $ssh2->disconnect(); } __END__ $ time perl test.pl real 0m0.389s user 0m0.127s sys 0m0.028s $ cat stdout-127.0.0.1.txt Date: 14:14:30 on Aug 8, 2018. STDOUT: total 16 drwxrwxr-x 2 user user 4096 Feb 28 21:10 . drwxrwxr-x 14 user user 4096 Aug 8 14:14 .. -rw-rw-r-- 1 user user 263 Feb 28 21:10 Bar.pm -rw-rw-r-- 1 user user 184 Jan 30 2018 Bar.pm~ $ cat stderr-127.0.0.1.txt STDERR: exit status: 0

Hope this helps, BR.

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

In reply to Re: EMC VNX SCRIPT by thanos1983
in thread EMC VNX SCRIPT by atulperl

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.