Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

by Spidy (Chaplain)
on Aug 19, 2005 at 05:36 UTC ( #485023=sourcecode: print w/replies, xml ) Need Help??
Category: Web Stuff
Author/Contact Info,
Description: A very barebones, quick and dirty mini-FTP client I built myself (The Perl Cookbook is a wonderful thing indeed!), when the nag screen of my not-so-free FTP client started to annoy me(read: I am a broke,non-pirating student). I haven't quite worked out the error reporting on it yet, but it's VERY useful for synchronizing something on your web server with whatevers in the folder you put in.

If you're not too worried about having your password echoed back to your screen, you can remove the lines that involve Term::ReadKey and any changes in ReadMode.

#!/usr/bin/perl -w
use strict;
use Net::FTP;
use Net::FTP::File;
use Term::ReadKey;
use diagnostics;

my $username;
my $password;
my $directory;
my $localfile;
my $remotefile;
my $filename;
my $domain;

print "Luke's Neato FTP Script, Version 1.1\n";

print "Domain: ";
chomp($domain = <STDIN>);
my $ftp;

if($ftp = Net::FTP->new($domain)) {
    print "Successfully connected to $domain\n";
} else {
    print "Couldn't connect:\n\t" . $ftp->message ;

print "Username: ";
chomp($username = <STDIN>);
ReadMode 2;
print "Password: ";
chomp($password = <STDIN>);
ReadMode 0;

if($ftp->login($username, $password)) {
    print "\nSuccessfully logged in as $username\@$domain\n";
} else {
    print "Couldn't Login:\n\t" . $ftp->message ;

print "Working Directory: ";
chomp($directory = <STDIN>);

if($ftp->cwd($directory)) {
    # change working directory
    print "Successfully changed working directory to $directory";
} else {
    print "Couldn't change working directory:\n\t" . $ftp->message ;

my @filearray;
my $input = '';
my $count = 0;
print "\n";
while () {
    print "File to upload: ";
    if ($input ne '') {
        push @filearray, $input;
    if ($input eq '') { last; }

foreach my $element (@filearray){
    my $chvalue = 0;
    if($element =~ /\.(.+?)$/){
        if($1 eq 'cgi' || $1 eq 'pl' || $1 eq 'pm') {
            # transfer in ASCII mode
            $chvalue = 755;
            # chmod it to 755
        } else {
            $chvalue = 644;
        if($ftp->put($element)) {
            print "$element uploaded successfully to $directory at $do
             if($ftp->chmod($chvalue, $element)) {
                 print "$element successfully chmoded to $chvalue.\n";
             } else {
                     print $ftp->message;
        } else {
            print "Couldn't put $element:\n\t" . $ftp->message ;
    } else {
        if($ftp->put($element)) {
            print "$element uploaded successfully to $directory at $do
        } else {
            print $ftp->message;

# quits, fancy that.
sub quit {
    print "Press any key to exit";
    chomp(my $chomp = <STDIN>);
Replies are listed 'Best First'.
by SamCG (Hermit) on Sep 07, 2005 at 19:59 UTC
    Pretty nice little command-line interface; I like that it chmods the files.

    If I were to make a suggestion, it would seem good to modify this so you didn't have to put copies of in each folder (I know technically you don't, you could put the fully qualified file names into the argument array). Maybe make the argument be a folder that syncs up all the files with the domain given?
      Erm...not 100% certain as to what you're saying. Do you mean something like, inputting the path to a folder on YOUR computer, that it synchronizes the files of to the folder on your webserver?

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: sourcecode [id://485023]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (3)
As of 2020-09-30 00:55 GMT
Find Nodes?
    Voting Booth?
    If at first I donít succeed, I Ö

    Results (155 votes). Check out past polls.