2011-10-16 2 views
1

내 프로그램이 작동하지 않는 이유는 무엇입니까? 그것은 호스트에 연결을 거부, 나는 두 개의 다른 서버를 시도하고 어떤 포트가 사용되었는지 확인했습니다. Perl에 관해서는별로 경험이 없습니다.Perl로 작성된 FTP 응용 프로그램이 연결되지 않습니다.

use strict; 
use Net::FTP; 
use warnings; 

my $num_args = $#ARGV+1; 
my $filename; 
my $port; 
my $host; 
my $ftp; 



if($num_args < 2) 
{ 
    print "Usage: ftp.pl host [port] file\n"; 
    exit(); 
} 
elsif($num_args == 3) 
{ 
    $port = $ARGV[1]; 
    $host = $ARGV[0]; 
    $filename = $ARGV[2]; 
    print "Connecting to $host on port $port.\n"; 
    $ftp = Net::FTP->new($host, Port => $port, Timeout => 30, Debug => 1) 
     or die "Can't open $host on port $port.\n"; 
} 
else 
{ 
    $host = $ARGV[0]; 
    $filename = $ARGV[1]; 
    print "Connecting to $host with the default port.\n"; 
    $ftp = Net::FTP->new($host, Timeout => 30, Debug => 1) 
     or die "Can't open $host on port $port.\n"; 
} 

print "Usename: "; 
my $username = <>; 
print "\nPassword: "; 
my $password = <>; 

$ftp->login($username, $password); 
$ftp->put($filename) or die "Can't upload $filename.\n"; 

print "Done!\n"; 

$ftp->quit; 

미리 감사드립니다.

+1

아마도 "작동하지 않는 부분"에 대해 자세히 설명해야합니다. 오류 메시지 등 어떻게 연결되지 않는지 어떻게 알 수 있습니까? – TLP

+0

오류 메시지가 없습니다. 정교화는 필요하지 않아야하며 연결되지 않습니다. 즉, Net :: FTP-> new()가 실패합니다. 그게 전부 야. 답변 해 주셔서 감사합니다. – Griffin

+0

Net :: FTP가 실패 할 수 없거나 스크립트가 중단되었을 수 있습니다. 사용자/암호 프롬프트가 나타 납니까? 그리고 사용자 이름과 암호를'chomp'로 기억해 봤습니까? – TLP

답변

2

이미 답변을 <> -><STDIN>으로 작성 했으므로 문제가 발생했다고 생각합니다. @ARGV에 무엇이 포함되어 있다면 <>은 '마법의 열린'상태입니다. Perl은 @ARGV의 다음 항목을 파일 이름으로 해석하고 열어서 한 줄씩 읽습니다. 따라서, 당신은 아마 같은 것을 할 수 있다고 생각 : 당신이

myname 
mypass 

다음

$ ftp.pl host 8020 file cred 

같은 파일 (말의 cred라는 이름의)에서 일부 연결 creditials이 있다면 그런

use strict; 
use Net::FTP; 
use warnings; 

use Scalar::Util 'looks_like_number'; 

if(@ARGV < 2) 
{ 
    print "Usage: ftp.pl host [port] file [credentials file]\n"; 
    exit(); 
} 

my $host = shift; # or equiv shift @ARGV; 
my $port = (looks_like_number $ARGV[0]) ? shift : 0; 
my $filename = shift; 

my @ftp_args = (
    $host, 
    Timeout => 30, 
    Debug => 1 
); 

if ($port) 
} 
    print "Connecting to $host on port $port.\n"; 
    push @ftp_args, (Port => $port); 
} 
else 
{ 
    print "Connecting to $host with the default port.\n"; 
} 
my $ftp = Net::FTP->new(@ftp_args) 
    or die "Can't open $host on port $port.\n"; 

#now if @ARGV is empty reads STDIN, if not opens file named in current $ARGV[0] 

print "Usename: "; 
chomp(my $username = <>); #reads line 1 of file 
print "\nPassword: "; 
chomp(my $password = <>); #reads line 2 of file 

$ftp->login($username, $password); 
$ftp->put($filename) or die "Can't upload $filename.\n"; 

print "Done!\n"; 

$ftp->quit; 

을 것 열린 호스트 : cred에서 credential을 사용하는 파일의 경우 8020

나는 당신이 그걸하고 싶지는 않다는 것을 알고 있습니다. 그게 바로 <>의 작동 방식입니다.

관련 문제