Gossamer Forum
Home : General : Perl Programming :

Using FTP to get file from another server

Quote Reply
Using FTP to get file from another server
This post

http://www.gossamer-threads.com/perl/gforum/gforum.cgi?post=247504

seems to be very relevant to what I'm interested in.

I want to hack my dbman script so that the following happens:

1. ftp to govt website
2. download 3 files to replace the existing copies on my server.

(Ideally I'd like the script to just grab the data off of the govt site each time, but that's another step)

If someone could give me the basic steps, I'm pretty sure I can make it work, but at the momemt I'm not sure where to start.

I would appreciate any guidance and any advice on security issues, etc.

TIA, -Mike.
Quote Reply
Re: [Watts] Using FTP to get file from another server In reply to
You could just do it with a shell script.

#!/bin/sh

cd /path/to/folder/where/file/are
rm -f *
wget http://www.locationsite.com/file1.tar
wget http://www.locationsite.com/file2.tar
wget http://www.locationsite.com/file3.tar

Not sure if these files are available on a normal server, so this may not do what you want :(

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Using FTP to get file from another server In reply to
Here's what I got to work...
Code:
#!/usr/local/bin/perl
use strict;
use warnings;
use CGI::Carp 'fatalsToBrowser';
use CGI qw/:standard/;

use Net::FTP;

my $ftp = Net::FTP->new("ofacftp.treas.gov", Debug => 0)
or die "Cannot connect to some.host.name: $@";

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

$ftp->cwd("/fac_delim")
or die "Cannot change working directory ", $ftp->message;

$ftp->get("ADD.DEL");

$ftp->quit;

This retrieves the file from the treasury dept's ftp server and places a copy of it on mine.

Question: by declaring "my $ftp" that is pretty much 'clearing out' the variable in case $ftp was used earlier in the script, right? I see how it all works but I am confused by the fact that nothing tells $ftp to actually run. Like "return $ftp" or "exec $ftp" or something (?).

Also... what role does the following play?
use strict;
use warnings;
use CGI::Carp 'fatalsToBrowser';
use CGI qw/:standard/;

Can I leave these out? Are there any that I should *not* leave out?

Just curious...
Quote Reply
Re: [Watts] Using FTP to get file from another server In reply to
Hi. Glad it working :)

>>>Question: by declaring "my $ftp" that is pretty much 'clearing out' the variable in case $ftp was used earlier in the script, right?<<<

Just using it without the 'my' should be ok, unless it hasn't been pre-defined as a variable before. I would just leave it as 'my' .. as this only 'masks' the previous definition.


Quote:
use strict;
use warnings;
use CGI::Carp 'fatalsToBrowser';
use CGI qw/:standard/;

You *could* leave all of these out. The first one means you have to define varibles..i.e put 'my' in front of them. The warnings one just shows warning with your codes; the fatalsToBrowser part i just a Perl module, that catches errors. I use it in most of my scripts, as its great to catch errors :)

The last one you could get rid of it. The only reason you would need it, is if you were doing something like;

Code:
my $IN = new CGI;

print $IN->header();
my $val = $IN->param('field');

Cheers

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!