2010-06-28 21 views

Respuesta

1

Si el archivo está en el espacio FTP en el servidor remoto, a continuación, utilizar Net :: FTP. Obtenga una lista ls del directorio y vea si su archivo está ahí.

Pero no puedes ir y ver si hay algún archivo arbitrario en el servidor. Piensa en lo que sería un problema de seguridad.

6

Es posible que se sirve mejor a través de SSH para hacer esto:

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

pero ¿qué hacer si es un servidor remoto que está protegido por contraseña? – Jithin

+0

@Jithin Para eso están las claves ssh. –

+0

Sí, lo tengo. Pero, ¿hay alguna solución en el script, si el servidor remoto está protegido con contraseña? – Jithin

1

Acceder al servidor FTP, y ver si puede obtener un FTP SIZE en el archivo que se preocupan por:

#!/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): [email protected]"; 
    $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"; 
} 
+0

¿Funcionará eso en un archivo de tamaño cero? – Schwern

+0

@Schwern, gracias y "no", pero el código revisado lo hará. – pilcrow

4

Se puede usar un comando como:

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

$directoryToCheck = "foo"; 

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

Se podría utilizar un script de espera para el mismo propósito (no requiere módulos adicionales). El esperado ejecutará "ls -l" en el servidor FTP y el script perl analizará el resultado y decidirá si el archivo existe. Es realmente simple de implementar.

Aquí está el código,

script en 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."; 
} 

ESPERAR guión: (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 
    } 
} 

He utilizado esta configuración en una de mis herramientas y yo podemos garantizar que funciona perfectamente :)

Cuestiones relacionadas