namishtiwari has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, Here is my piece of code--
#!/usr/bin/perl use Net::FTP; my $host="abc.def.com"; my $directory="/namish/logs/nali05/"; $ftp=Net::FTP->new($host,Timeout=>240) or $newerr=1; push @ERRORS, "Can't ftp to $host: $!\n" if $newerr; myerr() if $newerr; print "Connected\n"; $ftp->login("abcd","hi1234\@") or $newerr=1; print "Getting file list"; push @ERRORS, "Can't login to $host: $!\n" if $newerr; $ftp->quit if $newerr; myerr() if $newerr; print "Logged in\n"; $ftp->cwd($directory) or $newerr=1; push @ERRORS, "Can't cd $!\n" if $newerr; myerr() if $newerr; $ftp->quit if $newerr; @files=$ftp->dir or $newerr=1; push @ERRORS, "Can't get file list $!\n" if $newerr; myerr() if $newerr; print "Got file list\n"; foreach(@files) { print "$_\n"; } $ftp->quit; sub myerr { print "Error: \n"; print @ERRORS; exit 0; }
I am not getting the desired results as the files are not copied,am i missing anything here. It prints these lines--
RETRIEVING AOK LOG FILES FROM LOG.ARCOT.COM...... Connected Getting file listError: Can't login to abc.def.com:
Kindly give your valueable suggestions. I want to clear what exactly i am doing here.. I wrote the above perl script to get the files in a specifed directory called temp. The files which i get all are zipped ones. I want to unzip all the files and choose some which of my interest and delete rest of the files. I achieved the first part but i am struglling for the second. The files which i am getting after ftp are like this-- tali05_Jul122009_2358.zip tali05_Jul122009_4567.zip tali05_Jul122009_9876.zip I want to unzip all these files after getting them. When we unzip these files we get files like-- nt123_12Jul09_13_05_23.log ntrf_12Jul09_14_35_33.log and many more like this.. I want to keep only the first one and delete the second file. Kindly help me in achieving this. Thanks NT Reply With Quote Thanks NT

Replies are listed 'Best First'.
Re: ftp issue in perl
by Corion (Patriarch) on Jul 27, 2009 at 07:36 UTC

    The documentation for Net::FTP shows the following usage of the ->login method:

    $ftp->login($user, $password) or die "Cannot login ", $ftp->message;

    Maybe you would get more information if you used what the documentation says.

      I tried using that way also as you suggested but with no luck. I dont know why it is throwing the error. Thanks NT

        What was the message printed when you tried calling login like the documentation recommends?

        What did $ftp->message contain after trying to connect?

Re: ftp issue in perl
by cdarke (Prior) on Jul 27, 2009 at 07:41 UTC
    The login failed, you need to use the message method to discover the reason. This from Net::FTP doc:
    $ftp->login("anonymous",'-anonymous@') or die "Cannot login ", $ftp->message;
      Hi, I got it working with the way suggested by you people. It is working fine now. I have one question here, the files which i get all are zipped files, what i want is first unzip them and take the files which i want, the later part ie taking specific files i can do easily but how to unzip a file in perl. Thanks NT

        If this is the case you are going to have to connect to the server via something like Net::SSH, uncompress thie files you need from the archive and then transfer them. FTP can't provide that functionality.

        Also, in case you don't already know FTP - Security problems may be of interest.

        Martin

Re: ftp issue in perl
by apl (Monsignor) on Jul 27, 2009 at 11:32 UTC
    Unrelated suggestion ... You use the following construction several times:
    $ftp->cwd($directory) or $newerr=1; push @ERRORS, "Can't cd $!\n" if $newerr; myerr() if $newerr; $ftp->quit if $newerr;
    This does not clearly show you're trying to do, and distracts the viewer. An alternative might be:
    if ( !$ftp->cwd($directory) ) { push @ERRORS, "Can't cd $!\n"; myerr(); $ftp->quit; }
    Or, similarly
    $ftp->cwd($directory) or ReportError( "Can't cd" );
    where ReportError is a sub that takes the error-string as an argument, and does the push, the myerr, and the quit.
Re: ftp issue in perl
by Selvakumar (Scribe) on Jul 27, 2009 at 10:58 UTC
    I am using the below code and it's fine for me. Try this.
    use strict; use warnings; use Net::FTP; my $hostname = 'ftp.check.com'; my $username = 'check'; my $password = 'check'; # Hardcode the directory and filename to get my $home = '/ToFTP'; # Open the connection to the host my $ftp = Net::FTP->new($hostname); # construct object $ftp->login($username, $password); # log in #$ftp->cwd($home) or # die "Can't change directory ($home):" . # $ftp->message; my $pwd = $ftp->pwd; my @ls = $ftp->ls('-lR'); foreach (@ls) { print "$_\n"; } $ftp->quit;
      Hi Selva, This part is fine to me, i am also not getting any error in my code when i siply ftp a file from a server. but my requirement is diffrent, i want to FTP a file and then uncompress it and select some which i want. Thanks NT