2012-03-07 76 views
1

我有一個場景,我需要列出包含log.txt的遠程計算機上的msystem下的所有目錄。如果找到,然後從msystem目錄文件中使用ll command獲取列表。如何實現這一點 這是目錄結構在遠程計算機上查找Perl文件

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

上面的代碼有什麼問題?它錯誤嗎?不會產生期望的結果? – DVK 2012-03-07 05:53:24

+0

如何在具有'find'的本地系統上執行此操作? – 2012-03-07 08:30:36

+0

也許你可以參考http://stackoverflow.com/questions/2282686/how-to-echo-directories-containing-matching-file-with-bash問題的命令 – 2012-03-07 11:02:55

回答

1

使用查找與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將會更快。

相關問題