Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Having a problem with Net::FTP, I think...

by Spidy (Chaplain)
on Sep 05, 2005 at 19:17 UTC ( #489274=perlquestion: print w/replies, xml ) Need Help??
Spidy has asked for the wisdom of the Perl Monks concerning the following question:

I've written myself a script to do all of my FTP stuff for me, but it's having a problem: it's hanging right before the line where it is supposed to put() the file into where I want it to go.
Code Below:
#!/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 "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; quit(); } 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; quit(); } 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; quit(); } my @filearray = (); my $input = ''; my $count = 0; print "\n"; while () { print "File to upload: "; chomp($input=<STDIN>); if ($input ne '') { push @filearray, $input; $count++; next; } if ($input eq '') { last; } } foreach my $element (@filearray){ my $chvalue = 0; if($element =~ /\.(.+?)$/){ if($1 eq 'cgi' || $1 eq 'pl' || $1 eq 'pm') { $ftp->ascii; #transfer in ASCII mode $chvalue = 755; #chmod it to 755 } else { $ftp->binary; $chvalue = 644; } print "Preparing to put $element into $directory on $domain... +\n"; if($ftp->put($element)) { print "$element uploaded successfully to $directory at $do +main\n"; if($ftp->chmod($chvalue, $element)) { print "$element successfully chmoded to $chvalue.\n"; } else { print $ftp->message; quit(); } } else { print "Couldn't put $element:\n\t" . $ftp->message; quit(); } } else { if($ftp->put($element)) { print "$element uploaded successfully to $directory at $do +main\n" } else { print $ftp->message; quit(); } } } quit(); #quits, fancy that. sub quit { $ftp->quit(); print "Press any key to exit"; chomp(my $chomp = <STDIN>); exit; }

Does anyone have an idea for what's going wrong? When I run the script, it will get to the Preparing to put $element into $directory on $domain...\n line perfectly fine, but completely stop after that. The file I'm trying to upload is all of 5 kb, so I know that it's not just taking it's time because the file is large.


Replies are listed 'Best First'.
Re: Having a problem with Net::FTP, I think...
by sk (Curate) on Sep 05, 2005 at 22:10 UTC
    The script works fine in uploading the file. It quits on chmod with this error message.

    Can you add  $ftp->debug(1); before you ask for username and such?

    (Note: server specific information masked)

    Preparing to put c:/testftp.cgi into **** on ****... c:/testftp.cgi uploaded successfully to **** at **** Unknown command S +ITE CHMOD. Press any key to exit

    the file on Windows

    hello world hello world hello world

    The file on remote machine (Solaris)

    [sk]% cat testftp.cgi hello world hello world hello world

    Is your program stalling after  print "Preparing to put $element into $directory on $domain...\n"; or does it quit with some error message?


      It is stalling after print "Preparing to put $element into $directory on $domain...\n";.
        After turning on $ftp->debug(1);, this was what was returned at the stallpoint:
        Net::FTP=GLOB(0x18bbb90)>>> ALLO 5012 Net::FTP=GLOB(0x18bbb90)<<< 200 Zzz... Net::FTP=GLOB(0x18bbb90)>>> PORT ***** Net::FTP=GLOB(0x18bbb90)<<< 200 PORT command successful Net::FTP=GLOB(0x18bbb90)>>> STOR ****.cgi
Re: Having a problem with Net::FTP, I think...
by mattk (Pilgrim) on Sep 05, 2005 at 23:41 UTC
    Have you got an unmolested connection directly to your FTP server, or is it traversing firewalls/NAT? Have you tried setting $ftp->pasv()?
      Setting $ftp->pasv() did nothing, either.
Re: Having a problem with Net::FTP, I think...
by davidrw (Prior) on Sep 05, 2005 at 19:33 UTC
    If you comment out the chmod'ing does it work? I'm wondering if it's getting stuck there and you're just not seeing the 'uploaded successfully' message because of buffering -- try $|=1; at the top of your script..

      Don't think buffering is the issue here, as he has a "\n" at the end of the print line. It should be flushed right the way, if it ever reaches here.

      Even with the chmod'ing commented out, the same thing happens. with $| = 1; at the top, it still stalls.
Re: Having a problem with Net::FTP, I think...
by izut (Chaplain) on Sep 06, 2005 at 11:17 UTC

    I know it could be silly but have you tried doing that procedure with the ftp command before you automate that, from the mentionated host?

    If you can reproduce the commands using the ftp client, check if passive mode is on.

    Igor S. Lopes - izut
    surrender to perl. your code, your rules.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (2)
As of 2018-05-26 04:52 GMT
Find Nodes?
    Voting Booth?