Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

script for accessing ftp servers

by terrytedzi (Initiate)
on Apr 05, 2012 at 11:34 UTC ( #963656=perlquestion: print w/replies, xml ) Need Help??
terrytedzi has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks: I am writing a perl script that download files from a directory C:/Users/Win7/Desktop/intenship doc2 in the local machine added to an ftp server, to another directory C:/users/Desk/personal in the same local machine not added to the ftp server. When I run the script the server outputs an error: (000019)4/4/2012 12:20:20 PM - terence (> 550 CWD failed. "/C:/Users/Win7/Desktop/intenship doc2": directory not found. I checked the directory investigating if i have mixed up the cases of the letters but everything was O.K, Can the monks help m? here is the code

#!/usr/local/bin/perl use DBI; use Net::FTP; use File::Basename; #connection paraneters to localhost $IP=''; $users='Terence'; $password='christine'; $directory="C:/Users/Win7/Desktop/intenship doc2/"; #Connection to netboss $ftp = Net::FTP->new($IP, Debug => 0, Passive => 0) or die "cannot + connect"; $ftp->login($users,$password); $ftp->cwd($directory); #list files @fichier=$ftp->ls($directory); foreach (@fichier) { $file = basename($_); if($file = "00wa-mb") { $ftp->get($file,"C:/users/Desk/personal".$file); } $ftp->quit;

Replies are listed 'Best First'.
Re: script for accessing ftp servers
by moritz (Cardinal) on Apr 05, 2012 at 11:40 UTC

    A FTP server usually doesn't give access to the whole file system, but rather to a well defined directory. This is for security reasons -- you wouldn't want to give a FTP user full access to all files in your system.

    So you can't expect absolut paths with drive letters to work (well, you can expect them to work, but they won't).

    So find out how the FTP server is configured, and use a path relative to the FTP server's root directory.

Re: script for accessing ftp servers
by Happy-the-monk (Canon) on Apr 05, 2012 at 12:00 UTC

    to debug FTP, either use an interactive FTP client or just as well your way described above, but aim low.
    See if you get a connection working.
    See if you get something back with the ls or dir methods/commands.
    See if that makes sense. Then cd one level and repeat the ls or dir etc. until you successfully arrive where you wanted to go. Use what you have learned about the FTP server's structure and check, if you can shorten the path to one cd only.
    Good luck!

    Cheers, Sören

Re: script for accessing ftp servers
by stevieb (Abbot) on Apr 05, 2012 at 19:32 UTC

    I installed FileZilla on my Win7 laptop to test this out for you. I ran the script from a Ubuntu VM. Note that I had to change some of the variables to make it work in my environment, so just change them back to what you had originally.

    moritz was of course right about the FTP root directory. When you configure a user on your FTP server and set their 'shared directory' to "C:/Users/Win7/Desktop/intenship doc2/", you are automatically dropped into that directory when you log into the system via FTP. When I define the variable $directory in my code below, I specify I want to be in the root directory, which on the FTP server is the shared directory itself. I can not go up in the directory tree. If you print out $ftp->pwd() after you've logged in, you won't see the entire C:/Users... path at all.

    In FileZilla I set up, I configured the 'stevieb' user with a 'shared directory' of C:/Users/steve/Desktop, and placed a file called 'blah.txt' there. Note in my code how I specify that I want to work in that directory. I've also fixed the code so that it is strict compliant (you should get in the habit of using 'use strict;' and 'use warnings;'

    #!/usr/bin/perl use DBI; use Net::FTP; use File::Basename; use warnings; use strict; #connection paraneters to localhost my $IP=''; my $users='stevieb'; my $password='blah'; my $directory="/"; #Connection to netboss my $ftp = Net::FTP->new($IP, Debug => 0, Passive => 0) or die "cannot +connect"; $ftp->login($users,$password); $ftp->cwd($directory); #list files my @fichier = $ftp->ls($directory); for my $entry (@fichier) { my $filename = basename( $entry ); if( $filename =~ "blah.txt" ){ # you'll need to uncomment the next line, and comment out # the one below it to return to your desired behaviour #$ftp->get($filename,"C:/users/Desk/personal".$filename); $ftp->get( $filename ); } } $ftp->quit;
Re: script for accessing ftp servers
by terrytedzi (Initiate) on Apr 11, 2012 at 10:45 UTC
    @stevieb thanks, the directory stuff works but i keep having the message: (000006)4/10/2012 11:40:30 AM - terence (> RETR books1 (000006)4/10/2012 11:40:30 AM - terence (> 550 File not found here books1 is just a file I wanted to transfer to the folder personal. I am sorry I haven't be able to be in the forum these days.
Re: script for accessing ftp servers
by terrytedzi (Initiate) on Apr 05, 2012 at 16:05 UTC

    I configured the filezilla server myself and in the "Share Folder" I included C:/Users/Win7/Desktop/intenship doc2. Now @moritz I am really new to all these stuff, so I don't understand when you say I should use a path relative to the FTP server's root directory. I think it will be great if you can guide me wrt the configuration

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://963656]
Approved by moritz
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (7)
As of 2018-06-25 13:38 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (126 votes). Check out past polls.