Luke
Luke

Reputation: 317

How can I check if a file exists on a remote server using Perl?

How can I check if a file exists on a remote server using Perl?

Can I do this while using the Perl module Net::FTP?

CHECK TO SEE IF FILE EXISTS

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

Upvotes: 6

Views: 15028

Answers (5)

Jenson Jose
Jenson Jose

Reputation: 532

You could use an expect script for the same purpose (requires no extra modules). The expect will execute "ls -l" on the FTP server and the perl script will parse the output and decide if file exists. Its really simple to implement.

Here's the code,

PERL script: (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 script: (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
    }
}

I have used this setup in one of my tools and I can guarantee that it works perfectly :)

Upvotes: 0

claydiffrient
claydiffrient

Reputation: 1306

You could use a command such as:

use Net::FTP;
$ftp->new(url);
$ftp->login(usr,pass);

$directoryToCheck = "foo";

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

Upvotes: 4

pilcrow
pilcrow

Reputation: 58589

Log in to the FTP server, and see if you can get an FTP SIZE on the file you care about:

#!/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', '[email protected]') 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";
}

Upvotes: 1

Chas. Owens
Chas. Owens

Reputation: 64929

You might be best served by using SSH to do this:

#!/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";
}

Upvotes: 6

Andy Lester
Andy Lester

Reputation: 93676

If the file is in the FTP space on the remote server, then use Net::FTP. Get an ls listing of the directory and see if your file is in there.

But you can't just go and see if any arbitrary file is on the server. Think of what a security problem that would be.

Upvotes: 1

Related Questions