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

I need to use ftp client to get some files from the remote server.If the server is down or the file is not present, it should send an email to the administrator. I want to avoid the use of Net::FTP module. I checked a similar node on perlmonks and the code given doesn't work. My requirement is simple. As per the code below, it needs the ftp gateway. I don't have any ftp gateway...with the name of just the ftp server, I am able to connect otherwise. Can someone help me with just the ftp part(no problem with sending mail part)?
This should work with a gateway, this is working code with some modifi +ed names. my $gsRemoteMachine = "ftp.site.com"; my $gsRemoteDir = "/"; my $gsFtpId = "username"; my $gsFtpPwd = "password"; my $gsGateMachine = "gateway.site.com"; # ftp gateway my $gsGateConnect = "\@$gsRemoteMachine "; # remote machine connect +id for gateway my $gsRemoteMachine = $gsGateMachine; my $gsFeedsDir = "/mypath/"; my $gsFeedFile = "myfile"; system("ftp -n $RemoteMachine <<EOF> $StatusFile 2>&1\n\nuser $gsFtpId $gsGateConnect $gsFtpPwd\nverbose\n$gsFtpMode\nput $gsFeedsDir$gsFeed File $gsRemoteDir$gsFeedFile\nclose\nbye\nEOF");

Replies are listed 'Best First'.
Re: trouble while ftping through Perl Script
by matija (Priest) on Apr 29, 2004 at 11:47 UTC
    Why do you want to avoid the use of Net::FTP? I certainly think it would be the best tool for the job.

    You said you found the code you quote on perlmonks, but you don't tell us where, and you only quote a small excerpt of the code. After some super-searching, it turns out the script you found is meant to connect to a ftp server through a firewall - it depends on the gateway having specific functionality.

    If you absolutely refuse to have anything to do with Net::FTP (why? Why? WHY?), perhaps you could use Expect; and talk to the FTP prompts through that.

      Ok! I have decided to use the Net::FTP::Common module
      But, when I execute my code, I get the error
      error logging in: at /usr1/duser/ActivePerl-5.8/lib/site_perl/5.8.2/Net/FTP/Common.pm line 127. Can't call method "cwd" on an undefined value at /usr1/duser/ActivePerl-5.8/lib/site_perl/5.8.2/Net/FTP/Common.pm line 276.
      Is it a problem with my code or with the installation of the module? But, after installation of the module I got a message "Successfully installed Net-FTP-Common version 3.7 in ActivePerl 5.8.2.808." Here is my code
      use Net::FTP::Common; my $common_cfg = { Host => 'ftp.servername.com', User => 'username', Pass => 'password', RemoteDir => '/files' }; my $ftp = Net::FTP::Common->new($common_cfg, Debug => 0); $ftp->get("somefile.txt"); $ftp->quit;

        Provide problems, and we (try to) provide solutions, that's how perlmonks works. If you just told us your problems with Net::FTP at your first post, everyone had benefits out of it: you would have had the solution, and others would not have wasted their time trying to guess what the problem was and how to solve it

        Update: By the way, when you are going to use objects in Perl it's always better to check that you really have one before trying to use it. I mean:

        use Net::FTP; # this is wrong, because you cwd without checking # if $ftp is really an object or it is undef # (new() failed) $ftp = Net::FTP->new("some.host.name", Debug => 0) ; $ftp->cwd("/pub") ; # This is right instead, and it comes straight from # Net::FTP's documentation # See how every method call is checked for success $ftp = Net::FTP->new("some.host.name", Debug => 0) or die "Cannot connect to some.host.name: $@"; $ftp->login("anonymous",'-anonymous@') or die "Cannot login ", $ftp->message; $ftp->cwd("/pub") or die "Cannot change working directory ", $ftp->message; $ftp->get("that.file") or die "get failed ", $ftp->message; $ftp->quit;

        Ciao
        --bronto


        The very nature of Perl to be like natural language--inconsistant and full of dwim and special cases--makes it impossible to know it all without simply memorizing the documentation (which is not complete or totally correct anyway).
        --John M. Dlugosz

        The problem is that it is not logging in correctly. I would suggest that you use Net::FTP directly whereby you can get the error messages directly:

        use strict; use Net::FTP; my $client = Net::FTP->new('ftp.servername.com', Debug => 1) or die "Cannot connect : $@\n"; $client->cwd('/files') or die $client->message(); $client->get('somefile.txt') or die $client->message(); $client->quit()

        /J\

Re: trouble while ftping through Perl Script
by Tomte (Priest) on Apr 29, 2004 at 11:38 UTC

    [...]this is working code with some modified names.
    I doubt that very much, as you declare two lexical variables with the same name (or one variable twice, depends on how you see the problem ,), namely $gsRemoteMachine.

    You should RTFM of your ftp-client and/or RTF ftp-protocol-rfc (rfc 959).

    Maybe an introductory perl-text would also prove helpful, you could check the Tutorials...

    But why would you want to avoid to use a proven core(?) module?

    regards,
    tomte


    An intellectual is someone whose mind watches itself.
    -- Albert Camus

Re: trouble while ftping through Perl Script
by Abigail-II (Bishop) on Apr 29, 2004 at 11:20 UTC
    This should work with a gateway, this is working code with some modifi +ed names.
    Considering you have working code, what is your question? Preferably a Perl question, and not one about the syntax of your ftp program (the Perl part of your program is extremely trivial - just a bunch of assignments and a single system() call.)

    Abigail

    A reply falls below the community's threshold of quality. You may see it by logging in.