0

我正在尝试 ftp 进入服务器,一旦我在那里,我想获取一个文件,然后在休眠 5 分钟后将其放回。我的那部分是正确的,但是我在代码中添加了两个将在执行代码时输入的变量。用户将输入他们想要连接的用户名。我在连接时遇到问题。当我输入用户名 t14pb 时,它仍然转到第一个 if 语句,好像我说的是 t14pmds。这是代码:

#!/usr/bin/perl

use Net::FTP;

$host     = "fd00p02";
$username = "$ARGV[0]";
$ftpdir   = "/";
$file     = "$ARGV[1]";

print "$username\n";
print "$file\n";

if ($username == t14pmds) {

  $password = "test1";

  $ftp = Net::FTP->new($host) or die "Error connecting to $host: $!";
  $ftp->login($username, $password) or die "Login failed: $!";
  $ftp->cwd($ftpdir) or die "Can't go to $ftpdir: $!";
  $ftp->get($file) or die "Can't get $file: $!";

  sleep 5;

  $ftp->put($file) or die "Can't put $file: $!";
  $ftp->quit or die "Error closing ftp connection: $!";
}

if ($username == t14pb) {

  $password = "test2";

  $ftp = Net::FTP->new($host) or die "Error connecting to $host: $!";
  $ftp->login($username, $password) or die "Login failed: $!";
  $ftp->cwd($ftpdir) or die "Can't go to $ftpdir: $!";
  $ftp->get($file) or die "Can't get $file: $!";

  sleep 5;

  $ftp->put($file) or die "Can't put $file: $!";
  $ftp->quit or die "Error closing ftp connection: $!";
}

if ($username == t14pmds_out) {

  $password = "test3";

  $ftp = Net::FTP->new($host) or die "Error connecting to $host: $!";
  $ftp->login($username, $password) or die "Login failed: $!";
  $ftp->cwd($ftpdir) or die "Can't go to $ftpdir: $!";
  $ftp->get($file) or die "Can't get $file: $!";

  sleep 5;

  $ftp->put($file) or die "Can't put $file: $!";
  $ftp->quit or die "Error closing ftp connection: $!";
}

if ($username == t14fiserv) {

  $password = "test4";

  $ftp = Net::FTP->new($host) or die "Error connecting to $host: $!";
  $ftp->login($username, $password) or die "Login failed: $!";
  $ftp->cwd($ftpdir) or die "Can't go to $ftpdir: $!";
  $ftp->get($file) or die "Can't get $file: $!";

  sleep 5;

  $ftp->put($file) or die "Can't put $file: $!";
  $ftp->quit or die "Error closing ftp connection: $!";
}
4

2 回答 2

5

Perl 标量有两种风格:字符串和数字。您使用 测试数字相等性==。如果字符串看起来不像数字,则认为它们等于零。这就是为什么总是执行第一个分支(0 == 0)。

您测试与 的字符串等价eq,因此您想比较 like if ($username eq "foo") {...}

但是,if-brances 中的代码不会改变,因此您可以将公共代码移到外面。剩下的是用户名和密码之间的映射——哈希有利于:

my %uname_2_passwd = (
   t14pmds => "test1", # LHS of "=>" is auto-quoted, 
   ...                 # if it would be a valid variable name.
);

my $password = $uname_2_passwd{$username};

# common code here

另外,请use strict; use warnings在脚本的顶部。这将指出大多数错误(例如忘记引号,或不使用 eq 进行字符串比较,或在数字比较中使用字符串,或未声明变量)。

于 2012-12-17T18:46:42.367 回答
2

在您编写的每个use strict程序中,以及use warnings在接近第一个使用点时声明变量,这一点尤为重要。my

你的整个程序最好这样写,使用散列作为查找表来查找每个用户的密码。

#!/usr/bin/perl

use strict;
use warnings;

use Net::FTP;

my ($username, $file) = @ARGV;

my %passwords = (
  t14pmds => 'test1',
  t14pb => 'test2',
  t14pmds_out => 'test3',
  t14fiserv => 'test4',
);

my $password = $passwords{$username} or die qq<Unknown username "$username">;

print "User: $username\n";
print "Pass: $file\n";

my $host     = 'fd00p02';
my $ftpdir   = '/';

my $ftp = Net::FTP->new($host) or die "Error connecting to $host: $!";
$ftp->login($username, $password) or die "Login failed: $!";
$ftp->cwd($ftpdir) or die "Can't go to $ftpdir: $!";
$ftp->get($file) or die "Can't get $file: $!";

sleep 5;

$ftp->put($file) or die "Can't put $file: $!";
$ftp->quit or die "Error closing ftp connection: $!";

print "All phases successful\n";
于 2012-12-17T23:01:38.940 回答