1

I have a scenario where i need to list all the directories under msystem on a remote machine which contains a log.txt.If it is found then get the list using ll command from the msystem directory file.How can achieve this this is the directory structure

   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";
     }
  }
Rajeev
  • 44,985
  • 76
  • 186
  • 285
  • What is wrong with the code above? Does it error out? Not produce desired results? – DVK Mar 07 '12 at 05:53
  • How would you do it on a local system with `find`? – Jim Garrison Mar 07 '12 at 08:30
  • maybe you can refer http://stackoverflow.com/questions/2282686/how-to-echo-directories-containing-matching-file-with-bash question for command –  Mar 07 '12 at 11:02

2 Answers2

2

You can also do that using 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;

Though, running find in the remote host is going to be faster.

salva
  • 9,943
  • 4
  • 29
  • 57
1

Use find with exec.

Simply:

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



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

 }
user1126070
  • 5,059
  • 1
  • 16
  • 15