#!/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 22 update if you use other port $ssh2->auth( publickey => "/home/user/.ssh/id_rsa"); # ssh key authentication, 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 #### #!/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 #### #!/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