【问题标题】:Perl find file on remote machinePerl 在远程机器上查找文件
【发布时间】:2012-03-24 16:19:21
【问题描述】:

我有一个场景,我需要在包含 log.txt 的远程计算机上列出 msystem 下的所有目录。如果找到,则使用 msystem 目录文件中的ll command 获取列表。如何实现这一点 这是目录结构

   msystem
     dir1 dir2/info/log.txt dir3/ dir4/info/log.txt


  my $ssh = Net::SSH::Perl->new($hostname, protocol => '1,2', debug => 0, interactive => 1);
  $ssh->login($username, $password);
  ($stdout,$stderr,$exit) = $ssh->cmd("$check_lock_file");
  if((defined $stderr) && ($stderr =~ /No such file or directory/))
  {
     ($stdout,$stderr,$exit) = $ssh->cmd("What command to be used and get the ouput");
     if((defined $stderr) && ($stderr =~ /No such file or directory/))
     { 
                  print ""Error;
                  print "$stderr"; 
                   exit; 
     } 
     elsif($exit eq '0')
     { 
            print "dir2 dir4";
     }
  }

【问题讨论】:

  • 上面的代码有什么问题?它会出错吗?没有产生预期的结果?
  • 你会如何在本地系统上使用find
  • 也许你可以参考stackoverflow.com/questions/2282686/…命令问题

标签: perl


【解决方案1】:

将 find 与 exec 一起使用。

简单地说:

...$ssh->cmd("find mysystem/ -name "log.txt" -exec ls -la {} \\;");



 elsif($exit eq '0')
 { 
        foreach my $line (split(/\n/,$stdout)){
           print $line."\n";
        }

 }

【讨论】:

    【解决方案2】:

    您也可以使用 SFTP 做到这一点:

    use Net::SFTP::Foreign;
    my $sftp = Net::SFTP::Foreign->new($hostname,
                                       user => $user, password => $password);
    my @files = $sftp->find('/path/to/mysystem',
                            wanted => qr{^(?:.*/)?log\.txt$});
    print "$_->{longname}\n" for @files;
    

    不过,在远程主机上运行find 会更快。

    【讨论】:

      猜你喜欢
      • 2012-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-19
      • 2012-09-30
      • 1970-01-01
      相关资源
      最近更新 更多