Reputation: 1
I am trying to ftp into a server, once I'm there, I want to get a file, then put it back after sleeping for 5 minutes. I have that part correct, but i added to the code, two variables that will be inputted when the code is executed. The user will input the username they want to connect with. I am having trouble connecting though. When I input the username t14pb, it still goes to the the first if statement, as if I said t14pmds. here is the code:
#!/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: $!";
}
Upvotes: 0
Views: 263
Reputation: 126722
It is especially important to use strict
and use warnings
in every program you write, and declare variables using my
close to their first poiint of use.
Your entire program is best written like this, using a hash as a lookup table to find the password for each user.
#!/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";
Upvotes: 2
Reputation: 57590
Perl scalars come in two flavours: stringy and numeric. You test for numeric equality with ==
. Strings are considered to be equal to zero, if they don't look like a number. This is why the first branch is always executed (0 == 0).
You test for stringy equivalence with eq
, so you want to compare like if ($username eq "foo") {...}
.
However, the code in the if-brances doesn't change, so you can move the common code outside. What remains is a mapping between usernames and passwords – something a hash is good for:
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
Also, please use strict; use warnings
at the top of your script. This would have pointed out most mistakes (like forgetting quotes, or not using eq for string comparision, or using strings in numeric comparisions, or not declaring your variables).
Upvotes: 5