Perl Net :: SSH检查文件是否存在

时间:2012-08-22 08:51:14

标签: perl

我需要使用Net :: SSH远程检索文件,但如果文件远程不存在,我的脚本就会死掉。我怎么能阻止这个并让脚本继续下去?

eval {  
    my $login_output = $ssh->login($user, $pass);

    foreach $device (keys %{$deviceHashRef})
    {   
                    my $transfer_output = $scpe->scp("$host:/home/portal/runtime/portal_daemon/data/journal/2012.07/refrigeration.case.$device.hourly", '/tmp/poop/input');
    }
};

if($@)
{       
    print Dumper $@;
}

谢谢,

汤姆

1 个答案:

答案 0 :(得分:1)

如果将调用包装在eval {}中,它将捕获die(),并且您可以处理错误。

 my $transfer_output = eval { $scpe->scp("$host:/home/portal/runtime/portal_daemon/data/journal/2012.07/refrigeration.case.$device.hourly", '/tmp/poop/input') };
 if ($@) {
   my $err = $@;

   # you can parse the error message out of $err and build an error message
   if ($err =~ /not found/) {
      logger->err('the input file was not found on the remote server');
   }
   else {
     logger->err("file copy encountered a problem: $err");
   }
 }