#!/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