Operating System - HP-UX
1831320 Members
3009 Online
110023 Solutions
New Discussion

Automatically FTP logon and Grab file

 
SOLVED
Go to solution
Rootberry
Advisor

Automatically FTP logon and Grab file

I need a script that will automatically logon to another server, ftp and pull down a file. Something I can launch in my crontab. Does anyone have something like this?
4 REPLIES 4
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: Automatically FTP logon and Grab file

Try the attached Perl script, ftpget.pl. Invoke as ftpget.pl -u for full usage.

As an example:

typeset -i STAT=0
ftpget.pl -h remotehost -l mickey -p topsecret -A -d /xxx/yyy myfile
STAT=${?}
if [[ ${STAT} -eq 0 ]]
then
echo "Ok"
else
echo "Failed; status = ${STAT}" >&2
fi
exit ${STAT}

This script will also use a .netrc file so that it is not necessary to pass the password on the command line.
If it ain't broke, I can fix that.
Sean Dale
Trusted Contributor

Re: Automatically FTP logon and Grab file

A very simple perl script could do this:


#!/usr/bin/perl

use Net::FTP;

$ftp = Net::FTP->new("servername", Debug => 0);
$ftp->login("username",'password');
$ftp->cwd("/tmp");
$ftp->get("myfile.txt");
$ftp->quit;

replace servername, etc. with appropriate values
Live life everyday
A. Clay Stephenson
Acclaimed Contributor

Re: Automatically FTP logon and Grab file

I suppose that I should add that the above example FTP's to host "remhost"; logs in as user "mickey"; password "topsecret"; sets ASCII transfer mode; cd's to directory /xxx/yyy; and gets myfile.
If the exit status is 0; all of those steps worked. That's the beauty of using Perl's Net::FTP module; the error-checking is free. It's rather messy to properly capture all the errors from FTP in a shell script.
If it ain't broke, I can fix that.
James R. Ferguson
Acclaimed Contributor

Re: Automatically FTP logon and Grab file

Hi;

There a several variations and this is one:

{ echo "open $myhost
user $user $password
get $remotefile $localfile
close"
} | ftp -i -n -v

For a bit better security look at the manpages for 'netrc(4)' and use the '.netrc' autologin approach.

Regards!

...JRF...