2017-04-13 131 views
0

我的代码涉及到一个主机(使用openSSH),获取匹配模式的文件列表(使用远程查找命令OpenSSH),然后打开每个文件并获得并处理(grepping等)。 我已完成获取文件名并传递给函数。现在,我必须将这些文件名传递给我打开并处理它的函数。我试图用文件如下做到这一点::远程:在远程主机上读取文件

sub processFiles{ 
    my $fileList =shift; 
#Iterate over the file and try to find start and stop time stamps from the file 
for my $file (@{$fileList}) { 
#finding start time of file:its found in lines of file 
my $HEAD; 
open $HEAD, "host:head -1000 $File|" or die "Unable to check file for starting time"; 
while (<$HEAD>) { 
#process... 

但我无法打开主机上的文件中,我得到一个错误。

+0

你有什么问题? – AbhiNickz

+0

它说找不到File/Remote.pm – emma

+0

你配置了File :: remote perl模块吗? – RAFA

回答

0

当批量执行远程操作时,第一个原则是减少ssh连接的数量(理想情况下只有一个)。使用shell或perl一个班轮,你可以做非常复杂的行动。这里是我对你的需求的理解:

ssh hostname 'find dirname -name \*.pl| xargs grep -l pattern /dev/null' 

你可以在同一行管道进一步处理。如果您想在本地处理文件,则可以通过单个命令传输所有文件。这是一个完整的例子:

use Archive::Tar; 

open my $file, '-|', 'ssh -C hostname "find dirname -name \*.pl | xargs grep -l pattern /dev/null | xargs tar c"' 
    or die "Can't pipe: $!"; 

my $tar = Archive::Tar->new($file); 
foreach my $file ($tar->get_files()) { 
    print $file->name, ', ', $file->size, "\n"; 
} 
0

文件::远程模块不是Perl标准安装的一部分。如果你想使用这个模块,那么你需要安装它。

如何安装它取决于您使用的平台以及安装Perl的方式。在您使用系统Perl的Linux安装中,您可以尝试sudo yum install perl-File-Remote(在基于RedHat的系统上)或sudo apt-get install libfile-remote-perl(在基于Debian/Ubuntu的系统上)。您也可以尝试使用cpancpanm进行安装。

我还建议,因为这个模块是在2005年最后更新的,所以很可能它没有维护,所以我会谨慎使用它。