2017-04-13 135 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年最後更新的,所以很可能它沒有維護,所以我會謹慎使用它。