I am making a script in Perl using the File::Remote module. The script needs to parse through several files in several hosts (let’s say about 3 files per host). However, every time I open a file, the script connects to the host via SSH. My code looks like the following:
my $secure = new File::Remote(rsh => '/usr/bin/ssh', rcp => '/usr/bin/scp');
foreach $host (@hostList)
{
for ($number = 1; $number < 4; $number++)
{
my $logFile = "log_$number.log";
$secure->open(HANDLE, "$host:/home/$logFile") or die;
...
}
}
So if I have 4 hosts in the @hostList array, the script would attempt 12 SSH connections (the total number of files to parse through). However I found this to be inefficient and I was wondering if I can make it so that it needs to perform an SSH connection only once per host or SSH session (so in this case, a total of 4 SSH connections). Does such an option exist?
(assumes remote system has
cat, filenames have no spaces or other shell metacharacters, etc.)