用 Perl 编写的 FTP 应用程序无法连接

发布于 2024-12-10 12:19:20 字数 1050 浏览 0 评论 0原文

为什么我的程序不能运行?它拒绝连接到主机,我尝试了两个不同的服务器并验证了使用哪个端口。 请注意,我对 Perl 的经验不是很丰富。

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;

提前致谢。

Why doesn't my program work? It refuses to connect to the host, I've tried two different servers and verified which port is used.
Note that I'm not very experienced when it comes to Perl.

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;

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寻梦旅人 2024-12-17 12:19:20

现在您已经有了答案 <> -> ,我想我看到了问题。当 @ARGV 包含任何内容时,<> 就是“魔法打开”。 Perl 将 @ARGV 中的下一项解释为文件名,打开它并逐行读取它。因此,我认为您可能可以这样做:

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)中有一些连接信用

myname
mypass

,那么

$ ftp.pl host 8020 file cred

将使用 cred 中的凭据打开文件的 host:8020 。

我不确定你想这样做,这就是 <> 的工作原理。

Now that you already have your answer <> -> <STDIN>, I think I see the problem. When @ARGV contains anything, <> is the 'magic open'. Perl interprets the next item in @ARGV as a filename, opens it and reads it line by line. Therefore, I think you can probably do something like:

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;

Then if you had some connection creditials in a file (say named cred) like

myname
mypass

then

$ ftp.pl host 8020 file cred

would open host:8020 for file using credentials in cred.

I'm not sure you want to do that, its just that THAT is how <> works.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文