简体   繁体   English

读取远程主机上的文件

[英]Reading a file on a remote host

My code involves going to a host(doing with openSSH), getting the list of files matching a pattern(doing using remote find command-OpenSSH) and then opening each file I have got and processing each(grepping etc). 我的代码涉及到主机(使用openSSH),获取与模式匹配的文件列表(使用远程查找命令-OpenSSH),然后打开我得到的每个文件并处理每个文件(抓取等)。 I am done with getting the file names and passing to a function. 我已经完成了获取文件名并传递给函数的工作。 Now, I have to pass these filename to a function where I open each and process it. 现在,我必须将这些文件名传递给一个函数,在其中打开每个文件名并进行处理。 I am trying to do it using File::Remote as follows: 我正在尝试使用File :: Remote进行如下操作:

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...

But I am unable to open the file on the host an I am getting an error. 但是我无法在主机上打开文件,但出现错误。

When performing remote actions in batch, the first principle is to reduce the number of ssh connections (ideally only one). 批量执行远程操作时,第一个原则是减少ssh连接的数量(理想情况下只有一个)。 Using shell or perl one liner, you can do very complex actions. 使用shell或perl一种衬垫,您可以执行非常复杂的操作。 Here is my understanding of your need: 这是我对您的需求的理解:

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

You can pipe further processing in the same line. 您可以在同一行中传递进一步的处理。 If you want to process the files locally, you can transfer all the files in a single command. 如果要在本地处理文件,则可以在单个命令中传输所有文件。 Here is a full example: 这是一个完整的示例:

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";
}

The File::Remote module is not part of a standard Perl installation. File :: Remote模块不是标准Perl安装的一部分。 If you want to use this module, then you need to install it. 如果要使用此模块,则需要安装它。

How you install it will depend on what platform you're using and how you have installed Perl. 如何安装它取决于您使用的平台以及Perl的安装方式。 On a Linux installation where you are using the system Perl you could try sudo yum install perl-File-Remote (on a RedHat-based system) or sudo apt-get install libfile-remote-perl (on a Debian/Ubuntu-based system). 在使用系统Perl的Linux安装中,您可以尝试sudo yum install perl-File-Remote (在基于RedHat的系统上)或sudo apt-get install libfile-remote-perl (在基于Debian / Ubuntu的系统上) )。 You could also try using cpan or cpanm to install it. 您也可以尝试使用cpancpanm进行安装。

I'd also suggest that as this module was last updated in 2005, it's quite likely that it is unmaintained, so I would be wary of using it. 我还建议,由于该模块的最新更新时间是2005年,因此很有可能不需要维护该模块,因此我会谨慎使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM