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

perl: FTP using cgi not working but FTP s/w works well

by need_help21 (Novice)
on Nov 20, 2007 at 14:03 UTC ( [id://651931]=perlquestion: print w/replies, xml ) Need Help??

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

Hi All, Really need some help here. I am using PERL CGI application for uploading files through FTP and its not working. The message that it shows is "Can't open data connection". Only the file that i want to upload is created at the destination with zero bytes. Well when i do FTP using a client software, it works for that same remote machine and files are uploaded. This code works well when executed to connect another system in the same network and files are uploaded. The Code is:
sub fnSaveFile { my $sDriver = $vars{'driverfname'}; my $sXML = $vars{'xmlfname'}; #print $sDriver; if ($sDriver eq '' || $sXML eq '') { fnShowForm(); print "please provide information for the empty fie +lds to execute"; exit(); } my $sFile = "C:/MyCreationInPerl/$sDriver"; open(FILE,">$sFile") or die "Can not open file $sDriver"; $sReport1 = 'temp'; print FILE "some information"; close FILE; my $sFile = "C:/MyCreationInPerl/$sXML"; open(FILE,">$sFile") or die "cannot open file"; print FILE "some information"; close FILE; my $host = 'xx.xx.xx.xx'; my $ftp = Net::FTP->new($host, Timeout => 300, Debug=>1) or die "Cannot contact $host: $!"; my $output2 = $ftp->login('xxxx','xxxxxx') or die "Can't login ($host):" . $ftp->message; $ftp->pasv(); my $path1 = '/testdata/BatchValidation/driver/'; my $output = $ftp->cwd($path1) or die "cannot change dir +ectory $path1"; $ftp->ascii(); my $filenameDriver = "C:\\MyCreationInPerl\\$sDriver"; my $output1 = $ftp->put($filenameDriver) or die $ftp->message; print $output1; #my $path2 = '/testdata/BatchValidation/xml'; #my $output = $ftp->cwd($path2) or die "cannot change director +y $path2"; #$ftp->ascii(); #my $filenameXML = "C:\\MyCreationInPerl\\$sXML"; #my $output3 = $ftp->put($filenameXML) or die "cannot put $fil +enameXML to the destination"; ##print $output1; #Win32::MsgBox ("Files have been transfered"); $ftp->quit; fnShowFormForExecution(); }
Please someone help me!! Thanks !!

Replies are listed 'Best First'.
Re: perl: FTP using cgi not working but FTP s/w works well
by tuxz0r (Pilgrim) on Nov 20, 2007 at 17:08 UTC
    FTP uses two connections, one for commands/control and one for data (ports 21 and 22 I believe). Where in your script does the error message occur? At login? Prior to attempting the "put" for the file? "Can't open data connection" sounds like a connectivity/port issues. When you ftp files to the remote server on the command line, are you using passive mode there? Maybe try turning off passive mode and see if it makes a good data connection. What sort of troubleshooting have you already done with this? Give us some ideas of what you've tested so far, and folks can probably help point you in the right direction.

    ---
    echo S 1 [ Y V U | perl -ane 'print reverse map { $_ = chr(ord($_)-1) } @F;'
    Warning: Any code posted by tuxz0r is untested, unless otherwise stated, and is used at your own risk.

      The error i believe occurs at the place prior to attempting the "put" command. Even the turning off the Passive mode (by removing $ftp-> pasv() from the code) doesn't work and result is same. The file that was supposed to be transferred is created at the destination while executing this code but with ZERO bytes and no data is transferred. I even tried to connect the remote machine manually (instead of CGI code), i am able to login, but the file is not getting transfered. But strange to me that while using a software (Filezilla) i am able to FTP the files easily.
        As erroneouBollock noted, you need to bring this up with your administrator, I wasn't aware this had been brought up in the CB. If ftp'ing from your script and the command line don't work, I don't think using Filezilla would make a difference. Try using 'scp' to get the file their instead if ftp isn't working for you:
        $ scp -C localfile username@remotemachine:/remote/file/location/

        ---
        echo S 1 [ Y V U | perl -ane 'print reverse map { $_ = chr(ord($_)-1) } @F;'
        Warning: Any code posted by tuxz0r is untested, unless otherwise stated, and is used at your own risk.

        As a few of us said in the CB, you're likely suffering from the ftp-data port being blocked (incoming to your web server), combined with the FTP server not implementing PASV. If that's true nothing you change in your code will make it work.

        Even if your desktop PC receives ftp-data connections OK, that's no reason to believe that your network administrator has allowed the same for the web server.

        Either have your network administrator sort it out, or use another protocol. (eg: scp, webdav, rsync, etc)

        -David

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://651931]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (1)
As of 2024-04-19 18:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found