如何使用Perl检查远程服务器上的文件是否存在?

6

如何使用Perl检查远程服务器上的文件是否存在?

在使用Perl模块Net::FTP的同时,我可以这样做吗?

检查文件是否存在

if (-e $file_check) {  
    print "File Exists!\n";  
}   
else {  
    print "File Doesn't Exist!\n";  
}  
5个回答

6
您最好使用SSH来完成此操作:
#!/usr/bin/perl

use strict;
use warnings;

my $ssh  = "/usr/bin/ssh";
my $host = "localhost";
my $test = "/usr/bin/test";
my $file = shift;

system $ssh, $host, $test, "-e", $file;
my $rc = $? >> 8;
if ($rc) {
    print "file $file doesn't exist on $host\n";
} else {
    print "file $file exists on $host\n";
}

但是如果它是一个受密码保护的远程服务器,该怎么办? - Jithin
@Jithin 这就是 SSH 密钥的作用。 - Chas. Owens
是的,我明白了。但是如果远程服务器受到密码保护,脚本中是否有任何解决方法? - Jithin
@Jithin 是的,你可以尝试使用Net::SSH::Expect,但这样做是疯狂的。所有正确配置的机器都将接受密钥,它们更加安全。 - Chas. Owens
谢谢您的回复。我只是好奇,密码是否可以直接使用系统命令而不使用任何模块来提供..;-) - Jithin
@Jithin ssh 命令行程序的设计是不允许这样做的(由于安全问题)。 - Chas. Owens

4
您可以使用下面的命令: ```shell ```
use Net::FTP;
$ftp->new(url);
$ftp->login(usr,pass);

$directoryToCheck = "foo";

unless ($ftp->cwd($directoryToCheck))
{
   print "Directory doesn't exist
}

1

登录FTP服务器,查看您是否可以在所关心的文件上获取FTP SIZE

#!/usr/bin/env perl

use strict;
use warnings;

use Net::FTP;
use URI;

# ftp_file_exists('ftp://host/path')
#
# Return true if FTP URI points to an accessible, plain file.
# (May die on error, return false on inaccessible files, doesn't handle
# directories, and has hardcoded credentials.)
#
sub ftp_file_exists {
    my $uri = URI->new(shift); # Parse ftp:// into URI object

    my $ftp = Net::FTP->new($uri->host) or die "Connection error($uri): $@";
    $ftp->login('anonymous', 'anon@ftp.invalid') or die "Login error", $ftp->message;
    my $exists = defined $ftp->size($uri->path);
    $ftp->quit;

    return $exists;
}

for my $uri (@ARGV) {
    print "$uri: ", (ftp_file_exists($uri) ? "yes" : "no"), "\n";
}

1
如果文件在远程服务器的FTP空间中,则使用Net::FTP。获取目录的ls列表,查看您的文件是否在其中。
但是,您不能只是去查看服务器上的任意文件。想想那将是一个安全问题。

0
你可以使用expect脚本来实现同样的目的(不需要额外的模块)。expect将在FTP服务器上执行“ls -l”,perl脚本将解析输出并决定文件是否存在。这非常容易实现。
以下是代码,
PERL脚本:(main.pl)
# ftpLog variable stores output of the expect script which logs in to FTP server and runs "ls -l" command
$fileName = "myFile.txt";
$ftpLog = `/usr/local/bin/expect /path/to/expect_script/ftp_chk.exp $ftpIP $ftpUser $ftpPass $ftpPath`;

# verify that file exists on FTP server by looking for filename in "ls -l" output
if(index($ftpLog,$fileName) > -1)
{
    print "File exists!";
}
else
{
    print "File does not exist.";
}

EXPECT脚本:(ftp_chk.exp)

#!/usr/bin/expect -f

set force_conservative 0;
set timeout 30
set ftpIP [lindex $argv 0]
set ftpUser [lindex $argv 1]
set ftpPass [lindex $argv 2]
set ftpPath [lindex $argv 3]

spawn ftp $ftpIP

expect "Name ("
send "$ftpUser\r"

sleep 2

expect {
"assword:" {
    send "$ftpPass\r"
    sleep 2

    expect "ftp>"
    send "cd $ftpPath\r\n"
    sleep 2

    expect "ftp>"
    send "ls -l\r\n"
    sleep 2

    exit
    }
"yes/no)?" {
    send "yes\r"
    sleep 2
    exp_continue
    }
timeout {
    puts "\nError: ftp timed out.\n"
    exit
    }
}

我在我的一个工具中使用了这个设置,我可以保证它完美地工作 :)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接