0

我有一个现有程序,它采用目录路径并递归处理 tis 目录及其文件。我需要更改此代码以使用远程系统上的目录,例如流程如下:

---这里我根据需要获取远程目录的文件名,以便从unix系统访问

----执行进程目录

---sub process_directory #获取目录路径

4

2 回答 2

0

我觉得有必要提到的第一件事是,所有这些都应该通过 ssh/scp 而不是 rsh/rcp。也许这不是你现在的选择,但如果我不在这里至少说点什么的话,我今晚就无法入睡。:-) 话虽如此,无论您使用哪种方式,其余部分都同样适用。

在没有很多其他细节的情况下,我将在这里进行一个半受过教育的猜测。根据您的限制,我可以看到您正在执行的操作有两种可能的操作模式。

第一种可能性是您的脚本可能被“运送”到有问题的远程主机。在这种情况下,列出您要覆盖的所有主机,然后:

bash$ for this_host in `cat file_of_hosts`; do
> rcp existing_script $this_host:/wherever/you/want/it/to/live
> rsh $this_host '/wherever/you/want/it/to/live/existing_script /remote/target/directory'
> done

如果不是,那么它会变得有点混乱(警告,未经测试,ymmv,警告程序员等)......

#!/usr/bin/perl

# or change this to iterate over a file full of host/path pairs
my ($RemoteHost, $TargetDir) = @ARGV;
my $Temp = '/tmp';

sub ProcessRemoteDir {
    my ($host, $path) = @_;
    # the command is "ell ess space minus one capital eff"
    my $listCommand = "rsh $host 'ls -1F $path'";
    my @dirEntries = qx{ $listCommand };
    chomp(@dirEntries);

    foreach my $item (@dirEntries) {
         # If it ends in "/"
         if ($item =~ m/\/\Z/) {
              my $subDirectory = $path . "/" . $item;
              ProcessRemoteDir($host, $subDirectory);
         }
         # If it *isn't* a symlink, pipe, or socket
         elsif ($item !~ /(\@|\||\=)\Z/) {
              # in case one of the file's 'x' bits is set
              $item =~ s/\*\Z//;
              my $localCopy  = $Temp . "/" . $item;
              my $remoteCopy = $host . ":" . $path . "/" . $item;

              my $fetchCommand = "rcp $remoteCopy $localCopy";
              system($fetchCommand);

              # Your current file processing logic should go here, operating on $localCopy

              my $putCommand = "rcp $localCopy $remoteCopy";
              system($putCommand);
              unlink $localCopy;
         }
   }
}

ProcessRemoteDir($RemoteHost, $TargetDir);

__END__

这是深度优先;不确定这对你是否重要。另请注意,为简洁起见,我几乎省略了所有安全检查。想到的一些是检查 system() 和 qx{} 调用的返回,确保 $localCopy 不是零字节文件,更改 $listCommand(和 @dirEntries 的处理)以检查文件大小和权限并确保它们在此上下文和环境中有意义的任何限制内(并检查 $remoteCopy 和 $localCopy 以相同的大小开始),确保远程文件中没有任何嵌入的“非标准”字符/directory 名称可能最终会与构造的命令以及任何数量的其他命令混淆。在扣动扳机之前,请考虑一下它将在哪里运行以及在哪些类型的文件上运行。

如果您填写有关您要执行的操作的更多详细信息,我可能会更接近您的需要。不过,这有望帮助您入门。:-)

于 2011-06-11T01:31:56.103 回答
0

不知道你真正想要什么,但你可以:

  • 安装保险丝
  • 通过 sshfs 挂载远程目录
  • 在已安装的目录上使用您的脚本 - (脚本会认为它是本地目录)
于 2011-06-10T20:41:22.777 回答